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