class Jekyll::Renderer

Attributes

document[R]
layouts[W]
payload[W]
site[R]

Public Class Methods

new(site, document, site_payload = nil) click to toggle source
# File lib/jekyll/renderer.rb, line 8
def initialize(site, document, site_payload = nil)
  @site     = site
  @document = document
  @payload  = site_payload
end

Public Instance Methods

convert(content) click to toggle source

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

Returns String the converted content.

# File lib/jekyll/renderer.rb, line 98
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
converters() click to toggle source

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

Returns Array of Converter instances.

# File lib/jekyll/renderer.rb, line 37
def converters
  @converters ||= site.converters.select { |c| c.matches(document.extname) }.sort
end
invalid_layout?(layout) click to toggle source

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

# File lib/jekyll/renderer.rb, line 140
def invalid_layout?(layout)
  !document.data["layout"].nil? && layout.nil? && !(document.is_a? Jekyll::Excerpt)
end
layouts() click to toggle source

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.

# File lib/jekyll/renderer.rb, line 29
def layouts
  @layouts || site.layouts
end
output_ext() click to toggle source

Determine the extname the outputted file should have

Returns String the output extname including the leading period.

# File lib/jekyll/renderer.rb, line 44
def output_ext
  @output_ext ||= (permalink_ext || converter_output_ext)
end
payload() click to toggle source

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

# File lib/jekyll/renderer.rb, line 19
def payload
  @payload ||= site.site_payload
end
place_in_layouts(content, payload, info) click to toggle source

Render layouts and place document content inside.

Returns String rendered content

# File lib/jekyll/renderer.rb, line 147
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)

    if (layout = site.layouts[layout.data["layout"]])
      break if used.include?(layout)
      used << layout
    end
  end
  output
end
render_document() click to toggle source

Render the document.

Returns String rendered document output rubocop: disable AbcSize

# File lib/jekyll/renderer.rb, line 69
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) click to toggle source

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.

# File lib/jekyll/renderer.rb, line 120
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 RescueException
rescue Exception => e
  Jekyll.logger.error "Liquid Exception:",
    LiquidRenderer.format_error(e, path || document.relative_path)
  raise e
end
run() click to toggle source

Prepare payload and render the document

Returns String rendered document output

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

Private Instance Methods

add_regenerator_dependencies(layout) click to toggle source
# File lib/jekyll/renderer.rb, line 204
def add_regenerator_dependencies(layout)
  return unless document.write?
  site.regenerator.add_dependency(
    site.in_source_dir(document.path),
    layout.path
  )
end
assign_current_document!() click to toggle source

Set related posts to payload if document is a post.

Returns nothing

# File lib/jekyll/renderer.rb, line 227
def assign_current_document!
  payload["site"].current_document = document
end
assign_highlighter_options!() click to toggle source

Set highlighter prefix and suffix

Returns nothing

# File lib/jekyll/renderer.rb, line 235
def assign_highlighter_options!
  payload["highlighter_prefix"] = converters.first.highlighter_prefix
  payload["highlighter_suffix"] = converters.first.highlighter_suffix
end
assign_layout_data!() click to toggle source
# File lib/jekyll/renderer.rb, line 241
def assign_layout_data!
  layout = layouts[document.data["layout"]]
  if layout
    payload["layout"] = Utils.deep_merge_hashes(layout.data, payload["layout"] || {})
  end
end
assign_pages!() click to toggle source

Set page content to payload and assign pager if document has one.

Returns nothing

# File lib/jekyll/renderer.rb, line 216
def assign_pages!
  payload["page"] = document.to_liquid
  payload["paginator"] = if document.respond_to?(:pager)
                           document.pager.to_liquid
                         end
end
converter_output_ext() click to toggle source
# File lib/jekyll/renderer.rb, line 258
def converter_output_ext
  if output_exts.size == 1
    output_exts.last
  else
    output_exts[-2]
  end
end
liquid_options() click to toggle source
# File lib/jekyll/renderer.rb, line 274
def liquid_options
  @liquid_options ||= site.config["liquid"]
end
output_exts() click to toggle source
# File lib/jekyll/renderer.rb, line 267
def output_exts
  @output_exts ||= converters.map do |c|
    c.output_ext(document.extname)
  end.compact
end
render_layout(output, layout, info) click to toggle source

Render layout content into document.output

Returns String rendered content

# File lib/jekyll/renderer.rb, line 191
def render_layout(output, layout, info)
  payload["content"] = output
  payload["layout"]  = Utils.deep_merge_hashes(layout.data, payload["layout"] || {})

  render_liquid(
    layout.content,
    payload,
    info,
    layout.relative_path
  )
end
validate_layout(layout) click to toggle source

Checks if the layout specified in the document actually exists

layout - the layout to check Returns nothing

# File lib/jekyll/renderer.rb, line 174
def validate_layout(layout)
  if invalid_layout?(layout)
    Jekyll.logger.warn(
      "Build Warning:",
      "Layout '#{document.data["layout"]}' requested "\
      "in #{document.relative_path} does not exist."
    )
  elsif !layout.nil?
    layout_source = layout.path.start_with?(site.source) ? :site : :theme
    Jekyll.logger.debug "Layout source:", layout_source
  end
end