Sha256: 1fc109e3fa97ebd722ff625188478c0a8fd6b7b66a79ee18cda6ec9ec0818a88

Contents?: true

Size: 1.01 KB

Versions: 9

Compression:

Stored size: 1.01 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)
        Jekyll.logger.info("Populating LSI...")

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

        Jekyll.logger.info("Rebuilding index...")
        lsi.build_index
        Jekyll.logger.info("")
        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
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
jekyll-3.5.2 lib/jekyll/related_posts.rb
jekyll-3.5.1 lib/jekyll/related_posts.rb
jekyll-3.4.5 lib/jekyll/related_posts.rb
jekyll-3.5.0 lib/jekyll/related_posts.rb
jekyll-3.4.4 lib/jekyll/related_posts.rb
jekyll-3.4.3 lib/jekyll/related_posts.rb
jekyll-3.4.2 lib/jekyll/related_posts.rb
jekyll-3.4.1 lib/jekyll/related_posts.rb
jekyll-3.4.0 lib/jekyll/related_posts.rb