Class: Alula::FeedBuilder

Inherits:
Generator show all
Defined in:
lib/alula/generators/feedbuilder.rb

Instance Attribute Summary

Attributes inherited from Generator

#options, #site

Instance Method Summary collapse

Methods inherited from Generator

#generators, generators, #initialize, load, register, #substitutes

Constructor Details

This class inherits a constructor from Alula::Generator

Instance Method Details

#allow_compressing?Boolean

Returns:

  • (Boolean)


5
6
7
# File 'lib/alula/generators/feedbuilder.rb', line 5

def allow_compressing?
  :normal
end

#generateObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/alula/generators/feedbuilder.rb', line 9

def generate
  # Loop all languages and count posts per language
  @languages = {}
  (self.site.content.posts + self.site.content.pages).each do |post|
    post.languages.each do |lang|
      @languages[lang] ||= []
      @languages[lang] << post
    end
  end
  titles = Hash[@languages.collect {|lang, x| [lang, I18n.t("feedbuilder.recent_posts", locale: lang)]}]
  posts = Hash[
    @languages.collect do |lang, posts|
      [ lang, posts.slice(0, self.options.items) ]
    end
  ]
  
  @feed_page = Alula::Content::Page.new({
    generator: self,
    posts: posts,
    title: titles,
    name: self.options.name,
    slug: self.options.slug,
    sidebar: false,
    template: self.options.template,
    site: self.site,
    layout: "feed",
  },
  :previous => ->(hook, locale = nil) { nil },
  :next => ->(hook, locale = nil) { nil },
  :navigation => ->(hook, locale = nil) { nil },
  :write => ->(hook, locale = nil) {
    begin
      _old_renderer = self.posts.collect{|p| p..renderer}
      self.posts.cycle(1) { |p| p.flush; p..renderer = self.generator;}
      hook.call
    ensure
      self.posts.cycle(1) {|p| p..renderer = _old_renderer.shift }
    end
  },
  )
  self.site.content.pages << @feed_page
  
  # Add link to head
  Alula::Plugin.addon(:head, ->(context) {
    "<link rel=\"alternate\" type=\"application/rss+xml\" title=\"RSS\" href=\"#{context.url_for(@feed_page.url(context.locale))}\">"
  })
end