From b781b9967de4386440c2f09f2e670ad34d329f4e Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Tue, 30 Aug 2016 14:04:57 -0700 Subject: [PATCH 01/10] Proxy Convertible#render_all_layouts to Renderer.place_in_layouts --- .rubocop.yml | 1 - lib/jekyll/convertible.rb | 42 +++++++-------------------------------- 2 files changed, 7 insertions(+), 36 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 8ae01fb1..787fbdbe 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -4,7 +4,6 @@ AllCops: Include: - lib/**/*.rb Exclude: - - lib/jekyll/convertible.rb - lib/jekyll/renderer.rb - bin/**/* - benchmark/**/* diff --git a/lib/jekyll/convertible.rb b/lib/jekyll/convertible.rb index ca613f78..954802a1 100644 --- a/lib/jekyll/convertible.rb +++ b/lib/jekyll/convertible.rb @@ -95,7 +95,7 @@ module Jekyll # Returns the String extension for the output file. # e.g. ".html" for an HTML output file. def output_ext - Jekyll::Renderer.new(site, self).output_ext + _renderer.output_ext end # Determine which converter to use based on this convertible's @@ -211,40 +211,7 @@ module Jekyll # # Returns nothing def render_all_layouts(layouts, payload, info) - # recursively render layouts - layout = layouts[data["layout"]] - - Jekyll.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 - Jekyll.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.place_in_layouts(output, payload, info) end # Add any necessary layouts to this convertible document. @@ -306,5 +273,10 @@ module Jekyll data[property] end end + + private + def _renderer + @_renderer ||= Jekyll::Renderer.new(site, self) + end end end From 741102bd40db3d12b2bb9221a37aefd3d6beb744 Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Tue, 30 Aug 2016 14:13:26 -0700 Subject: [PATCH 02/10] Proxy Convertible#converters to Renderer --- lib/jekyll/convertible.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/jekyll/convertible.rb b/lib/jekyll/convertible.rb index 954802a1..db187dc9 100644 --- a/lib/jekyll/convertible.rb +++ b/lib/jekyll/convertible.rb @@ -103,7 +103,7 @@ module Jekyll # # Returns the Converter instance. def converters - @converters ||= site.converters.select { |c| c.matches(ext) }.sort + _renderer.converters end # Render Liquid in the content From d0f57b61d4e5e4ff1a6514578bf23f4ef0628296 Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Tue, 30 Aug 2016 14:14:03 -0700 Subject: [PATCH 03/10] Proxy Convertible#transform to Renderer --- lib/jekyll/convertible.rb | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/lib/jekyll/convertible.rb b/lib/jekyll/convertible.rb index db187dc9..8e87eec9 100644 --- a/lib/jekyll/convertible.rb +++ b/lib/jekyll/convertible.rb @@ -76,18 +76,7 @@ module Jekyll # # Returns the transformed contents. def transform - converters.reduce(content) do |output, converter| - begin - converter.convert output - rescue => e - Jekyll.logger.error( - "Conversion error:", - "#{converter.class} encountered an error while converting '#{path}':" - ) - Jekyll.logger.error("", e.to_s) - raise e - end - end + _renderer.transform end # Determine the extension depending on content_type. From 8496e2e8b6b79e6804f1c1daf90ea790bec81751 Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Tue, 30 Aug 2016 14:14:12 -0700 Subject: [PATCH 04/10] Proxy Convertible#render_liquid to Renderer --- lib/jekyll/convertible.rb | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/lib/jekyll/convertible.rb b/lib/jekyll/convertible.rb index 8e87eec9..c0c97e33 100644 --- a/lib/jekyll/convertible.rb +++ b/lib/jekyll/convertible.rb @@ -103,17 +103,7 @@ module Jekyll # # Returns the converted content def render_liquid(content, payload, info, path) - template = site.liquid_renderer.file(path).parse(content) - template.warnings.each do |e| - Jekyll.logger.warn "Liquid Warning:", - LiquidRenderer.format_error(e, path || self.path) - end - template.render!(payload, info) - # rubocop: disable RescueException - rescue Exception => e - Jekyll.logger.error "Liquid Exception:", - LiquidRenderer.format_error(e, path || self.path) - raise e + _renderer.render_liquid(content, payload, info, path) end # rubocop: enable RescueException From 73e79f0049b642dd1246aebdf7e4696682ec56c2 Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Tue, 30 Aug 2016 14:14:22 -0700 Subject: [PATCH 05/10] Proxy Convertible#converters to Renderer#run --- lib/jekyll/convertible.rb | 25 ++----------------------- 1 file changed, 2 insertions(+), 23 deletions(-) diff --git a/lib/jekyll/convertible.rb b/lib/jekyll/convertible.rb index c0c97e33..9922f779 100644 --- a/lib/jekyll/convertible.rb +++ b/lib/jekyll/convertible.rb @@ -200,30 +200,9 @@ module Jekyll # # Returns nothing. def do_layout(payload, layouts) - Jekyll.logger.debug "Rendering:", self.relative_path + @_renderer = Jekyll::Renderer.new(site, self, payload) + _renderer.run - Jekyll.logger.debug "Pre-Render Hooks:", self.relative_path - Jekyll::Hooks.trigger hook_owner, :pre_render, self, payload - info = { - :filters => [Jekyll::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? - Jekyll.logger.debug "Rendering Liquid:", self.relative_path - self.content = render_liquid(content, payload, info, path) - end - Jekyll.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? Jekyll.logger.debug "Post-Render Hooks:", self.relative_path Jekyll::Hooks.trigger hook_owner, :post_render, self end From 8014c54ee0bafbaede8196c15741101f65197b52 Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Tue, 30 Aug 2016 14:14:33 -0700 Subject: [PATCH 06/10] Convertible#converters: sort the converters --- lib/jekyll/renderer.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/jekyll/renderer.rb b/lib/jekyll/renderer.rb index 083a2a6b..44f3fb5e 100644 --- a/lib/jekyll/renderer.rb +++ b/lib/jekyll/renderer.rb @@ -15,7 +15,7 @@ module Jekyll # # Returns an array of Converter instances. def converters - @converters ||= site.converters.select { |c| c.matches(document.extname) } + @converters ||= site.converters.select { |c| c.matches(document.extname) }.sort end # Determine the extname the outputted file should have From ac6bbc190690fcb4c4ec4e9b821ac8fd922cbd88 Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Tue, 30 Aug 2016 14:14:52 -0700 Subject: [PATCH 07/10] Renderer#invalid_layout? should check if document is an excerpt --- lib/jekyll/renderer.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/jekyll/renderer.rb b/lib/jekyll/renderer.rb index 44f3fb5e..b1177b60 100644 --- a/lib/jekyll/renderer.rb +++ b/lib/jekyll/renderer.rb @@ -126,7 +126,7 @@ module Jekyll # # Returns true if the layout is invalid, false if otherwise def invalid_layout?(layout) - !document.data["layout"].nil? && layout.nil? + !document.data["layout"].nil? && layout.nil? && !(document.is_a? Jekyll::Excerpt) end # Render layouts and place given content inside. From 9ddc12bad8773f022456be2bd72107ade97c06c2 Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Tue, 30 Aug 2016 14:39:20 -0700 Subject: [PATCH 08/10] Convertible: ensure layouts the argument and payload the argument are set properly in the renderer --- lib/jekyll/convertible.rb | 12 ++++++++++-- lib/jekyll/renderer.rb | 31 ++++++++++++++++++++++++++----- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/lib/jekyll/convertible.rb b/lib/jekyll/convertible.rb index 9922f779..52fc49b4 100644 --- a/lib/jekyll/convertible.rb +++ b/lib/jekyll/convertible.rb @@ -190,7 +190,10 @@ module Jekyll # # Returns nothing def render_all_layouts(layouts, payload, info) + _renderer.layouts = layouts _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. @@ -200,11 +203,16 @@ module Jekyll # # Returns nothing. def do_layout(payload, layouts) - @_renderer = Jekyll::Renderer.new(site, self, payload) - _renderer.run + _renderer.tap do |renderer| + renderer.layouts = layouts + renderer.payload = payload + renderer.run + end Jekyll.logger.debug "Post-Render Hooks:", self.relative_path Jekyll::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. diff --git a/lib/jekyll/renderer.rb b/lib/jekyll/renderer.rb index b1177b60..db01a167 100644 --- a/lib/jekyll/renderer.rb +++ b/lib/jekyll/renderer.rb @@ -2,12 +2,33 @@ module Jekyll class Renderer - attr_reader :document, :site, :payload + attr_reader :document, :site + attr_writer :layouts, :payload - def initialize(site, document, site_payload = nil) + def initialize(site, document, site_payload = nil, layouts: nil) @site = site @document = document - @payload = site_payload || site.site_payload + @payload = site_payload + @layouts = layouts + end + + # 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 + def payload + @payload ||= site.site_payload + end + + # 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. + def layouts + @layouts || site.layouts end # Determine which converters to use based on this document's @@ -137,7 +158,7 @@ module Jekyll # Returns the content placed in the Liquid-rendered layouts def place_in_layouts(content, payload, info) output = content.dup - layout = site.layouts[document.data["layout"]] + layout = layouts[document.data["layout"]] Jekyll.logger.warn( "Build Warning:", @@ -167,7 +188,7 @@ module Jekyll site.in_source_dir(layout.path) ) if document.write? - if (layout = site.layouts[layout.data["layout"]]) + if (layout = layouts[layout.data["layout"]]) break if used.include?(layout) used << layout end From a78426f20db370bc0ee7f0bba681b3d91cd2ca2e Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Tue, 30 Aug 2016 14:39:49 -0700 Subject: [PATCH 09/10] Convertible#read_yaml: disable Metrics/AbcSize check --- lib/jekyll/convertible.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/jekyll/convertible.rb b/lib/jekyll/convertible.rb index 52fc49b4..65052b68 100644 --- a/lib/jekyll/convertible.rb +++ b/lib/jekyll/convertible.rb @@ -35,6 +35,7 @@ module Jekyll # 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) @@ -58,6 +59,7 @@ module Jekyll self.data end + # rubocop:enable Metrics/AbcSize def validate_data!(filename) unless self.data.is_a?(Hash) From 4af0f0262ec119fd2a6049b236bba97314b2976e Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Thu, 1 Sep 2016 13:26:14 -0700 Subject: [PATCH 10/10] Remove layouts named param from Renderer#initialize --- lib/jekyll/renderer.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/jekyll/renderer.rb b/lib/jekyll/renderer.rb index db01a167..749882bf 100644 --- a/lib/jekyll/renderer.rb +++ b/lib/jekyll/renderer.rb @@ -5,11 +5,10 @@ module Jekyll attr_reader :document, :site attr_writer :layouts, :payload - def initialize(site, document, site_payload = nil, layouts: nil) + def initialize(site, document, site_payload = nil) @site = site @document = document @payload = site_payload - @layouts = layouts end # Fetches the payload used in Liquid rendering.