Skip to content

Commit

Permalink
Revert "Merge pull request jekyll#5308 from jekyll/rubocop-convertible"
Browse files Browse the repository at this point in the history
This reverts commit 2f167ae, reversing
changes made to 4f80dde.
  • Loading branch information
Crunch09 committed Sep 6, 2016
1 parent f69671c commit 1fc3415
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 47 deletions.
1 change: 1 addition & 0 deletions .rubocop.yml
Expand Up @@ -4,6 +4,7 @@ AllCops:
Include:
- lib/**/*.rb
Exclude:
- lib/jekyll/convertible.rb
- lib/jekyll/renderer.rb
- bin/**/*
- benchmark/**/*
Expand Down
102 changes: 81 additions & 21 deletions lib/jekyll/convertible.rb
Expand Up @@ -35,7 +35,6 @@ def published?
# 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)

Expand All @@ -59,7 +58,6 @@ def read_yaml(base, name, opts = {})

self.data
end
# rubocop:enable Metrics/AbcSize

def validate_data!(filename)
unless self.data.is_a?(Hash)
Expand All @@ -78,23 +76,34 @@ def validate_permalink!(filename)
#
# Returns the transformed contents.
def transform
_renderer.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
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
_renderer.output_ext
Jekyll::Renderer.new(site, self).output_ext
end

# Determine which converter to use based on this convertible's
# extension.
#
# Returns the Converter instance.
def converters
_renderer.converters
@converters ||= site.converters.select { |c| c.matches(ext) }.sort
end

# Render Liquid in the content
Expand All @@ -105,7 +114,17 @@ def converters
#
# Returns the converted content
def render_liquid(content, payload, info, path)
_renderer.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
end
# rubocop: enable RescueException

Expand Down Expand Up @@ -192,10 +211,40 @@ def invalid_layout?(layout)
#
# 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
# 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
end

# Add any necessary layouts to this convertible document.
Expand All @@ -205,16 +254,32 @@ def render_all_layouts(layouts, payload, info)
#
# Returns nothing.
def do_layout(payload, layouts)
_renderer.tap do |renderer|
renderer.layouts = layouts
renderer.payload = payload
renderer.run
Jekyll.logger.debug "Rendering:", self.relative_path

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
ensure
@_renderer = nil # this will allow the modifications above to disappear
end

# Write the generated page file to the destination directory.
Expand All @@ -241,10 +306,5 @@ def [](property)
data[property]
end
end

private
def _renderer
@_renderer ||= Jekyll::Renderer.new(site, self)
end
end
end
32 changes: 6 additions & 26 deletions lib/jekyll/renderer.rb
Expand Up @@ -2,40 +2,20 @@

module Jekyll
class Renderer
attr_reader :document, :site
attr_writer :layouts, :payload
attr_reader :document, :site, :payload

def initialize(site, document, site_payload = nil)
@site = site
@document = document
@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
@payload = site_payload || site.site_payload
end

# Determine which converters to use based on this document's
# extension.
#
# Returns an array of Converter instances.
def converters
@converters ||= site.converters.select { |c| c.matches(document.extname) }.sort
@converters ||= site.converters.select { |c| c.matches(document.extname) }
end

# Determine the extname the outputted file should have
Expand Down Expand Up @@ -146,7 +126,7 @@ def render_liquid(content, payload, info, path = nil)
#
# Returns true if the layout is invalid, false if otherwise
def invalid_layout?(layout)
!document.data["layout"].nil? && layout.nil? && !(document.is_a? Jekyll::Excerpt)
!document.data["layout"].nil? && layout.nil?
end

# Render layouts and place given content inside.
Expand All @@ -157,7 +137,7 @@ def invalid_layout?(layout)
# Returns the content placed in the Liquid-rendered layouts
def place_in_layouts(content, payload, info)
output = content.dup
layout = layouts[document.data["layout"]]
layout = site.layouts[document.data["layout"]]

Jekyll.logger.warn(
"Build Warning:",
Expand Down Expand Up @@ -187,7 +167,7 @@ def place_in_layouts(content, payload, info)
site.in_source_dir(layout.path)
) if document.write?

if (layout = layouts[layout.data["layout"]])
if (layout = site.layouts[layout.data["layout"]])
break if used.include?(layout)
used << layout
end
Expand Down

0 comments on commit 1fc3415

Please sign in to comment.