From f67f7f3db43e64c9fc174d3a8e6e6651cffe93f8 Mon Sep 17 00:00:00 2001 From: Matt Rogers Date: Tue, 7 May 2013 22:27:38 -0500 Subject: [PATCH 1/6] Move the building of related posts into their own class --- lib/jekyll/post.rb | 26 ++-------------- lib/jekyll/related_posts.rb | 59 +++++++++++++++++++++++++++++++++++++ lib/jekyll/site.rb | 2 -- 3 files changed, 62 insertions(+), 25 deletions(-) create mode 100644 lib/jekyll/related_posts.rb diff --git a/lib/jekyll/post.rb b/lib/jekyll/post.rb index 571d854d..287bade0 100644 --- a/lib/jekyll/post.rb +++ b/lib/jekyll/post.rb @@ -1,3 +1,5 @@ +require 'jekyll/related_posts' + module Jekyll class Post include Comparable @@ -244,29 +246,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..7c5a597d --- /dev/null +++ b/lib/jekyll/related_posts.rb @@ -0,0 +1,59 @@ +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 + puts "Starting the classifier..." + lsi = Classifier::LSI.new(:auto_rebuild => false) + display(" Populating LSI... ") + + self.site.posts.each do |x| + display(".") + lsi.add_item(x) + end + + display("\n Rebuilding LSI index... ") + lsi.build_index + puts "" + lsi + end + end + + def lsi_related_posts + 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(output) + $stdout.flush + end + + end +end diff --git a/lib/jekyll/site.rb b/lib/jekyll/site.rb index d8a36e1b..f9b223fd 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}/ From 64f8a7b2289c17f8de1bf6e091d94580857cca48 Mon Sep 17 00:00:00 2001 From: Matt Rogers Date: Wed, 8 May 2013 07:39:00 -0500 Subject: [PATCH 2/6] Move the require for related_posts to jekyll.rb --- lib/jekyll.rb | 1 + lib/jekyll/post.rb | 2 -- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/jekyll.rb b/lib/jekyll.rb index 4f3bc1cc..0728a8b5 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 287bade0..c5f5dc5e 100644 --- a/lib/jekyll/post.rb +++ b/lib/jekyll/post.rb @@ -1,5 +1,3 @@ -require 'jekyll/related_posts' - module Jekyll class Post include Comparable From 1aa49fa734c38c42ec14a1847ab9319e8ede3adc Mon Sep 17 00:00:00 2001 From: Matt Rogers Date: Wed, 8 May 2013 21:57:10 -0500 Subject: [PATCH 3/6] Wrap tests around Jekyll::RelatedPosts This gives me more confidence that we're doing the right things when it comes to both the LSI and non-LSI cases and prevents regressions. --- test/test_related_posts.rb | 40 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 test/test_related_posts.rb diff --git a/test/test_related_posts.rb b/test/test_related_posts.rb new file mode 100644 index 00000000..cff43362 --- /dev/null +++ b/test/test_related_posts.rb @@ -0,0 +1,40 @@ +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] } + end + assert_equal @site.posts[-1..-9], Jekyll::RelatedPosts.new(@site.posts.last).build + end + end +end From 08b49ec9db3d814dc84f4b397741f2799b042c82 Mon Sep 17 00:00:00 2001 From: Matt Rogers Date: Wed, 8 May 2013 22:02:23 -0500 Subject: [PATCH 4/6] Update the display of the LSI progress output It now fits more in line with what the other messages display. --- lib/jekyll/related_posts.rb | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/lib/jekyll/related_posts.rb b/lib/jekyll/related_posts.rb index 7c5a597d..ab13a968 100644 --- a/lib/jekyll/related_posts.rb +++ b/lib/jekyll/related_posts.rb @@ -24,26 +24,25 @@ module Jekyll end end + def build_index self.class.lsi ||= begin - puts "Starting the classifier..." lsi = Classifier::LSI.new(:auto_rebuild => false) - display(" Populating LSI... ") + display("\n Populating LSI... ") self.site.posts.each do |x| - display(".") lsi.add_item(x) end - display("\n Rebuilding LSI index... ") + display("\nRebuilding index... ") lsi.build_index - puts "" + display("") lsi end end def lsi_related_posts - lsi.find_related(post.content, 11) - [self.post] + self.class.lsi.find_related(post.content, 11) - [self.post] end def most_recent_posts @@ -54,6 +53,5 @@ module Jekyll $stdout.print(output) $stdout.flush end - end end From 770402d91269a4e6271582bbef98bd762fc8e260 Mon Sep 17 00:00:00 2001 From: Matt Rogers Date: Wed, 8 May 2013 22:33:20 -0500 Subject: [PATCH 5/6] Also stub the building of the index Since we don't actually use the index in getting the related posts from the tests there's no need to build an index, which can take a long time if the ruby bindings for the GSL library are not installed. --- test/test_related_posts.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/test/test_related_posts.rb b/test/test_related_posts.rb index cff43362..98833f2f 100644 --- a/test/test_related_posts.rb +++ b/test/test_related_posts.rb @@ -33,6 +33,7 @@ class TestRelatedPosts < Test::Unit::TestCase 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 From 98aff03595130eea9b4e37e2de65515156b19bad Mon Sep 17 00:00:00 2001 From: Matt Rogers Date: Tue, 14 May 2013 22:04:34 -0500 Subject: [PATCH 6/6] Use Jekyll::Stevenson#formatted_topic for printing messages Beats trying to line them up yourself. :-) --- lib/jekyll/related_posts.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/jekyll/related_posts.rb b/lib/jekyll/related_posts.rb index ab13a968..8de5dc78 100644 --- a/lib/jekyll/related_posts.rb +++ b/lib/jekyll/related_posts.rb @@ -28,13 +28,13 @@ module Jekyll def build_index self.class.lsi ||= begin lsi = Classifier::LSI.new(:auto_rebuild => false) - display("\n Populating LSI... ") + display("Populating LSI...") self.site.posts.each do |x| lsi.add_item(x) end - display("\nRebuilding index... ") + display("Rebuilding index...") lsi.build_index display("") lsi @@ -50,7 +50,8 @@ module Jekyll end def display(output) - $stdout.print(output) + $stdout.print("\n") + $stdout.print(Jekyll::Stevenson.formatted_topic(output)) $stdout.flush end end