lib/bunto/convertible.rb in bunto-3.2.1 vs lib/bunto/convertible.rb in bunto-3.4.5

- old
+ new

@@ -33,10 +33,11 @@ # base - The String path to the dir containing the file. # name - The String filename of the file. # opts - optional parameter to File.read, default at site configs # # Returns nothing. + # rubocop:disable Metrics/AbcSize def read_yaml(base, name, opts = {}) filename = File.join(base, name) begin self.content = File.read(@path || site.in_source_dir(base, name), @@ -56,10 +57,11 @@ validate_data! filename validate_permalink! filename self.data end + # rubocop:enable Metrics/AbcSize def validate_data!(filename) unless self.data.is_a?(Hash) raise Errors::InvalidYAMLFrontMatterError, "Invalid YAML front matter in #{filename}" @@ -74,59 +76,38 @@ # Transform the contents based on the content type. # # Returns the transformed contents. def transform - converters.reduce(content) do |output, converter| - begin - converter.convert output - rescue => e - Bunto.logger.error( - "Conversion error:", - "#{converter.class} encountered an error while converting '#{path}':" - ) - Bunto.logger.error("", e.to_s) - raise e - end - end + _renderer.transform end # Determine the extension depending on content_type. # # Returns the String extension for the output file. # e.g. ".html" for an HTML output file. def output_ext - Bunto::Renderer.new(site, self).output_ext + _renderer.output_ext end # Determine which converter to use based on this convertible's # extension. # # Returns the Converter instance. def converters - @converters ||= site.converters.select { |c| c.matches(ext) }.sort + _renderer.converters end # Render Liquid in the content # # content - the raw Liquid content to render # payload - the payload for Liquid # info - the info for Liquid # # Returns the converted content def render_liquid(content, payload, info, path) - template = site.liquid_renderer.file(path).parse(content) - template.warnings.each do |e| - Bunto.logger.warn "Liquid Warning:", - LiquidRenderer.format_error(e, path || self.path) - end - template.render!(payload, info) - # rubocop: disable RescueException - rescue Exception => e - Bunto.logger.error "Liquid Exception:", - LiquidRenderer.format_error(e, path || self.path) - raise e + _renderer.render_liquid(content, payload, info, path) end # rubocop: enable RescueException # Convert this Convertible's data to a Hash suitable for use by Liquid. # @@ -209,79 +190,32 @@ # payload - the payload for Liquid # info - the info for Liquid # # Returns nothing def render_all_layouts(layouts, payload, info) - # recursively render layouts - layout = layouts[data["layout"]] - - Bunto.logger.warn( - "Build Warning:", - "Layout '#{data["layout"]}' requested in #{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 - Bunto.logger.debug "Rendering Layout:", path - payload["content"] = output - payload["layout"] = Utils.deep_merge_hashes(layout.data, payload["layout"] || {}) - - self.output = render_liquid(layout.content, - payload, - info, - layout.relative_path) - - # Add layout to dependency tree - site.regenerator.add_dependency( - site.in_source_dir(path), - site.in_source_dir(layout.path) - ) - - if (layout = layouts[layout.data["layout"]]) - break if used.include?(layout) - used << layout - end - end + _renderer.layouts = layouts + self.output = _renderer.place_in_layouts(output, payload, info) + ensure + @_renderer = nil # this will allow the modifications above to disappear end # Add any necessary layouts to this convertible document. # # payload - The site payload Drop or Hash. # layouts - A Hash of {"name" => "layout"}. # # Returns nothing. def do_layout(payload, layouts) - Bunto.logger.debug "Rendering:", self.relative_path + self.output = _renderer.tap do |renderer| + renderer.layouts = layouts + renderer.payload = payload + end.run - Bunto.logger.debug "Pre-Render Hooks:", self.relative_path - Bunto::Hooks.trigger hook_owner, :pre_render, self, payload - info = { - :filters => [Bunto::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 - - if render_with_liquid? - Bunto.logger.debug "Rendering Liquid:", self.relative_path - self.content = render_liquid(content, payload, info, path) - end - Bunto.logger.debug "Rendering Markup:", self.relative_path - self.content = transform - - # output keeps track of what will finally be written - self.output = content - - render_all_layouts(layouts, payload, info) if place_in_layout? Bunto.logger.debug "Post-Render Hooks:", self.relative_path Bunto::Hooks.trigger hook_owner, :post_render, self + ensure + @_renderer = nil # this will allow the modifications above to disappear end # Write the generated page file to the destination directory. # # dest - The String path to the destination dir. @@ -303,8 +237,13 @@ if self.class::ATTRIBUTES_FOR_LIQUID.include?(property) send(property) else data[property] end + end + + private + def _renderer + @_renderer ||= Bunto::Renderer.new(site, self) end end end