Refactoring Jekyll::Convertible

This commit is contained in:
Parker Moore 2013-04-26 19:36:52 +02:00
parent fe36b0f479
commit 29a1c18300
1 changed files with 43 additions and 35 deletions

View File

@ -66,6 +66,45 @@ module Jekyll
@converter ||= self.site.converters.find { |c| c.matches(self.ext) } @converter ||= self.site.converters.find { |c| c.matches(self.ext) }
end end
# Render Liquid in the content
#
# content -
# payload -
# info -
#
# 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
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. # Add any necessary layouts to this convertible document.
# #
# payload - The site payload Hash. # payload - The site payload Hash.
@ -79,46 +118,15 @@ module Jekyll
payload["pygments_prefix"] = converter.pygments_prefix payload["pygments_prefix"] = converter.pygments_prefix
payload["pygments_suffix"] = converter.pygments_suffix payload["pygments_suffix"] = converter.pygments_suffix
begin self.content = self.render_liquid(self.content,
self.content = Liquid::Template.parse(self.content).render!(payload, info) payload.merge({:file => self.name}),
rescue => e info)
puts "Liquid Exception: #{e.message} in #{self.name}"
e.backtrace.each do |backtrace|
puts backtrace
end
abort("Build Failed")
end
self.transform self.transform
# output keeps track of what will finally be written # output keeps track of what will finally be written
self.output = self.content self.output = self.content
# recursively render layouts self.render_all_layouts(layouts, payload, info)
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
end end
# Write the generated page file to the destination directory. # Write the generated page file to the destination directory.