Class: Amadeusz::Jekyll::RelatedPosts

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/jekyll-related-posts.rb

Instance Method Summary collapse

Constructor Details

#initializeRelatedPosts

Returns a new instance of RelatedPosts.



18
19
20
21
22
23
# File 'lib/jekyll-related-posts.rb', line 18

def initialize
  @posts = Array.new
  @keywords = Array.new
  @tokenizer = Tokenizer::WhitespaceTokenizer.new(:en)
  @stopwords_filter = Stopwords::Snowball::Filter.new('en')
end

Instance Method Details

#add_post(post) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/jekyll-related-posts.rb', line 25

def add_post(post)
  post = {
    url: post.url,
    title: post.data['title'].dup,
    content: (stem(post.content) + stem(post.data['title']))
  }

  @posts << post
  @keywords += post[:content]
  @keywords.uniq!
end

#build!(site) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/jekyll-related-posts.rb', line 37

def build!(site)
  conf = config(site)
  @weights = keywords_weights(conf['weights'])
  related = find_releated(conf['max_count'], conf['min_score'], conf['accuracy'])
  template = Liquid::Template.parse(File.read(template_path(site)))

  @posts.each do |post|
    filename = File.join(site.config['destination'], post[:url])
    filename = File.join(filename, 'index.html') if File.directory? filename
    rendered = File.read(filename)

    output = template.render('related_posts' => related[post])

    rendered.gsub! '<related-posts />', output
    File.write(filename, rendered)
  end
end