From f67f7f3db43e64c9fc174d3a8e6e6651cffe93f8 Mon Sep 17 00:00:00 2001 From: Matt Rogers Date: Tue, 7 May 2013 22:27:38 -0500 Subject: [PATCH] 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}/