Module: Jekyll::Convertible
Instance Method Summary collapse
-
#content_type ⇒ Object
Determine which formatting engine to use based on this convertible’s extension.
-
#do_layout(payload, layouts) ⇒ Object
Add any necessary layouts to this convertible document
layouts
is a Hash of => “layout”site_payload
is the site payload hash. -
#read_yaml(base, name) ⇒ Object
Read the YAML frontmatter
base
is the String path to the dir containing the filename
is the String filename of the file. -
#render_haml_in_context(haml_engine, params = {}) ⇒ Object
Sets up a context for Haml and renders in it.
-
#to_s ⇒ Object
Return the contents as a string.
-
#transform ⇒ Object
Transform the contents based on the file extension.
Instance Method Details
#content_type ⇒ Object
Determine which formatting engine to use based on this convertible’s extension
Returns one of :textile, :markdown, :haml, or :unknown
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/jekyll/convertible.rb', line 68 def content_type case self.ext[1..-1] when /textile/i return 'textile' when /markdown/i, /mkdn/i, /md/i return 'markdown' when /haml/i return 'haml' end return 'unknown' 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
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/jekyll/convertible.rb', line 97 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 if self.content_type == "haml" self.transform self.content = render_haml_in_context(self.content, :site => self.site, :page => ClosedStruct.new(payload["page"])) else self.content = Liquid::Template.parse(self.content).render(payload, info) self.transform end # output keeps track of what will finally be written self.output = self.content if self.is_a? Jekyll::Post payload["page"].merge!({"content" => self.content, "excerpt" => self.data['excerpt'], "remainder" => self.data['remainder']}) end # recursively render layouts layout = layouts[self.data["layout"]] while layout payload = payload.deep_merge({"content" => self.output, "page" => layout.data}) if site.config['haml'] && layout.content.is_a?(Haml::Engine) self.output = render_haml_in_context(layout.content, :site => ClosedStruct.new(payload["site"]), :page => ClosedStruct.new(payload["page"]), :content => payload["content"]) else self.output = Liquid::Template.parse(layout.content).render(payload, info) end 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 29 30 31 32 33 34 35 36 |
# 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 ||= {} if self.is_a? Jekyll::Post divider = /<!--\s*[Mm][Oo][Rr][Ee]\s*-->\s*\n/ if self.content =~ divider self.data['excerpt'], self.data['remainder'] = self.content.split($&, 2) self.content = self.data['excerpt'] + "\n" + '<div id="more"></div>' + "\n" + self.data['remainder'] end end end |
#render_haml_in_context(haml_engine, params = {}) ⇒ Object
Sets up a context for Haml and renders in it. The context has accessors matching the passed-in hash, e.g. “site”, “page” and “content”, and has helper modules mixed in.
Returns String.
85 86 87 88 89 90 |
# File 'lib/jekyll/convertible.rb', line 85 def render_haml_in_context(haml_engine, params={}) context = ClosedStruct.new(params) context.extend(HamlHelpers) context.extend(::Helpers) if defined?(::Helpers) haml_engine.render(context) end |
#to_s ⇒ Object
Return the contents as a string
9 10 11 |
# File 'lib/jekyll/convertible.rb', line 9 def to_s self.content || '' end |
#transform ⇒ Object
Transform the contents based on the file extension.
Returns nothing
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/jekyll/convertible.rb', line 41 def transform case self.content_type when 'textile' self.ext = ".html" self.content = self.site.textile(self.content) if self.is_a? Jekyll::Post and self.data['excerpt'] self.data['excerpt'] = self.site.textile(self.data['excerpt']) self.data['remainder'] = self.site.textile(self.data['remainder']) end when 'markdown' self.ext = ".html" self.content = self.site.markdown(self.content) if self.is_a? Jekyll::Post and self.data['excerpt'] self.data['excerpt'] = self.site.markdown(self.data['excerpt']) self.data['remainder'] = self.site.markdown(self.data['remainder']) end when 'haml' self.ext = ".html" # Actually rendered in do_layout. self.content = Haml::Engine.new(self.content, :attr_wrapper => %{"}) end end |