Module: Jekyll::Convertible

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

Instance Method Summary (collapse)

Instance Method Details

- (Object) converter

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

Returns the Converter instance.



63
64
65
# File 'lib/jekyll/convertible.rb', line 63

def converter
  @converter ||= self.site.converters.find { |c| c.matches(self.ext) }
end

- (Object) do_layout(payload, layouts)

Add any necessary layouts to this convertible document.

payload - The site payload Hash. layouts - A Hash of => “layout”.

Returns nothing.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/jekyll/convertible.rb', line 73

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

  begin
    self.content = Liquid::Template.parse(self.content).render!(payload, info)
  rescue => e
    puts "Liquid Exception: #{e.message} in #{self.name}"
    e.backtrace.each do |backtrace|
      puts backtrace
    end
    abort("Build Failed")
  end

  self.transform

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

  # recursively render layouts
  layout = layouts[self.data["layout"]]
  used = Set.new([layout])

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

    begin
      self.output = Liquid::Template.parse(layout.content).render!(payload, info)
    rescue => e
      puts "Liquid Exception: #{e.message} in #{self.data["layout"]}"
      e.backtrace.each do |backtrace|
        puts backtrace
      end
      abort("Build Failed")
    end

    if layout = layouts[layout.data["layout"]]
      if used.include?(layout)
        layout = nil # avoid recursive chain
      else
        used << layout
      end
    end
  end
end

- (Object) output_ext

Determine the extension depending on content_type.

Returns the String extension for the output file.

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


55
56
57
# File 'lib/jekyll/convertible.rb', line 55

def output_ext
  converter.output_ext(self.ext)
end

- (Object) read_yaml(base, name)

Read the YAML frontmatter.

base - The String path to the dir containing the file. name - The String filename of the file.

Returns nothing.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/jekyll/convertible.rb', line 27

def read_yaml(base, name)
  begin
    self.content = File.read(File.join(base, name))

    if self.content =~ /\A(---\s*\n.*?\n?)^(---\s*$\n?)/m
      self.content = $POSTMATCH
      self.data = YAML.safe_load($1)
    end
  rescue => e
    puts "Error reading file #{File.join(base, name)}: #{e.message}"
  rescue SyntaxError => e
    puts "YAML Exception reading #{File.join(base, name)}: #{e.message}"
  end

  self.data ||= {}
end

- (Object) to_s

Returns the contents as a String.



17
18
19
# File 'lib/jekyll/convertible.rb', line 17

def to_s
  self.content || ''
end

- (Object) transform

Transform the contents based on the content type.

Returns nothing.



47
48
49
# File 'lib/jekyll/convertible.rb', line 47

def transform
  self.content = converter.convert(self.content)
end

- (Object) write(dest)

Write the generated page file to the destination directory.

dest - The String path to the destination dir.

Returns nothing.



127
128
129
130
131
132
133
# File 'lib/jekyll/convertible.rb', line 127

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