class Bridgetown::Renderer

This class handles the output rendering and layout placement of pages and documents. For rendering of resources in particular, see Bridgetown::Resource::Transformer

Attributes

document[R]
site[R]

Public Class Methods

new(site, document) click to toggle source
# File lib/bridgetown-core/renderer.rb, line 9
def initialize(site, document)
  @site     = site
  @document = document
end

Public Instance Methods

convert(content, document) click to toggle source

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

Returns String the converted content.

# File lib/bridgetown-core/renderer.rb, line 74
def convert(content, document)
  converters.reduce(content) do |output, converter|
    if converter.method(:convert).arity == 1
      converter.convert output
    else
      converter.convert output, document
    end
  rescue StandardError => e
    Bridgetown.logger.error "Conversion error:",
                            "#{converter.class} encountered an error while "\
                            "converting `#{document.relative_path}'"
    raise e
  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/bridgetown-core/renderer.rb, line 18
def converters
  @converters ||= site.converters.select do |converter|
    if converter.method(:matches).arity == 1
      converter.matches(document.extname)
    else
      converter.matches(document.extname, document)
    end
  end.sort
end
execute_inline_ruby!() click to toggle source
# File lib/bridgetown-core/renderer.rb, line 65
def execute_inline_ruby!
  return unless site.config.should_execute_inline_ruby?

  Bridgetown::Utils::RubyExec.search_data_for_ruby_code(document, self)
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/bridgetown-core/renderer.rb, line 31
def output_ext
  @output_ext ||= (permalink_ext || converter_output_ext)
end
place_in_layouts(content) click to toggle source

Render layouts and place document content inside.

Returns String rendered content

# File lib/bridgetown-core/renderer.rb, line 92
def place_in_layouts(content)
  output = content.dup
  layout = site.layouts[document.data["layout"]]
  validate_layout(layout)

  used = Set.new([layout])

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

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

    used << layout
  end
  output
end
render_document() click to toggle source

Render the document.

Returns String rendered document output

# File lib/bridgetown-core/renderer.rb, line 49
def render_document
  execute_inline_ruby!

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

  if document.place_in_layout?
    Bridgetown.logger.debug "Rendering Layout:", document.relative_path
    output = place_in_layouts(output)
  end

  output
end
run() click to toggle source

Run hooks and render the document

Returns nothing

# File lib/bridgetown-core/renderer.rb, line 38
def run
  Bridgetown.logger.debug "Rendering:", document.relative_path

  document.trigger_hooks :pre_render
  document.output = render_document
  document.trigger_hooks :post_render
end

Private Instance Methods

add_regenerator_dependencies(layout) click to toggle source
# File lib/bridgetown-core/renderer.rb, line 147
def add_regenerator_dependencies(layout)
  return unless document.write?

  site.regenerator.add_dependency(
    site.in_source_dir(document.path),
    layout.path
  )
end
converter_output_ext() click to toggle source
# File lib/bridgetown-core/renderer.rb, line 165
def converter_output_ext
  if output_exts.size == 1
    output_exts.last
  else
    output_exts[-2]
  end
end
output_exts() click to toggle source
# File lib/bridgetown-core/renderer.rb, line 173
def output_exts
  @output_exts ||= converters.map do |c|
    c.output_ext(document.extname)
  end.compact
end
render_layout(output, layout) click to toggle source

Render layout content into document.output

Returns String rendered content

# File lib/bridgetown-core/renderer.rb, line 129
def render_layout(output, layout)
  layout_converters = site.matched_converters_for_convertible(layout)

  layout_content = layout.content.dup
  layout_converters.reduce(layout_content) do |layout_output, converter|
    next(layout_output) unless converter.method(:convert).arity == 2

    layout.current_document = document
    layout.current_document_output = output
    converter.convert layout_output, layout
  rescue StandardError => e
    Bridgetown.logger.error "Conversion error:",
                            "#{converter.class} encountered an error while "\
                            "converting `#{document.relative_path}'"
    raise e
  end
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/bridgetown-core/renderer.rb, line 117
def validate_layout(layout)
  return unless document.data["layout"].present? &&
    layout.nil? &&
    !(document.is_a? Bridgetown::Excerpt)

  Bridgetown.logger.warn "Build Warning:", "Layout '#{document.data["layout"]}' requested " \
    "in #{document.relative_path} does not exist."
end