Skip to content

Commit

Permalink
Move the building of related posts into their own class
Browse files Browse the repository at this point in the history
  • Loading branch information
mattr- committed May 8, 2013
1 parent 8bd14f1 commit f67f7f3
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 25 deletions.
26 changes: 3 additions & 23 deletions lib/jekyll/post.rb
@@ -1,3 +1,5 @@
require 'jekyll/related_posts'

module Jekyll
class Post
include Comparable
Expand Down Expand Up @@ -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.
Expand Down
59 changes: 59 additions & 0 deletions 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
2 changes: 0 additions & 2 deletions lib/jekyll/site.rb
Expand Up @@ -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}/
Expand Down

0 comments on commit f67f7f3

Please sign in to comment.