lib/bunto/convertible.rb in bunto-3.0.0 vs lib/bunto/convertible.rb in bunto-3.2.1
- old
+ new
@@ -1,8 +1,8 @@
# encoding: UTF-8
-require 'set'
+require "set"
# Convertible provides methods for converting a pagelike item
# from a certain type of markup into actual content
#
# Requires
@@ -18,16 +18,16 @@
module Bunto
module Convertible
# Returns the contents as a String.
def to_s
- content || ''
+ content || ""
end
# Whether the file is published or not, as indicated in YAML front-matter
def published?
- !(data.key?('published') && data['published'] == false)
+ !(data.key?("published") && data["published"] == false)
end
# Read the YAML frontmatter.
#
# base - The String path to the dir containing the file.
@@ -45,11 +45,11 @@
self.content = $POSTMATCH
self.data = SafeYAML.load(Regexp.last_match(1))
end
rescue SyntaxError => e
Bunto.logger.warn "YAML Exception reading #{filename}: #{e.message}"
- rescue Exception => e
+ rescue => e
Bunto.logger.warn "Error reading file #{filename}: #{e.message}"
end
self.data ||= {}
@@ -59,16 +59,17 @@
self.data
end
def validate_data!(filename)
unless self.data.is_a?(Hash)
- raise Errors::InvalidYAMLFrontMatterError, "Invalid YAML front matter in #{filename}"
+ raise Errors::InvalidYAMLFrontMatterError,
+ "Invalid YAML front matter in #{filename}"
end
end
def validate_permalink!(filename)
- if self.data['permalink'] && self.data['permalink'].size == 0
+ if self.data["permalink"] && self.data["permalink"].empty?
raise Errors::InvalidPermalinkError, "Invalid permalink in #{filename}"
end
end
# Transform the contents based on the content type.
@@ -77,11 +78,14 @@
def transform
converters.reduce(content) do |output, converter|
begin
converter.convert output
rescue => e
- Bunto.logger.error "Conversion error:", "#{converter.class} encountered an error while converting '#{path}':"
+ Bunto.logger.error(
+ "Conversion error:",
+ "#{converter.class} encountered an error while converting '#{path}':"
+ )
Bunto.logger.error("", e.to_s)
raise e
end
end
end
@@ -108,18 +112,23 @@
# payload - the payload for Liquid
# info - the info for Liquid
#
# Returns the converted content
def render_liquid(content, payload, info, path)
- site.liquid_renderer.file(path).parse(content).render!(payload, info)
- rescue Tags::IncludeTagError => e
- Bunto.logger.error "Liquid Exception:", "#{e.message} in #{e.path}, included in #{path || self.path}"
- raise e
+ template = site.liquid_renderer.file(path).parse(content)
+ template.warnings.each do |e|
+ Bunto.logger.warn "Liquid Warning:",
+ LiquidRenderer.format_error(e, path || self.path)
+ end
+ template.render!(payload, info)
+ # rubocop: disable RescueException
rescue Exception => e
- Bunto.logger.error "Liquid Exception:", "#{e.message} in #{path || self.path}"
+ Bunto.logger.error "Liquid Exception:",
+ LiquidRenderer.format_error(e, path || self.path)
raise e
end
+ # rubocop: enable RescueException
# Convert this Convertible's data to a Hash suitable for use by Liquid.
#
# Returns the Hash representation of this Convertible.
def to_liquid(attrs = nil)
@@ -166,11 +175,11 @@
# Determine whether the document is a CoffeeScript file.
#
# Returns true if extname == .coffee, false otherwise.
def coffeescript_file?
- '.coffee'.eql?(ext)
+ ".coffee" == ext
end
# Determine whether the file should be rendered with Liquid.
#
# Always returns true.
@@ -203,18 +212,24 @@
# Returns nothing
def render_all_layouts(layouts, payload, info)
# recursively render layouts
layout = layouts[data["layout"]]
- Bunto.logger.warn("Build Warning:", "Layout '#{data["layout"]}' requested in #{path} does not exist.") if invalid_layout? layout
+ Bunto.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
Bunto.logger.debug "Rendering Layout:", path
payload["content"] = output
- payload["layout"] = Utils.deep_merge_hashes(payload["layout"] || {}, layout.data)
+ payload["layout"] = Utils.deep_merge_hashes(layout.data, payload["layout"] || {})
self.output = render_liquid(layout.content,
payload,
info,
layout.relative_path)
@@ -223,16 +238,13 @@
site.regenerator.add_dependency(
site.in_source_dir(path),
site.in_source_dir(layout.path)
)
- if layout = layouts[layout.data["layout"]]
- if used.include?(layout)
- layout = nil # avoid recursive chain
- else
- used << layout
- end
+ if (layout = layouts[layout.data["layout"]])
+ break if used.include?(layout)
+ used << layout
end
end
end
# Add any necessary layouts to this convertible document.
@@ -244,11 +256,14 @@
def do_layout(payload, layouts)
Bunto.logger.debug "Rendering:", self.relative_path
Bunto.logger.debug "Pre-Render Hooks:", self.relative_path
Bunto::Hooks.trigger hook_owner, :pre_render, self, payload
- info = { :filters => [Bunto::Filters], :registers => { :site => site, :page => payload["page"] } }
+ info = {
+ :filters => [Bunto::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
@@ -273,12 +288,10 @@
#
# Returns nothing.
def write(dest)
path = destination(dest)
FileUtils.mkdir_p(File.dirname(path))
- File.open(path, 'wb') do |f|
- f.write(output)
- end
+ File.write(path, output, :mode => "wb")
Bunto::Hooks.trigger hook_owner, :post_write, self
end
# Accessor for data properties by Liquid.
#