Merge pull request #1001 from mojombo/refactoring-convertible

Refactoring Jekyll::Convertible
This commit is contained in:
Parker Moore 2013-04-28 16:43:38 -07:00
commit b4b66d5713
1 changed files with 50 additions and 35 deletions

View File

@ -66,6 +66,52 @@ module Jekyll
@converter ||= self.site.converters.find { |c| c.matches(self.ext) }
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)
Liquid::Template.parse(content).render!(payload, info)
rescue => e
Jekyll::Logger.error "Liquid Exception:", "#{e.message} in #{payload[:file]}"
e.backtrace.each do |backtrace|
puts backtrace
end
abort("Build Failed")
end
# Recursively render layouts
#
# layouts - a list of the layouts
# payload - the payload for Liquid
# info - the info for Liquid
#
# Returns nothing
def render_all_layouts(layouts, payload, info)
# recursively render layouts
layout = layouts[self.data["layout"]]
used = Set.new([layout])
while layout
payload = payload.deep_merge({"content" => self.output, "page" => layout.data})
self.output = self.render_liquid(layout.content,
payload.merge({:file => self.data["layout"]}),
info)
if layout = layouts[layout.data["layout"]]
if used.include?(layout)
layout = nil # avoid recursive chain
else
used << layout
end
end
end
end
# Add any necessary layouts to this convertible document.
#
# payload - The site payload Hash.
@ -79,46 +125,15 @@ module Jekyll
payload["pygments_prefix"] = converter.pygments_prefix
payload["pygments_suffix"] = converter.pygments_suffix
begin
self.content = Liquid::Template.parse(self.content).render!(payload, info)
rescue => e
puts "Liquid Exception: #{e.message} in #{self.name}"
e.backtrace.each do |backtrace|
puts backtrace
end
abort("Build Failed")
end
self.content = self.render_liquid(self.content,
payload.merge({:file => self.name}),
info)
self.transform
# output keeps track of what will finally be written
self.output = self.content
# recursively render layouts
layout = layouts[self.data["layout"]]
used = Set.new([layout])
while layout
payload = payload.deep_merge({"content" => self.output, "page" => layout.data})
begin
self.output = Liquid::Template.parse(layout.content).render!(payload, info)
rescue => e
puts "Liquid Exception: #{e.message} in #{self.data["layout"]}"
e.backtrace.each do |backtrace|
puts backtrace
end
abort("Build Failed")
end
if layout = layouts[layout.data["layout"]]
if used.include?(layout)
layout = nil # avoid recursive chain
else
used << layout
end
end
end
self.render_all_layouts(layouts, payload, info)
end
# Write the generated page file to the destination directory.