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
13
# File 'lib/jekyll/renderer.rb', line 8

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

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document


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

def document
  @document
end

#layoutsObject

The list of layouts registered for this Renderer. It can be written with #layouts=(new_layouts) Falls back to site.layouts if no layouts are registered.

Returns a Hash of String => Jekyll::Layout identified as basename without the extension name.


30
31
32
# File 'lib/jekyll/renderer.rb', line 30

def layouts
  @layouts || site.layouts
end

#payloadObject

Fetches the payload used in Liquid rendering. It can be written with #payload=(new_payload) Falls back to site.site_payload if no payload is set.

Returns a Jekyll::Drops::UnifiedPayloadDrop


20
21
22
# File 'lib/jekyll/renderer.rb', line 20

def payload
  @payload ||= site.site_payload
end

#siteObject (readonly)

Returns the value of attribute site


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

def site
  @site
end

Instance Method Details

#convert(content) ⇒ Object

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

Returns String the converted content.


99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/jekyll/renderer.rb', line 99

def convert(content)
  converters.reduce(content) do |output, converter|
    begin
      converter.convert output
    rescue StandardError => 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 Array of Converter instances.


38
39
40
# File 'lib/jekyll/renderer.rb', line 38

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

#invalid_layout?(layout) ⇒ Boolean

Checks if the layout specified in the document actually exists

layout - the layout to check

Returns Boolean true if the layout is invalid, false if otherwise

Returns:

  • (Boolean)

141
142
143
# File 'lib/jekyll/renderer.rb', line 141

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

#output_extObject

Determine the extname the outputted file should have

Returns String the output extname including the leading period.


45
46
47
# File 'lib/jekyll/renderer.rb', line 45

def output_ext
  @output_ext ||= (permalink_ext || converter_output_ext)
end

#place_in_layouts(content, payload, info) ⇒ Object

Render layouts and place document content inside.

Returns String rendered content


148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/jekyll/renderer.rb', line 148

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

  used = Set.new([layout])

  # Reset the payload layout data to ensure it starts fresh for each page.
  payload["layout"] = nil

  while layout
    output = render_layout(output, layout, info)
    add_regenerator_dependencies(layout)

    next unless (layout = site.layouts[layout.data["layout"]])
    break if used.include?(layout)

    used << layout
  end
  output
end

#render_documentObject

Render the document.

Returns String rendered document output rubocop: disable Metrics/AbcSize


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/jekyll/renderer.rb', line 70

def render_document
  info = {
    :registers        => { :site => site, :page => payload["page"] },
    :strict_filters   => liquid_options["strict_filters"],
    :strict_variables => liquid_options["strict_variables"],
  }

  output = document.content
  if document.render_with_liquid?
    Jekyll.logger.debug "Rendering Liquid:", document.relative_path
    output = render_liquid(output, payload, info, document.path)
  end

  Jekyll.logger.debug "Rendering Markup:", document.relative_path
  output = convert(output.to_s)
  document.content = output

  if document.place_in_layout?
    Jekyll.logger.debug "Rendering Layout:", document.relative_path
    output = place_in_layouts(output, payload, info)
  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 String the content, rendered by Liquid.


121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/jekyll/renderer.rb', line 121

def render_liquid(content, payload, info, path = nil)
  template = site.liquid_renderer.file(path).parse(content)
  template.warnings.each do |e|
    Jekyll.logger.warn "Liquid Warning:",
                       LiquidRenderer.format_error(e, path || document.relative_path)
  end
  template.render!(payload, info)
# rubocop: disable Lint/RescueException
rescue Exception => e
  Jekyll.logger.error "Liquid Exception:",
                      LiquidRenderer.format_error(e, path || document.relative_path)
  raise e
end

#runObject

Prepare payload and render the document

Returns String rendered document output


52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/jekyll/renderer.rb', line 52

def run
  Jekyll.logger.debug "Rendering:", document.relative_path

  assign_pages!
  assign_current_document!
  assign_highlighter_options!
  assign_layout_data!

  Jekyll.logger.debug "Pre-Render Hooks:", document.relative_path
  document.trigger_hooks(:pre_render, payload)

  render_document
end