Class: Bunto::Renderer
- Inherits:
-
Object
- Object
- Bunto::Renderer
- Defined in:
- lib/bunto/renderer.rb
Instance Attribute Summary collapse
-
#document ⇒ Object
readonly
Returns the value of attribute document.
-
#layouts ⇒ Object
The list of layouts registered for this Renderer.
-
#payload ⇒ Object
Fetches the payload used in Liquid rendering.
-
#site ⇒ Object
readonly
Returns the value of attribute site.
Instance Method Summary collapse
-
#convert(content) ⇒ Object
Convert the given content using the converters which match this renderer's document.
-
#converters ⇒ Object
Determine which converters to use based on this document's extension.
-
#initialize(site, document, site_payload = nil) ⇒ Renderer
constructor
A new instance of Renderer.
-
#invalid_layout?(layout) ⇒ Boolean
Checks if the layout specified in the document actually exists.
-
#output_ext ⇒ Object
Determine the extname the outputted file should have.
-
#place_in_layouts(content, payload, info) ⇒ Object
Render layouts and place given content inside.
-
#render_liquid(content, payload, info, path = nil) ⇒ Object
Render the given content with the payload and info.
-
#run ⇒ Object
DAT RENDER THO.
Constructor Details
#initialize(site, document, site_payload = nil) ⇒ Renderer
Returns a new instance of Renderer.
8 9 10 11 12 |
# File 'lib/bunto/renderer.rb', line 8 def initialize(site, document, site_payload = nil) @site = site @document = document @payload = site_payload end |
Instance Attribute Details
#document ⇒ Object (readonly)
Returns the value of attribute document.
5 6 7 |
# File 'lib/bunto/renderer.rb', line 5 def document @document end |
#layouts ⇒ Object
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 => Bunto::Layout identified as basename without the extension name.
29 30 31 |
# File 'lib/bunto/renderer.rb', line 29 def layouts @layouts || site.layouts end |
#payload ⇒ Object
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 Bunto::Drops::UnifiedPayloadDrop
19 20 21 |
# File 'lib/bunto/renderer.rb', line 19 def payload @payload ||= site.site_payload end |
#site ⇒ Object (readonly)
Returns the value of attribute site.
5 6 7 |
# File 'lib/bunto/renderer.rb', line 5 def site @site 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.
106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/bunto/renderer.rb', line 106 def convert(content) converters.reduce(content) do |output, converter| begin converter.convert output rescue => e Bunto.logger.error "Conversion error:", "#{converter.class} encountered an error while "\ "converting '#{document.relative_path}':" Bunto.logger.error("", e.to_s) raise e end end end |
#converters ⇒ Object
Determine which converters to use based on this document's extension.
Returns an array of Converter instances.
37 38 39 |
# File 'lib/bunto/renderer.rb', line 37 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 true if the layout is invalid, false if otherwise
148 149 150 |
# File 'lib/bunto/renderer.rb', line 148 def invalid_layout?(layout) !document.data["layout"].nil? && layout.nil? && !(document.is_a? Bunto::Excerpt) end |
#output_ext ⇒ Object
Determine the extname the outputted file should have
Returns the output extname including the leading period.
44 45 46 |
# File 'lib/bunto/renderer.rb', line 44 def output_ext @output_ext ||= (permalink_ext || converter_output_ext) 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
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/bunto/renderer.rb', line 158 def place_in_layouts(content, payload, info) output = content.dup layout = layouts[document.data["layout"]] Bunto.logger.warn( "Build Warning:", "Layout '#{document.data["layout"]}' requested in "\ "#{document.relative_path} does not exist." ) if invalid_layout? layout used = Set.new([layout]) # Reset the payload layout data to ensure it starts fresh for each page. payload["layout"] = nil while layout payload["content"] = output payload["layout"] = Utils.deep_merge_hashes(layout.data, payload["layout"] || {}) output = render_liquid( layout.content, payload, info, layout.relative_path ) # 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 = layouts[layout.data["layout"]]) break if used.include?(layout) used << layout 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.
128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/bunto/renderer.rb', line 128 def render_liquid(content, payload, info, path = nil) template = site.liquid_renderer.file(path).parse(content) template.warnings.each do |e| Bunto.logger.warn "Liquid Warning:", LiquidRenderer.format_error(e, path || document.relative_path) end template.render!(payload, info) # rubocop: disable RescueException rescue Exception => e Bunto.logger.error "Liquid Exception:", LiquidRenderer.format_error(e, path || document.relative_path) raise e end |
#run ⇒ Object
DAT RENDER THO
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/bunto/renderer.rb', line 52 def run Bunto.logger.debug "Rendering:", document.relative_path payload["page"] = document.to_liquid if document.respond_to? :pager payload["paginator"] = document.pager.to_liquid end if document.is_a?(Document) && document.collection.label == "posts" payload["site"]["related_posts"] = document. else payload["site"]["related_posts"] = nil end # 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 Bunto.logger.debug "Pre-Render Hooks:", document.relative_path document.trigger_hooks(:pre_render, payload) info = { :registers => { :site => site, :page => payload["page"] } } output = document.content if document.render_with_liquid? Bunto.logger.debug "Rendering Liquid:", document.relative_path output = render_liquid(output, payload, info, document.path) end Bunto.logger.debug "Rendering Markup:", document.relative_path output = convert(output) document.content = output if document.place_in_layout? Bunto.logger.debug "Rendering Layout:", document.relative_path place_in_layouts( output, payload, info ) else output end end |