Sha256: c2a4f4f4e50ccac02eee32efc57a18a626077ecf3e34959adb80636cf4b9a0bc

Contents?: true

Size: 1.11 KB

Versions: 16

Compression:

Stored size: 1.11 KB

Contents

module Jekyll
  class RelatedPosts
    class << self
      attr_accessor :lsi
    end

    attr_reader :post, :site

    def initialize(post)
      @post = post
      @site = post.site
      Jekyll::External.require_with_graceful_fail('classifier-reborn') if site.lsi
    end

    def build
      return [] unless site.posts.docs.size > 1

      if site.lsi
        build_index
        lsi_related_posts
      else
        most_recent_posts
      end
    end

    def build_index
      self.class.lsi ||= begin
        lsi = ClassifierReborn::LSI.new(:auto_rebuild => false)
        display("Populating LSI...")

        site.posts.docs.each do |x|
          lsi.add_item(x)
        end

        display("Rebuilding index...")
        lsi.build_index
        display("")
        lsi
      end
    end

    def lsi_related_posts
      self.class.lsi.find_related(post, 11)
    end

    def most_recent_posts
      @most_recent_posts ||= (site.posts.docs.reverse - [post]).first(10)
    end

    def display(output)
      $stdout.print("\n")
      $stdout.print(Jekyll.logger.formatted_topic(output))
      $stdout.flush
    end
  end
end

Version data entries

16 entries across 16 versions & 3 rubygems

Version Path
blackboard-3.1.9 lib/jekyll/related_posts.rb
blackboard-3.1.8 lib/jekyll/related_posts.rb
blackboard-3.1.7 lib/jekyll/related_posts.rb
jekyll-3.2.0.pre.beta1 lib/jekyll/related_posts.rb
jekyll-3.1.6 lib/jekyll/related_posts.rb
jekyll-3.1.5 lib/jekyll/related_posts.rb
jekyll-3.1.4 lib/jekyll/related_posts.rb
jekyll-3.1.3 lib/jekyll/related_posts.rb
jekyll-3.1.2 lib/jekyll/related_posts.rb
jekyllplusadmin-1.1.0 lib/jekyll/related_posts.rb
jekyllplusadmin-1.0.0 lib/jekyll/related_posts.rb
jekyll-3.1.1 lib/jekyll/related_posts.rb
jekyll-3.1.0 lib/jekyll/related_posts.rb
jekyll-3.1.0.pre.rc3 lib/jekyll/related_posts.rb
jekyll-3.1.0.pre.rc2 lib/jekyll/related_posts.rb
jekyll-3.1.0.pre.rc1 lib/jekyll/related_posts.rb