Merge pull request #5308 from jekyll/rubocop-convertible
Merge pull request 5308
This commit is contained in:
commit
2f167aeea7
|
@ -4,7 +4,6 @@ AllCops:
|
||||||
Include:
|
Include:
|
||||||
- lib/**/*.rb
|
- lib/**/*.rb
|
||||||
Exclude:
|
Exclude:
|
||||||
- lib/jekyll/convertible.rb
|
|
||||||
- lib/jekyll/renderer.rb
|
- lib/jekyll/renderer.rb
|
||||||
- bin/**/*
|
- bin/**/*
|
||||||
- benchmark/**/*
|
- benchmark/**/*
|
||||||
|
|
|
@ -35,6 +35,7 @@ module Jekyll
|
||||||
# opts - optional parameter to File.read, default at site configs
|
# opts - optional parameter to File.read, default at site configs
|
||||||
#
|
#
|
||||||
# Returns nothing.
|
# Returns nothing.
|
||||||
|
# rubocop:disable Metrics/AbcSize
|
||||||
def read_yaml(base, name, opts = {})
|
def read_yaml(base, name, opts = {})
|
||||||
filename = File.join(base, name)
|
filename = File.join(base, name)
|
||||||
|
|
||||||
|
@ -58,6 +59,7 @@ module Jekyll
|
||||||
|
|
||||||
self.data
|
self.data
|
||||||
end
|
end
|
||||||
|
# rubocop:enable Metrics/AbcSize
|
||||||
|
|
||||||
def validate_data!(filename)
|
def validate_data!(filename)
|
||||||
unless self.data.is_a?(Hash)
|
unless self.data.is_a?(Hash)
|
||||||
|
@ -76,18 +78,7 @@ module Jekyll
|
||||||
#
|
#
|
||||||
# Returns the transformed contents.
|
# Returns the transformed contents.
|
||||||
def transform
|
def transform
|
||||||
converters.reduce(content) do |output, converter|
|
_renderer.transform
|
||||||
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
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Determine the extension depending on content_type.
|
# Determine the extension depending on content_type.
|
||||||
|
@ -95,7 +86,7 @@ module Jekyll
|
||||||
# Returns the String extension for the output file.
|
# Returns the String extension for the output file.
|
||||||
# e.g. ".html" for an HTML output file.
|
# e.g. ".html" for an HTML output file.
|
||||||
def output_ext
|
def output_ext
|
||||||
Jekyll::Renderer.new(site, self).output_ext
|
_renderer.output_ext
|
||||||
end
|
end
|
||||||
|
|
||||||
# Determine which converter to use based on this convertible's
|
# Determine which converter to use based on this convertible's
|
||||||
|
@ -103,7 +94,7 @@ module Jekyll
|
||||||
#
|
#
|
||||||
# Returns the Converter instance.
|
# Returns the Converter instance.
|
||||||
def converters
|
def converters
|
||||||
@converters ||= site.converters.select { |c| c.matches(ext) }.sort
|
_renderer.converters
|
||||||
end
|
end
|
||||||
|
|
||||||
# Render Liquid in the content
|
# Render Liquid in the content
|
||||||
|
@ -114,17 +105,7 @@ module Jekyll
|
||||||
#
|
#
|
||||||
# Returns the converted content
|
# Returns the converted content
|
||||||
def render_liquid(content, payload, info, path)
|
def render_liquid(content, payload, info, path)
|
||||||
template = site.liquid_renderer.file(path).parse(content)
|
_renderer.render_liquid(content, payload, info, path)
|
||||||
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
|
|
||||||
end
|
end
|
||||||
# rubocop: enable RescueException
|
# rubocop: enable RescueException
|
||||||
|
|
||||||
|
@ -211,40 +192,10 @@ module Jekyll
|
||||||
#
|
#
|
||||||
# Returns nothing
|
# Returns nothing
|
||||||
def render_all_layouts(layouts, payload, info)
|
def render_all_layouts(layouts, payload, info)
|
||||||
# recursively render layouts
|
_renderer.layouts = layouts
|
||||||
layout = layouts[data["layout"]]
|
_renderer.place_in_layouts(output, payload, info)
|
||||||
|
ensure
|
||||||
Jekyll.logger.warn(
|
@_renderer = nil # this will allow the modifications above to disappear
|
||||||
"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
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Add any necessary layouts to this convertible document.
|
# Add any necessary layouts to this convertible document.
|
||||||
|
@ -254,32 +205,16 @@ module Jekyll
|
||||||
#
|
#
|
||||||
# Returns nothing.
|
# Returns nothing.
|
||||||
def do_layout(payload, layouts)
|
def do_layout(payload, layouts)
|
||||||
Jekyll.logger.debug "Rendering:", self.relative_path
|
_renderer.tap do |renderer|
|
||||||
|
renderer.layouts = layouts
|
||||||
Jekyll.logger.debug "Pre-Render Hooks:", self.relative_path
|
renderer.payload = payload
|
||||||
Jekyll::Hooks.trigger hook_owner, :pre_render, self, payload
|
renderer.run
|
||||||
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
|
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.logger.debug "Post-Render Hooks:", self.relative_path
|
||||||
Jekyll::Hooks.trigger hook_owner, :post_render, self
|
Jekyll::Hooks.trigger hook_owner, :post_render, self
|
||||||
|
ensure
|
||||||
|
@_renderer = nil # this will allow the modifications above to disappear
|
||||||
end
|
end
|
||||||
|
|
||||||
# Write the generated page file to the destination directory.
|
# Write the generated page file to the destination directory.
|
||||||
|
@ -306,5 +241,10 @@ module Jekyll
|
||||||
data[property]
|
data[property]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
def _renderer
|
||||||
|
@_renderer ||= Jekyll::Renderer.new(site, self)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,12 +2,32 @@
|
||||||
|
|
||||||
module Jekyll
|
module Jekyll
|
||||||
class Renderer
|
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)
|
||||||
@site = site
|
@site = site
|
||||||
@document = document
|
@document = document
|
||||||
@payload = site_payload || site.site_payload
|
@payload = site_payload
|
||||||
|
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
|
end
|
||||||
|
|
||||||
# Determine which converters to use based on this document's
|
# Determine which converters to use based on this document's
|
||||||
|
@ -15,7 +35,7 @@ module Jekyll
|
||||||
#
|
#
|
||||||
# Returns an array of Converter instances.
|
# Returns an array of Converter instances.
|
||||||
def converters
|
def converters
|
||||||
@converters ||= site.converters.select { |c| c.matches(document.extname) }
|
@converters ||= site.converters.select { |c| c.matches(document.extname) }.sort
|
||||||
end
|
end
|
||||||
|
|
||||||
# Determine the extname the outputted file should have
|
# Determine the extname the outputted file should have
|
||||||
|
@ -126,7 +146,7 @@ module Jekyll
|
||||||
#
|
#
|
||||||
# Returns true if the layout is invalid, false if otherwise
|
# Returns true if the layout is invalid, false if otherwise
|
||||||
def invalid_layout?(layout)
|
def invalid_layout?(layout)
|
||||||
!document.data["layout"].nil? && layout.nil?
|
!document.data["layout"].nil? && layout.nil? && !(document.is_a? Jekyll::Excerpt)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Render layouts and place given content inside.
|
# Render layouts and place given content inside.
|
||||||
|
@ -137,7 +157,7 @@ module Jekyll
|
||||||
# Returns the content placed in the Liquid-rendered layouts
|
# Returns the content placed in the Liquid-rendered layouts
|
||||||
def place_in_layouts(content, payload, info)
|
def place_in_layouts(content, payload, info)
|
||||||
output = content.dup
|
output = content.dup
|
||||||
layout = site.layouts[document.data["layout"]]
|
layout = layouts[document.data["layout"]]
|
||||||
|
|
||||||
Jekyll.logger.warn(
|
Jekyll.logger.warn(
|
||||||
"Build Warning:",
|
"Build Warning:",
|
||||||
|
@ -167,7 +187,7 @@ module Jekyll
|
||||||
site.in_source_dir(layout.path)
|
site.in_source_dir(layout.path)
|
||||||
) if document.write?
|
) if document.write?
|
||||||
|
|
||||||
if (layout = site.layouts[layout.data["layout"]])
|
if (layout = layouts[layout.data["layout"]])
|
||||||
break if used.include?(layout)
|
break if used.include?(layout)
|
||||||
used << layout
|
used << layout
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue