Module: Jekyll::Convertible

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

Instance Method Summary collapse

Instance Method Details

#converterObject

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

Returns the Converter instance.



76
77
78
# File 'lib/jekyll/convertible.rb', line 76

def converter
  @converter ||= self.site.converters.find { |c| c.matches(self.ext) }
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.



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/jekyll/convertible.rb', line 143

def do_layout(payload, layouts)
  info = { :filters => [Jekyll::Filters], :registers => { :site => self.site, :page => payload['page'] } }

  # render and transform content (this becomes the final content of the object)
  payload["pygments_prefix"] = converter.pygments_prefix
  payload["pygments_suffix"] = converter.pygments_suffix

  self.content = self.render_liquid(self.content,
                                    payload,
                                    info)
  self.transform

  # output keeps track of what will finally be written
  self.output = self.content

  self.render_all_layouts(layouts, payload, info)
end

#merged_file_read_opts(opts) ⇒ Object

Returns merged optin hash for File.read of self.site (if exists) and a given param



25
26
27
# File 'lib/jekyll/convertible.rb', line 25

def merged_file_read_opts(opts)
  (self.site ? self.site.file_read_opts : {}).merge(opts)
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.


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

def output_ext
  converter.output_ext(self.ext)
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.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/jekyll/convertible.rb', line 36

def read_yaml(base, name, opts = {})
  begin
    self.content = File.read_with_options(File.join(base, name),
                                          merged_file_read_opts(opts))
    if self.content =~ /\A(---\s*\n.*?\n?)^(---\s*$\n?)/m
      self.content = $POSTMATCH
      self.data = YAML.safe_load($1)
    end
  rescue SyntaxError => e
    puts "YAML Exception reading #{File.join(base, name)}: #{e.message}"
  rescue Exception => e
    puts "Error reading file #{File.join(base, name)}: #{e.message}"
  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



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/jekyll/convertible.rb', line 114

def render_all_layouts(layouts, payload, info)
  # recursively render layouts
  layout = layouts[self.data["layout"]]
  used = Set.new([layout])

  while layout
    payload = payload.deep_merge({"content" => self.output, "page" => layout.data})

    self.output = self.render_liquid(layout.content,
                                     payload,
                                     info,
                                     File.join(self.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



87
88
89
90
91
92
93
94
95
# File 'lib/jekyll/convertible.rb', line 87

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.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

#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.



100
101
102
103
104
105
# File 'lib/jekyll/convertible.rb', line 100

def to_liquid(attrs = nil)
  further_data = Hash[(attrs || self.class::ATTRIBUTES_FOR_LIQUID).map { |attribute|
    [attribute, send(attribute)]
  }]
  data.deep_merge(further_data)
end

#to_sObject

Returns the contents as a String.



19
20
21
# File 'lib/jekyll/convertible.rb', line 19

def to_s
  self.content || ''
end

#transformObject

Transform the contents based on the content type.

Returns nothing.



56
57
58
59
60
61
62
# File 'lib/jekyll/convertible.rb', line 56

def transform
  self.content = converter.convert(self.content)
rescue => e
  Jekyll.logger.error "Conversion error:", "There was an error converting" +
    " '#{self.path}'."
  raise e
end

#write(dest) ⇒ Object

Write the generated page file to the destination directory.

dest - The String path to the destination dir.

Returns nothing.



166
167
168
169
170
171
172
# File 'lib/jekyll/convertible.rb', line 166

def write(dest)
  path = destination(dest)
  FileUtils.mkdir_p(File.dirname(path))
  File.open(path, 'wb') do |f|
    f.write(self.output)
  end
end