Module: Jekyll::Convertible

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

Instance Method Summary collapse

Instance Method Details

#content_typeObject

Determine which formatting engine to use based on this convertible’s extension

Returns the content type as a string, usually the same as the extension



43
44
45
46
47
48
49
# File 'lib/jekyll/convertible.rb', line 43

def content_type
  case self.ext[1..-1]
  when /markdown/i, /mkdn/i, /md/i # aliases for the Markdown format
    return 'markdown'
  end
  ext[1..-1]
end

#do_layout(payload, layouts) ⇒ Object

Add any necessary layouts to this convertible document

+layouts+ is a Hash of {"name" => "layout"}
+site_payload+ is the site payload hash

Returns nothing



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/jekyll/convertible.rb', line 56

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

  # render and transform content (this becomes the final content of the object)
  payload["content_type"] = self.content_type
  self.content = Liquid::Template.parse(self.content).render(payload, info)
  self.transform

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

  # recursively render layouts
  layout = layouts[self.data["layout"]]
  while layout
    payload = payload.deep_merge({"content" => self.output, "page" => layout.data})
    layout.transform
    self.output = Liquid::Template.parse(layout.content).render(payload, info)

    layout = layouts[layout.data["layout"]]
  end
end

#read_yaml(base, name) ⇒ Object

Read the YAML frontmatter

+base+ is the String path to the dir containing the file
+name+ is the String filename of the file

Returns nothing



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/jekyll/convertible.rb', line 18

def read_yaml(base, name)
  self.content = File.read(File.join(base, name))
  
  if self.content =~ /^(---\s*\n.*?\n?)(---.*?\n)/m
    self.content = self.content[($1.size + $2.size)..-1]
  
    self.data = YAML.load($1)
  end
  
  self.data ||= {}
end

#to_sObject

Return the contents as a string



9
10
11
# File 'lib/jekyll/convertible.rb', line 9

def to_s
  self.content || ''
end

#transformObject

Transform the contents based on content type.

Returns nothing



33
34
35
36
37
# File 'lib/jekyll/convertible.rb', line 33

def transform
  return if %w(html xml atom rss).include?(self.content_type) # no transformation needed
  self.content = Engines.method(self.content_type.to_sym).call(self.content)
  self.ext = ".html"
end