TomDoc convertible.rb.

This commit is contained in:
Tom Preston-Werner 2011-03-11 16:00:32 -08:00
parent 68eaadd13a
commit 6c94db1486
1 changed files with 20 additions and 15 deletions

View File

@ -10,16 +10,17 @@
# self.output= # self.output=
module Jekyll module Jekyll
module Convertible module Convertible
# Return the contents as a string # Returns the contents as a String.
def to_s def to_s
self.content || '' self.content || ''
end end
# Read the YAML frontmatter # 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 # base - The String path to the dir containing the file.
# name - The String filename of the file.
#
# Returns nothing.
def read_yaml(base, name) def read_yaml(base, name)
self.content = File.read(File.join(base, name)) self.content = File.read(File.join(base, name))
@ -38,42 +39,46 @@ module Jekyll
# Transform the contents based on the content type. # Transform the contents based on the content type.
# #
# Returns nothing # Returns nothing.
def transform def transform
self.content = converter.convert(self.content) self.content = converter.convert(self.content)
end end
# Determine the extension depending on content_type # Determine the extension depending on content_type.
# #
# Returns the extensions for the output file # Returns the String extension for the output file.
# e.g. ".html" for an HTML output file.
def output_ext def output_ext
converter.output_ext(self.ext) converter.output_ext(self.ext)
end end
# Determine which converter to use based on this convertible's # Determine which converter to use based on this convertible's
# extension # extension.
#
# Returns the Converter instance.
def converter def converter
@converter ||= self.site.converters.find { |c| c.matches(self.ext) } @converter ||= self.site.converters.find { |c| c.matches(self.ext) }
end end
# Add any necessary layouts to this convertible document # Add any necessary layouts to this convertible document.
# +layouts+ is a Hash of {"name" => "layout"}
# +site_payload+ is the site payload hash
# #
# Returns nothing # payload - The site payload Hash.
# layouts - A Hash of {"name" => "layout"}.
#
# Returns nothing.
def do_layout(payload, layouts) def do_layout(payload, layouts)
info = { :filters => [Jekyll::Filters], :registers => { :site => self.site } } info = { :filters => [Jekyll::Filters], :registers => { :site => self.site } }
# render and transform content (this becomes the final content of the object) # render and transform content (this becomes the final content of the object)
payload["pygments_prefix"] = converter.pygments_prefix payload["pygments_prefix"] = converter.pygments_prefix
payload["pygments_suffix"] = converter.pygments_suffix payload["pygments_suffix"] = converter.pygments_suffix
begin begin
self.content = Liquid::Template.parse(self.content).render(payload, info) self.content = Liquid::Template.parse(self.content).render(payload, info)
rescue => e rescue => e
puts "Liquid Exception: #{e.message} in #{self.data["layout"]}" puts "Liquid Exception: #{e.message} in #{self.data["layout"]}"
end end
self.transform self.transform
# output keeps track of what will finally be written # output keeps track of what will finally be written