Move the building of related posts into their own class
This commit is contained in:
parent
8bd14f18f6
commit
f67f7f3db4
|
@ -1,3 +1,5 @@
|
||||||
|
require 'jekyll/related_posts'
|
||||||
|
|
||||||
module Jekyll
|
module Jekyll
|
||||||
class Post
|
class Post
|
||||||
include Comparable
|
include Comparable
|
||||||
|
@ -244,29 +246,7 @@ module Jekyll
|
||||||
#
|
#
|
||||||
# Returns an Array of related Posts.
|
# Returns an Array of related Posts.
|
||||||
def related_posts(posts)
|
def related_posts(posts)
|
||||||
return [] unless posts.size > 1
|
Jekyll::RelatedPosts.new(self).build
|
||||||
|
|
||||||
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
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Add any necessary layouts to this post.
|
# 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
|
|
@ -71,8 +71,6 @@ module Jekyll
|
||||||
#
|
#
|
||||||
# Returns nothing.
|
# Returns nothing.
|
||||||
def setup
|
def setup
|
||||||
require 'classifier' if self.lsi
|
|
||||||
|
|
||||||
# Check that the destination dir isn't the source dir or a directory
|
# Check that the destination dir isn't the source dir or a directory
|
||||||
# parent to the source dir.
|
# parent to the source dir.
|
||||||
if self.source =~ /^#{self.dest}/
|
if self.source =~ /^#{self.dest}/
|
||||||
|
|
Loading…
Reference in New Issue