Class: Jekyll::Renderer

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll/renderer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site, document, site_payload = nil) ⇒ Renderer

Returns a new instance of Renderer.


8
9
10
11
12
# File 'lib/jekyll/renderer.rb', line 8

def initialize(site, document, site_payload = nil)
  @site         = site
  @document     = document
  @site_payload = site_payload
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document


6
7
8
# File 'lib/jekyll/renderer.rb', line 6

def document
  @document
end

#siteObject (readonly)

Returns the value of attribute site


6
7
8
# File 'lib/jekyll/renderer.rb', line 6

def site
  @site
end

#site_payloadObject (readonly)

Returns the value of attribute site_payload


6
7
8
# File 'lib/jekyll/renderer.rb', line 6

def site_payload
  @site_payload
end

Instance Method Details

#convert(content) ⇒ Object

Convert the given content using the converters which match this renderer's document.

content - the raw, unconverted content

Returns the converted content.


72
73
74
75
76
77
78
79
80
81
82
# File 'lib/jekyll/renderer.rb', line 72

def convert(content)
  converters.reduce(content) do |output, converter|
    begin
      converter.convert output
    rescue => e
      Jekyll.logger.error "Conversion error:", "#{converter.class} encountered an error while converting '#{document.relative_path}':"
      Jekyll.logger.error("", e.to_s)
      raise e
    end
  end
end

#convertersObject

Determine which converters to use based on this document's extension.

Returns an array of Converter instances.


18
19
20
# File 'lib/jekyll/renderer.rb', line 18

def converters
  @converters ||= site.converters.select { |c| c.matches(document.extname) }
end

#invalid_layout?(layout) ⇒ Boolean

Checks if the layout specified in the document actually exists

layout - the layout to check

Returns true if the layout is invalid, false if otherwise

Returns:

  • (Boolean)

107
108
109
# File 'lib/jekyll/renderer.rb', line 107

def invalid_layout?(layout)
  !document.data["layout"].nil? && layout.nil?
end

#output_extObject

Determine the extname the outputted file should have

Returns the output extname including the leading period.


25
26
27
# File 'lib/jekyll/renderer.rb', line 25

def output_ext
  converters.first.output_ext(document.extname)
end

#place_in_layouts(content, payload, info) ⇒ Object

Render layouts and place given content inside.

content - the content to be placed in the layout

Returns the content placed in the Liquid-rendered layouts


117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/jekyll/renderer.rb', line 117

def place_in_layouts(content, payload, info)
  output = content.dup
  layout = site.layouts[document.data["layout"]]

  Jekyll.logger.warn("Build Warning:", "Layout '#{document.data["layout"]}' requested in #{document.relative_path} does not exist.") if invalid_layout? layout

  used   = Set.new([layout])

  while layout
    payload = Utils.deep_merge_hashes(
      payload,
      {
        "content" => output,
        "page"    => document.to_liquid,
        "layout"  => layout.data
      }
    )

    output = render_liquid(
      layout.content,
      payload,
      info,
      File.join(site.config['layouts'], layout.name)
    )

    # Add layout to dependency tree
    site.regenerator.add_dependency(
      site.in_source_dir(document.path),
      site.in_source_dir(layout.path)
    ) if document.write?

    if layout = site.layouts[layout.data["layout"]]
      if used.include?(layout)
        layout = nil # avoid recursive chain
      else
        used << layout
      end
    end
  end

  output
end

#render_liquid(content, payload, info, path = nil) ⇒ Object

Render the given content with the payload and info

content - payload - info - path - (optional) the path to the file, for use in ex

Returns the content, rendered by Liquid.


92
93
94
95
96
97
98
99
100
# File 'lib/jekyll/renderer.rb', line 92

def render_liquid(content, payload, info, path = nil)
  Liquid::Template.parse(content).render!(payload, info)
rescue Tags::IncludeTagError => e
  Jekyll.logger.error "Liquid Exception:", "#{e.message} in #{e.path}, included in #{path || document.relative_path}"
  raise e
rescue Exception => e
  Jekyll.logger.error "Liquid Exception:", "#{e.message} in #{path || document.relative_path}"
  raise e
end

#runObject

DAT RENDER THO


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/jekyll/renderer.rb', line 33

def run
  payload = Utils.deep_merge_hashes({
    "page" => document.to_liquid
  }, site_payload || site.site_payload)

  info = {
    filters:   [Jekyll::Filters],
    registers: { :site => site, :page => payload['page'] }
  }

  # render and transform content (this becomes the final content of the object)
  payload["highlighter_prefix"] = converters.first.highlighter_prefix
  payload["highlighter_suffix"] = converters.first.highlighter_suffix

  output = document.content

  if document.render_with_liquid?
    output = render_liquid(output, payload, info)
  end

  output = convert(output)
  document.content = output

  if document.place_in_layout?
    place_in_layouts(
      output,
      payload,
      info
    )
  else
    output
  end
end