Module: Jekyll::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.
-
#invalid_layout?(layout) ⇒ Boolean
Checks if the layout specified in the document actually exists.
-
#merged_file_read_opts(opts) ⇒ Object
Returns merged option hash for File.read of self.site (if exists) and a given param.
-
#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 = nil) ⇒ 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.
-
#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.
260 261 262 263 264 265 266 |
# File 'lib/jekyll/convertible.rb', line 260 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.
146 147 148 |
# File 'lib/jekyll/convertible.rb', line 146 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.
160 161 162 |
# File 'lib/jekyll/convertible.rb', line 160 def coffeescript_file? '.coffee'.eql?(ext) end |
#converters ⇒ Object
Determine which converter to use based on this convertible’s extension.
Returns the Converter instance.
94 95 96 |
# File 'lib/jekyll/convertible.rb', line 94 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 Hash. layouts - A Hash of => “layout”.
Returns nothing.
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/jekyll/convertible.rb', line 226 def do_layout(payload, layouts) 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 self.content = render_liquid(content, payload, info) if render_with_liquid? 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? 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
183 184 185 |
# File 'lib/jekyll/convertible.rb', line 183 def invalid_layout?(layout) !data["layout"].nil? && layout.nil? && !(self.is_a? Jekyll::Excerpt) end |
#merged_file_read_opts(opts) ⇒ Object
Returns merged option hash for File.read of self.site (if exists) and a given param
33 34 35 |
# File 'lib/jekyll/convertible.rb', line 33 def merged_file_read_opts(opts) (site ? site.file_read_opts : {}).merge(opts) 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.
80 81 82 83 84 85 86 87 88 |
# File 'lib/jekyll/convertible.rb', line 80 def output_ext if converters.all? { |c| c.is_a?(Jekyll::Converters::Identity) } ext else converters.map { |c| c.output_ext(ext) unless c.is_a?(Jekyll::Converters::Identity) }.compact.last end end |
#place_in_layout? ⇒ Boolean
Determine whether the file should be placed into layouts.
Returns false if the document is an asset file.
174 175 176 |
# File 'lib/jekyll/convertible.rb', line 174 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/jekyll/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.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/jekyll/convertible.rb', line 44 def read_yaml(base, name, opts = {}) begin self.content = File.read(site.in_source_dir(base, name), merged_file_read_opts(opts)) if content =~ /\A(---\s*\n.*?\n?)^((---|\.\.\.)\s*$\n?)/m self.content = $POSTMATCH self.data = SafeYAML.load($1) end rescue SyntaxError => e Jekyll.logger.warn "YAML Exception reading #{File.join(base, name)}: #{e.}" rescue Exception => e Jekyll.logger.warn "Error reading file #{File.join(base, name)}: #{e.}" end 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
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/jekyll/convertible.rb', line 194 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 payload = Utils.deep_merge_hashes(payload, {"content" => output, "page" => layout.data}) self.output = render_liquid(layout.content, payload, info, File.join(site.config['layouts'], layout.name)) if layout = layouts[layout.data["layout"]] if used.include?(layout) layout = nil # avoid recursive chain else used << layout end end end end |
#render_liquid(content, payload, info, path = nil) ⇒ 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
105 106 107 108 109 110 111 112 113 |
# File 'lib/jekyll/convertible.rb', line 105 def render_liquid(content, payload, info, path = nil) Liquid::Template.parse(content).render!(payload, info) rescue Tags::IncludeTagError => e Jekyll.logger.error "Liquid Exception:", "#{e.} in #{e.path}, included in #{path || self.path}" raise e rescue Exception => e Jekyll.logger.error "Liquid Exception:", "#{e.} in #{path || self.path}" raise e end |
#render_with_liquid? ⇒ Boolean
Determine whether the file should be rendered with Liquid.
Always returns true.
167 168 169 |
# File 'lib/jekyll/convertible.rb', line 167 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.
153 154 155 |
# File 'lib/jekyll/convertible.rb', line 153 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.
118 119 120 121 122 123 124 125 |
# File 'lib/jekyll/convertible.rb', line 118 def to_liquid(attrs = nil) further_data = Hash[(attrs || self.class::ATTRIBUTES_FOR_LIQUID).map { |attribute| [attribute, send(attribute)] }] 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/jekyll/convertible.rb', line 22 def to_s content || '' end |
#transform ⇒ Object
Transform the contents based on the content type.
Returns the transformed contents.
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/jekyll/convertible.rb', line 64 def transform converters.reduce(content) do |output, converter| begin converter.convert output rescue => e Jekyll.logger.error "Conversion error:", "#{converter.class} encountered an error converting '#{path}'." Jekyll.logger.error("Conversion 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.
131 132 133 134 135 136 137 138 139 |
# File 'lib/jekyll/convertible.rb', line 131 def type if is_a?(Draft) :drafts elsif is_a?(Post) :posts elsif is_a?(Page) :pages end end |
#write(dest) ⇒ Object
Write the generated page file to the destination directory.
dest - The String path to the destination dir.
Returns nothing.
247 248 249 250 251 252 253 |
# File 'lib/jekyll/convertible.rb', line 247 def write(dest) path = destination(dest) FileUtils.mkdir_p(File.dirname(path)) File.open(path, 'wb') do |f| f.write(output) end end |