module Jekyll::Convertible
Public Instance Methods
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.
# File lib/jekyll/convertible.rb, line 288 def [](property) if self.class::ATTRIBUTES_FOR_LIQUID.include?(property) send(property) else data[property] end end
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.
# File lib/jekyll/convertible.rb, line 155 def asset_file? sass_file? || coffeescript_file? end
Determine whether the document is a CoffeeScript file.
Returns true if extname == .coffee, false otherwise.
# File lib/jekyll/convertible.rb, line 169 def coffeescript_file? '.coffee'.eql?(ext) end
Determine which converter to use based on this convertible's extension.
Returns the Converter instance.
# File lib/jekyll/convertible.rb, line 100 def converters @converters ||= site.converters.select { |c| c.matches(ext) }.sort end
Add any necessary layouts to this convertible document.
payload - The site payload Drop or Hash. layouts - A Hash of {“name” => “layout”}.
Returns nothing.
# File lib/jekyll/convertible.rb, line 243 def do_layout(payload, layouts) 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 end
returns the owner symbol for hook triggering
# File lib/jekyll/convertible.rb, line 144 def hook_owner if is_a?(Page) :pages end end
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
# File lib/jekyll/convertible.rb, line 192 def invalid_layout?(layout) !data["layout"].nil? && layout.nil? && !(self.is_a? Jekyll::Excerpt) end
Determine the extension depending on content_type.
Returns the String extension for the output file.
e.g. ".html" for an HTML output file.
# File lib/jekyll/convertible.rb, line 92 def output_ext Jekyll::Renderer.new(site, self).output_ext end
Determine whether the file should be placed into layouts.
Returns false if the document is an asset file.
# File lib/jekyll/convertible.rb, line 183 def place_in_layout? !asset_file? end
Whether the file is published or not, as indicated in YAML front-matter
# File lib/jekyll/convertible.rb, line 26 def published? !(data.key?('published') && data['published'] == false) end
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.
# File lib/jekyll/convertible.rb, line 37 def read_yaml(base, name, opts = {}) filename = File.join(base, name) begin self.content = File.read(site.in_source_dir(base, name), Utils.merged_file_read_opts(site, opts)) if content =~ /\A(---\s*\n.*?\n?)^((---|\.\.\.)\s*$\n?)/m self.content = $POSTMATCH self.data = SafeYAML.load(Regexp.last_match(1)) end rescue SyntaxError => e Jekyll.logger.warn "YAML Exception reading #{filename}: #{e.message}" rescue Exception => e Jekyll.logger.warn "Error reading file #{filename}: #{e.message}" end self.data ||= {} validate_data! filename validate_permalink! filename self.data end
Recursively render layouts
layouts - a list of the layouts payload - the payload for Liquid info - the info for Liquid
Returns nothing
# File lib/jekyll/convertible.rb, line 203 def render_all_layouts(layouts, payload, info) # 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]) while layout Jekyll.logger.debug "Rendering Layout:", path payload["content"] = output payload["layout"] = Utils.deep_merge_hashes(payload["layout"] || {}, layout.data) self.output = render_liquid(layout.content, payload, info, File.join(site.config['layouts_dir'], layout.name)) # 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"]] if used.include?(layout) layout = nil # avoid recursive chain else used << layout end end end 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
# File lib/jekyll/convertible.rb, line 111 def render_liquid(content, payload, info, path) site.liquid_renderer.file(path).parse(content).render!(payload, info) rescue Tags::IncludeTagError => e Jekyll.logger.error "Liquid Exception:", "#{e.message} in #{e.path}, included in #{path || self.path}" raise e rescue Exception => e Jekyll.logger.error "Liquid Exception:", "#{e.message} in #{path || self.path}" raise e end
Determine whether the file should be rendered with Liquid.
Always returns true.
# File lib/jekyll/convertible.rb, line 176 def render_with_liquid? true end
Determine whether the document is a Sass file.
Returns true if extname == .sass or .scss, false otherwise.
# File lib/jekyll/convertible.rb, line 162 def sass_file? %w(.sass .scss).include?(ext) end
Convert this Convertible's data to a Hash suitable for use by Liquid.
Returns the Hash representation of this Convertible.
# File lib/jekyll/convertible.rb, line 124 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
Returns the contents as a String.
# File lib/jekyll/convertible.rb, line 21 def to_s content || '' end
Transform the contents based on the content type.
Returns the transformed contents.
# File lib/jekyll/convertible.rb, line 76 def 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
The type of a document,
i.e., its classname downcase'd and to_sym'd.
Returns the type of self.
# File lib/jekyll/convertible.rb, line 137 def type if is_a?(Page) :pages end end
# File lib/jekyll/convertible.rb, line 61 def validate_data!(filename) unless self.data.is_a?(Hash) raise Errors::InvalidYAMLFrontMatterError, "Invalid YAML front matter in #{filename}" end end
# File lib/jekyll/convertible.rb, line 67 def validate_permalink!(filename) if self.data['permalink'] && self.data['permalink'].size == 0 raise Errors::InvalidPermalinkError, "Invalid permalink in #{filename}" end end
Write the generated page file to the destination directory.
dest - The String path to the destination dir.
Returns nothing.
# File lib/jekyll/convertible.rb, line 274 def write(dest) path = destination(dest) FileUtils.mkdir_p(File.dirname(path)) File.open(path, 'wb') do |f| f.write(output) end Jekyll::Hooks.trigger hook_owner, :post_write, self end