Module: Jekyll::Convertible

Included in:
Layout, Page
Defined in:
lib/jekyll/convertible.rb

Instance Method Summary collapse

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.



238
239
240
241
242
243
244
# File 'lib/jekyll/convertible.rb', line 238

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.

Returns:

  • (Boolean)


148
149
150
# File 'lib/jekyll/convertible.rb', line 148

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.

Returns:

  • (Boolean)


162
163
164
# File 'lib/jekyll/convertible.rb', line 162

def coffeescript_file?
  ext == ".coffee"
end

#convertersObject

Determine which converter to use based on this convertible’s extension.

Returns the Converter instance.



98
99
100
# File 'lib/jekyll/convertible.rb', line 98

def converters
  _renderer.converters
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.



209
210
211
212
213
214
215
216
217
218
219
# File 'lib/jekyll/convertible.rb', line 209

def do_layout(payload, layouts)
  self.output = _renderer.tap do |renderer|
    renderer.layouts = layouts
    renderer.payload = payload
  end.run

  Jekyll.logger.debug "Post-Render Hooks:", self.relative_path
  Jekyll::Hooks.trigger hook_owner, :post_render, self
ensure
  @_renderer = nil # this will allow the modifications above to disappear
end

#hook_ownerObject

returns the owner symbol for hook triggering



137
138
139
140
141
# File 'lib/jekyll/convertible.rb', line 137

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

Returns:

  • (Boolean)


185
186
187
# File 'lib/jekyll/convertible.rb', line 185

def invalid_layout?(layout)
  !data["layout"].nil? && layout.nil? && !(self.is_a? Jekyll::Excerpt)
end

#output_extObject

Determine the extension depending on content_type.

Returns the String extension for the output file.

e.g. ".html" for an HTML output file.


90
91
92
# File 'lib/jekyll/convertible.rb', line 90

def output_ext
  _renderer.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.

Returns:

  • (Boolean)


176
177
178
# File 'lib/jekyll/convertible.rb', line 176

def place_in_layout?
  !asset_file?
end

#published?Boolean

Whether the file is published or not, as indicated in YAML front-matter

Returns:

  • (Boolean)


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. rubocop:disable Metrics/AbcSize



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/jekyll/convertible.rb', line 39

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
    Jekyll.logger.warn "YAML Exception reading #{filename}: #{e.message}"
    raise e if self.site.config["strict_front_matter"]
  rescue => e
    Jekyll.logger.warn "Error reading file #{filename}: #{e.message}"
    raise e if self.site.config["strict_front_matter"]
  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



196
197
198
199
200
201
# File 'lib/jekyll/convertible.rb', line 196

def render_all_layouts(layouts, payload, info)
  _renderer.layouts = layouts
  self.output = _renderer.place_in_layouts(output, payload, info)
ensure
  @_renderer = nil # this will allow the modifications above to disappear
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



109
110
111
# File 'lib/jekyll/convertible.rb', line 109

def render_liquid(content, payload, info, path)
  _renderer.render_liquid(content, payload, info, path)
end

#render_with_liquid?Boolean

Determine whether the file should be rendered with Liquid.

Always returns true.

Returns:

  • (Boolean)


169
170
171
# File 'lib/jekyll/convertible.rb', line 169

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.

Returns:

  • (Boolean)


155
156
157
# File 'lib/jekyll/convertible.rb', line 155

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.



117
118
119
120
121
122
123
124
# File 'lib/jekyll/convertible.rb', line 117

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_sObject

Returns the contents as a String.



22
23
24
# File 'lib/jekyll/convertible.rb', line 22

def to_s
  content || ""
end

#transformObject

Transform the contents based on the content type.

Returns the transformed contents.



82
83
84
# File 'lib/jekyll/convertible.rb', line 82

def transform
  _renderer.convert(content)
end

#typeObject

The type of a document,

i.e., its classname downcase'd and to_sym'd.

Returns the type of self.



130
131
132
133
134
# File 'lib/jekyll/convertible.rb', line 130

def type
  if is_a?(Page)
    :pages
  end
end

#validate_data!(filename) ⇒ Object

rubocop:enable Metrics/AbcSize



66
67
68
69
70
71
# File 'lib/jekyll/convertible.rb', line 66

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



73
74
75
76
77
# File 'lib/jekyll/convertible.rb', line 73

def validate_permalink!(filename)
  if self.data["permalink"] && self.data["permalink"].to_s.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.



226
227
228
229
230
231
# File 'lib/jekyll/convertible.rb', line 226

def write(dest)
  path = destination(dest)
  FileUtils.mkdir_p(File.dirname(path))
  File.write(path, output, :mode => "wb")
  Jekyll::Hooks.trigger hook_owner, :post_write, self
end