Merge pull request #1057 from mojombo/refactor-related-posts

Move the building of related posts into their own class
This commit is contained in:
Parker Moore 2013-05-15 07:25:53 -07:00
commit ef48cbefec
5 changed files with 101 additions and 25 deletions

View File

@ -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'

View File

@ -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.

View File

@ -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

View File

@ -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}/

View File

@ -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