diff --git a/lib/jekyll.rb b/lib/jekyll.rb index 2b4f127a..d466397c 100644 --- a/lib/jekyll.rb +++ b/lib/jekyll.rb @@ -40,6 +40,7 @@ require 'jekyll/draft' require 'jekyll/filters' require 'jekyll/static_file' require 'jekyll/errors' +require 'jekyll/related_posts' # extensions require 'jekyll/plugin' diff --git a/lib/jekyll/post.rb b/lib/jekyll/post.rb index 9d10a5cb..c478adb5 100644 --- a/lib/jekyll/post.rb +++ b/lib/jekyll/post.rb @@ -244,29 +244,7 @@ module Jekyll # # Returns an Array of related Posts. def related_posts(posts) - return [] unless posts.size > 1 - - if self.site.lsi - build_index - - related = self.class.lsi.find_related(self.content, 11) - related - [self] - else - (posts - [self])[0..9] - end - end - - def build_index - self.class.lsi ||= begin - puts "Starting the classifier..." - lsi = Classifier::LSI.new(:auto_rebuild => false) - $stdout.print(" Populating LSI... "); $stdout.flush - self.site.posts.each { |x| $stdout.print("."); $stdout.flush; lsi.add_item(x) } - $stdout.print("\n Rebuilding LSI index... ") - lsi.build_index - puts "" - lsi - end + Jekyll::RelatedPosts.new(self).build end # Add any necessary layouts to this post. diff --git a/lib/jekyll/related_posts.rb b/lib/jekyll/related_posts.rb new file mode 100644 index 00000000..8de5dc78 --- /dev/null +++ b/lib/jekyll/related_posts.rb @@ -0,0 +1,58 @@ +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 + (self.site.posts - [self.post])[0..9] + end + + def display(output) + $stdout.print("\n") + $stdout.print(Jekyll::Stevenson.formatted_topic(output)) + $stdout.flush + end + end +end diff --git a/lib/jekyll/site.rb b/lib/jekyll/site.rb index 0b77b5e6..e8b65135 100644 --- a/lib/jekyll/site.rb +++ b/lib/jekyll/site.rb @@ -71,8 +71,6 @@ module Jekyll # # Returns nothing. def setup - require 'classifier' if self.lsi - # Check that the destination dir isn't the source dir or a directory # parent to the source dir. if self.source =~ /^#{self.dest}/ diff --git a/test/test_related_posts.rb b/test/test_related_posts.rb new file mode 100644 index 00000000..98833f2f --- /dev/null +++ b/test/test_related_posts.rb @@ -0,0 +1,41 @@ +require 'helper' + +class TestRelatedPosts < Test::Unit::TestCase + context "building related posts without lsi" do + setup do + stub(Jekyll).configuration do + Jekyll::Configuration::DEFAULTS.merge({'source' => source_dir, + 'destination' => dest_dir}) + end + @site = Site.new(Jekyll.configuration) + end + + should "use the most recent posts for related posts" do + @site.reset + @site.read + assert_equal @site.posts[0..9], Jekyll::RelatedPosts.new(@site.posts.last).build + end + end + + context "building related posts with lsi" do + setup do + stub(Jekyll).configuration do + Jekyll::Configuration::DEFAULTS.merge({'source' => source_dir, + 'destination' => dest_dir, + 'lsi' => true}) + end + @site = Site.new(Jekyll.configuration) + end + + should "use lsi for the related posts" do + @site.reset + @site.read + require 'classifier' + any_instance_of(::Classifier::LSI) do |c| + stub(c).find_related { @site.posts[-1..-9] } + stub(c).build_index + end + assert_equal @site.posts[-1..-9], Jekyll::RelatedPosts.new(@site.posts.last).build + end + end +end