Class: Jekyll::RelatedPosts

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll/related_posts.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(post) ⇒ RelatedPosts



10
11
12
13
14
# File 'lib/jekyll/related_posts.rb', line 10

def initialize(post)
  @post = post
  @site = post.site
  require 'classifier-reborn' if site.lsi
end

Class Attribute Details

.lsiObject

Returns the value of attribute lsi



5
6
7
# File 'lib/jekyll/related_posts.rb', line 5

def lsi
  @lsi
end

Instance Attribute Details

#postObject (readonly)

Returns the value of attribute post



8
9
10
# File 'lib/jekyll/related_posts.rb', line 8

def post
  @post
end

#siteObject (readonly)

Returns the value of attribute site



8
9
10
# File 'lib/jekyll/related_posts.rb', line 8

def site
  @site
end

Instance Method Details

#buildObject



16
17
18
19
20
21
22
23
24
25
# File 'lib/jekyll/related_posts.rb', line 16

def build
  return [] unless site.posts.size > 1

  if site.lsi
    build_index
    lsi_related_posts
  else
    most_recent_posts
  end
end

#build_indexObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/jekyll/related_posts.rb', line 28

def build_index
  self.class.lsi ||= begin
    lsi = ClassifierReborn::LSI.new(:auto_rebuild => false)
    display("Populating LSI...")

    site.posts.each do |x|
      lsi.add_item(x)
    end

    display("Rebuilding index...")
    lsi.build_index
    display("")
    lsi
  end
end

#display(output) ⇒ Object



52
53
54
55
56
# File 'lib/jekyll/related_posts.rb', line 52

def display(output)
  $stdout.print("\n")
  $stdout.print(Jekyll.logger.formatted_topic(output))
  $stdout.flush
end


44
45
46
# File 'lib/jekyll/related_posts.rb', line 44

def lsi_related_posts
  self.class.lsi.find_related(post.content, 11) - [post]
end

#most_recent_postsObject



48
49
50
# File 'lib/jekyll/related_posts.rb', line 48

def most_recent_posts
  @most_recent_posts ||= (site.posts.reverse - [post]).first(10)
end