Module: Bunto::Convertible
Instance Method Summary collapse
-
#[](property) ⇒ Object
Accessor for data properties by Liquid.
-
#asset_file? ⇒ Boolean
Determine whether the document is an asset file.
-
#coffeescript_file? ⇒ Boolean
Determine whether the document is a CoffeeScript file.
-
#converters ⇒ Object
Determine which converter to use based on this convertible’s extension.
-
#do_layout(payload, layouts) ⇒ Object
Add any necessary layouts to this convertible document.
-
#hook_owner ⇒ Object
returns the owner symbol for hook triggering.
-
#invalid_layout?(layout) ⇒ Boolean
Checks if the layout specified in the document actually exists.
-
#output_ext ⇒ Object
Determine the extension depending on content_type.
-
#place_in_layout? ⇒ Boolean
Determine whether the file should be placed into layouts.
-
#published? ⇒ Boolean
Whether the file is published or not, as indicated in YAML front-matter.
-
#read_yaml(base, name, opts = {}) ⇒ Object
Read the YAML frontmatter.
-
#render_all_layouts(layouts, payload, info) ⇒ Object
Recursively render layouts.
-
#render_liquid(content, payload, info, path) ⇒ Object
Render Liquid in the content.
-
#render_with_liquid? ⇒ Boolean
Determine whether the file should be rendered with Liquid.
-
#sass_file? ⇒ Boolean
Determine whether the document is a Sass file.
-
#to_liquid(attrs = nil) ⇒ Object
Convert this Convertible’s data to a Hash suitable for use by Liquid.
-
#to_s ⇒ Object
Returns the contents as a String.
-
#transform ⇒ Object
Transform the contents based on the content type.
-
#type ⇒ Object
The type of a document, i.e., its classname downcase’d and to_sym’d.
- #validate_data!(filename) ⇒ Object
- #validate_permalink!(filename) ⇒ Object
-
#write(dest) ⇒ Object
Write the generated page file to the destination directory.
Instance Method Details
#[](property) ⇒ Object
Accessor for data properties by Liquid.
property - The String name of the property to retrieve.
Returns the String value or nil if the property isn’t included.
302 303 304 305 306 307 308 |
# File 'lib/bunto/convertible.rb', line 302 def [](property) if self.class::ATTRIBUTES_FOR_LIQUID.include?(property) send(property) else data[property] end end |
#asset_file? ⇒ Boolean
Determine whether the document is an asset file. Asset files include CoffeeScript files and Sass/SCSS files.
Returns true if the extname belongs to the set of extensions
that asset files use.
165 166 167 |
# File 'lib/bunto/convertible.rb', line 165 def asset_file? sass_file? || coffeescript_file? end |
#coffeescript_file? ⇒ Boolean
Determine whether the document is a CoffeeScript file.
Returns true if extname == .coffee, false otherwise.
179 180 181 |
# File 'lib/bunto/convertible.rb', line 179 def coffeescript_file? ".coffee" == ext end |
#converters ⇒ Object
Determine which converter to use based on this convertible’s extension.
Returns the Converter instance.
105 106 107 |
# File 'lib/bunto/convertible.rb', line 105 def converters @converters ||= site.converters.select { |c| c.matches(ext) }.sort end |
#do_layout(payload, layouts) ⇒ Object
Add any necessary layouts to this convertible document.
payload - The site payload Drop or Hash. layouts - A Hash of => “layout”.
Returns nothing.
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/bunto/convertible.rb', line 256 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"] } } # 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? Bunto.logger.debug "Rendering Liquid:", self.relative_path self.content = render_liquid(content, payload, info, path) end Bunto.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? Bunto.logger.debug "Post-Render Hooks:", self.relative_path Bunto::Hooks.trigger hook_owner, :post_render, self end |
#hook_owner ⇒ Object
returns the owner symbol for hook triggering
154 155 156 157 158 |
# File 'lib/bunto/convertible.rb', line 154 def hook_owner if is_a?(Page) :pages end end |
#invalid_layout?(layout) ⇒ Boolean
Checks if the layout specified in the document actually exists
layout - the layout to check
Returns true if the layout is invalid, false if otherwise
202 203 204 |
# File 'lib/bunto/convertible.rb', line 202 def invalid_layout?(layout) !data["layout"].nil? && layout.nil? && !(self.is_a? Bunto::Excerpt) end |
#output_ext ⇒ Object
Determine the extension depending on content_type.
Returns the String extension for the output file.
e.g. ".html" for an HTML output file.
97 98 99 |
# File 'lib/bunto/convertible.rb', line 97 def output_ext Bunto::Renderer.new(site, self).output_ext end |
#place_in_layout? ⇒ Boolean
Determine whether the file should be placed into layouts.
Returns false if the document is an asset file.
193 194 195 |
# File 'lib/bunto/convertible.rb', line 193 def place_in_layout? !asset_file? end |
#published? ⇒ Boolean
Whether the file is published or not, as indicated in YAML front-matter
27 28 29 |
# File 'lib/bunto/convertible.rb', line 27 def published? !(data.key?("published") && data["published"] == false) end |
#read_yaml(base, name, opts = {}) ⇒ Object
Read the YAML frontmatter.
base - The String path to the dir containing the file. name - The String filename of the file. opts - optional parameter to File.read, default at site configs
Returns nothing.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/bunto/convertible.rb', line 38 def read_yaml(base, name, opts = {}) filename = File.join(base, name) begin self.content = File.read(@path || site.in_source_dir(base, name), Utils.merged_file_read_opts(site, opts)) if content =~ Document::YAML_FRONT_MATTER_REGEXP self.content = $POSTMATCH self.data = SafeYAML.load(Regexp.last_match(1)) end rescue SyntaxError => e Bunto.logger.warn "YAML Exception reading #{filename}: #{e.}" rescue => e Bunto.logger.warn "Error reading file #{filename}: #{e.}" end self.data ||= {} validate_data! filename validate_permalink! filename self.data end |
#render_all_layouts(layouts, payload, info) ⇒ Object
Recursively render layouts
layouts - a list of the layouts payload - the payload for Liquid info - the info for Liquid
Returns nothing
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/bunto/convertible.rb', line 213 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 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(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 |
#render_liquid(content, payload, info, path) ⇒ Object
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
116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/bunto/convertible.rb', line 116 def render_liquid(content, payload, info, path) 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:", LiquidRenderer.format_error(e, path || self.path) raise e end |
#render_with_liquid? ⇒ Boolean
Determine whether the file should be rendered with Liquid.
Always returns true.
186 187 188 |
# File 'lib/bunto/convertible.rb', line 186 def render_with_liquid? true end |
#sass_file? ⇒ Boolean
Determine whether the document is a Sass file.
Returns true if extname == .sass or .scss, false otherwise.
172 173 174 |
# File 'lib/bunto/convertible.rb', line 172 def sass_file? %w(.sass .scss).include?(ext) end |
#to_liquid(attrs = nil) ⇒ Object
Convert this Convertible’s data to a Hash suitable for use by Liquid.
Returns the Hash representation of this Convertible.
134 135 136 137 138 139 140 141 |
# File 'lib/bunto/convertible.rb', line 134 def to_liquid(attrs = nil) further_data = Hash[(attrs || self.class::ATTRIBUTES_FOR_LIQUID).map do |attribute| [attribute, send(attribute)] end] defaults = site.frontmatter_defaults.all(relative_path, type) Utils.deep_merge_hashes defaults, Utils.deep_merge_hashes(data, further_data) end |
#to_s ⇒ Object
Returns the contents as a String.
22 23 24 |
# File 'lib/bunto/convertible.rb', line 22 def to_s content || "" end |
#transform ⇒ Object
Transform the contents based on the content type.
Returns the transformed contents.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/bunto/convertible.rb', line 78 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("", e.to_s) raise e end end end |
#type ⇒ Object
The type of a document,
i.e., its classname downcase'd and to_sym'd.
Returns the type of self.
147 148 149 150 151 |
# File 'lib/bunto/convertible.rb', line 147 def type if is_a?(Page) :pages end end |
#validate_data!(filename) ⇒ Object
62 63 64 65 66 67 |
# File 'lib/bunto/convertible.rb', line 62 def validate_data!(filename) unless self.data.is_a?(Hash) raise Errors::InvalidYAMLFrontMatterError, "Invalid YAML front matter in #{filename}" end end |
#validate_permalink!(filename) ⇒ Object
69 70 71 72 73 |
# File 'lib/bunto/convertible.rb', line 69 def validate_permalink!(filename) if self.data["permalink"] && self.data["permalink"].empty? raise Errors::InvalidPermalinkError, "Invalid permalink in #{filename}" end end |
#write(dest) ⇒ Object
Write the generated page file to the destination directory.
dest - The String path to the destination dir.
Returns nothing.
290 291 292 293 294 295 |
# File 'lib/bunto/convertible.rb', line 290 def write(dest) path = destination(dest) FileUtils.mkdir_p(File.dirname(path)) File.write(path, output, :mode => "wb") Bunto::Hooks.trigger hook_owner, :post_write, self end |