Class: Jekyll::GenerateJSON

Inherits:
Generator
  • Object
show all
Defined in:
lib/jekyll_react/json-generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#siteObject (readonly)

Returns the value of attribute site.



17
18
19
# File 'lib/jekyll_react/json-generator.rb', line 17

def site
  @site
end

Instance Method Details

#dest_dirObject



56
57
58
# File 'lib/jekyll_react/json-generator.rb', line 56

def dest_dir
  File.join('api', 'v1')
end

#generate(site) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/jekyll_react/json-generator.rb', line 21

def generate(site)
  @site = site
  config = site.config['react']
  config_json = config.to_json
  new_page('config.json', config_json, self.dest_dir)
  parse_pages
  parse_data
end

#new_page(name, data, dir) ⇒ Object



50
51
52
53
54
# File 'lib/jekyll_react/json-generator.rb', line 50

def new_page(name, data, dir)
  page = PageWithoutAFile.new(@site, File.dirname(__FILE__), dir, name.to_s)
  page.content = data
  @site.pages << page
end

#parse_dataObject



44
45
46
47
48
# File 'lib/jekyll_react/json-generator.rb', line 44

def parse_data
  @site.data.each do |site_file|
    new_page("#{site_file[0]}.json", site_file.to_json, self.dest_dir)
  end
end

#parse_pagesObject



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/jekyll_react/json-generator.rb', line 30

def parse_pages
  @site.pages.each do |page|
    if page.data['render'] && page.data['render'] == 'dynamic'
      rendered_content = JekyllReact::Render.new(self.site, page, site_payload = nil)
      page.content=rendered_content.run
      dyn_page = JekyllReact::PageData.new(@site, page)
      name = "#{page.data['title']}.json"
      page_json = dyn_page.to_hash.to_json
      new_page(name, page_json, page.url)
      page.content = ''
    end
  end
end