Permalink
Browse files

Move the building of related posts into their own class

  • Loading branch information...
mattr- committed May 8, 2013
1 parent 8bd14f1 commit f67f7f3db43e64c9fc174d3a8e6e6651cffe93f8
Showing with 62 additions and 25 deletions.
  1. +3 −23 lib/jekyll/post.rb
  2. +59 −0 lib/jekyll/related_posts.rb
  3. +0 −2 lib/jekyll/site.rb
View
@@ -1,3 +1,5 @@
require 'jekyll/related_posts'
module Jekyll
class Post
include Comparable
@@ -244,29 +246,7 @@ def id
#
# 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.
@@ -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
View
@@ -71,8 +71,6 @@ def reset
#
# 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}/

0 comments on commit f67f7f3

Please sign in to comment.