Sha256: 81826de7405a3f1982a274ea66223db75bc31bbf95ef9e8a938bf07f2922f5cc

Contents?: true

Size: 1.1 KB

Versions: 15

Compression:

Stored size: 1.1 KB

Contents

module Jekyll
  class RelatedPosts

    class << self
      attr_accessor :lsi
    end

    attr_reader :post, :site

    def initialize(post)
      @post = post
      @site = post.site
      require 'classifier' if site.lsi
    end

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

      if self.site.lsi
        build_index
        lsi_related_posts
      else
        most_recent_posts
      end
    end


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

        self.site.posts.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.content, 11) - [self.post]
    end

    def most_recent_posts
      recent_posts = self.site.posts.reverse - [self.post]
      recent_posts.first(10)
    end

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

Version data entries

15 entries across 15 versions & 2 rubygems

Version Path
jekyll-1.5.1 lib/jekyll/related_posts.rb
jekyll-1.5.0 lib/jekyll/related_posts.rb
jekyll-2.0.0.alpha.1 lib/jekyll/related_posts.rb
jekyll-1.4.3 lib/jekyll/related_posts.rb
jekyll-1.4.2 lib/jekyll/related_posts.rb
jekyll-1.4.1 lib/jekyll/related_posts.rb
jekyll-1.4.0 lib/jekyll/related_posts.rb
jekyll-1.3.1 lib/jekyll/related_posts.rb
jekyll-1.3.0 lib/jekyll/related_posts.rb
jekyll-1.3.0.rc lib/jekyll/related_posts.rb
monad-0.0.3 lib/jekyll/related_posts.rb
jekyll-1.2.1 lib/jekyll/related_posts.rb
jekyll-1.2.0 lib/jekyll/related_posts.rb
jekyll-1.1.2 lib/jekyll/related_posts.rb
jekyll-1.1.1 lib/jekyll/related_posts.rb