Class: Jekyll::LayoutsGenerator

Inherits:
Generator
  • Object
show all
Defined in:
lib/jekyll-layouts.rb

Constant Summary collapse

DEFAULT_CONVERTER =
"Default"

Instance Method Summary collapse

Constructor Details

#initialize(config = Hash.new) ⇒ LayoutsGenerator

Returns a new instance of LayoutsGenerator.



12
13
14
# File 'lib/jekyll-layouts.rb', line 12

def initialize(config = Hash.new)
  @static = config_static(config['jekyll-layouts'])
end

Instance Method Details

#config_static(configs) ⇒ Object



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

def config_static(configs)
  case configs
  when nil, NilClass
    false
  when String
    configs == "static"
  when Hash
    configs.fetch('static', false)
  else
    raise ArgumentError.new("Your jekyll-layouts config has to either be a string or a hash! It's a #{configs.class} right now.")
  end
end

#generate(site) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/jekyll-layouts.rb', line 29

def generate(site)
  @site = site
  site.docs_to_write.each { |doc| process_options doc }
  site.posts.each { |post| process_options post }
  site.pages.each { |page| process_options page }

  site.docs_to_write.each { |doc| render_layouts doc }
  site.posts.each { |post| render_layouts post }
  site.pages.each { |page| render_layouts page }
end

#process_options(page) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/jekyll-layouts.rb', line 40

def process_options(page)
  return unless page.data.has_key? "layouts"
  # Make sure we are using a clone of the hash to avoid overwriting defaults
  page.data["layouts"] = page.data["layouts"].clone
  page.data["layouts"].each do |extension, options|
    case options
    when nil
      options = { "converter" => "", "suffix" => ".#{extension}" }
    when String
      options = { "layout" => options, "converter" => DEFAULT_CONVERTER, "suffix" => ".#{extension}" }
    end

    if options.is_a? Hash
      options = options.clone
      options['converter'] ||= DEFAULT_CONVERTER
      options['suffix'] ||= ".#{extension}"
      options['url'] ||= page.destination(@site.dest, "index#{options['suffix']}")[@site.dest.length..-1]
    end
    page.data["layouts"][extension] = options
  end
end

#render_layouts(page) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/jekyll-layouts.rb', line 62

def render_layouts(page)
  (page.data['layouts'] || []).each do |extension, options|
    pagename = "index#{options['suffix']}"
    case options['converter']
    when DEFAULT_CONVERTER
      render_page(@site, page, options["layout"])
      write_page(page, @site.dest, pagename)
      write_page(page, @site.source, pagename) if @static
    end
  end
end

#render_page(site, page, layout) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/jekyll-layouts.rb', line 78

def render_page(site, page, layout)
  original_layout = page.data['layout']
  page.data['layout'] = layout
  case page
  when Jekyll::Document
    page.output = Jekyll::Renderer.new(site, page, site.site_payload).run
  when Jekyll::Post, Jekyll::Page
    page.render(site.layouts, site.site_payload)
  end
ensure
  page.data['layout'] = original_layout
end

#write_page(page, dest, pagename) ⇒ Object



74
75
76
# File 'lib/jekyll-layouts.rb', line 74

def write_page(page, dest, pagename)
  page.write(dest, pagename)
end