Class: Alula::FeedJSON

Inherits:
Generator show all
Defined in:
lib/alula/generators/feedjson.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)


6
7
8
# File 'lib/alula/generators/feedjson.rb', line 6

def allow_compressing?
  :normal
end

#generateObject



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
56
# File 'lib/alula/generators/feedjson.rb', line 10

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("feedjson.recent_posts", locale: lang)]}]
  posts = Hash[
    @languages.collect do |lang, posts|
      [ lang, posts.slice(0, self.options.items) ]
    end
  ]
  
  @feedjson = 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.json",
  },
  :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 << @feedjson
  
  # Add link to head
  Alula::Plugin.addon(:head, ->(context) {
    "<link rel=\"alternate\" type=\"application/json\" title=\"FeedJSON\" href=\"#{context.url_for(@feedjson.url(context.locale))}\">"
  })
end