Allow Convertibles to be converted by >= 1 converters.

This commit is contained in:
Parker Moore 2014-08-06 16:20:24 -04:00
parent b00c5e9587
commit d004bc4ea5
3 changed files with 37 additions and 21 deletions

View File

@ -62,27 +62,36 @@ module Jekyll
# #
# Returns nothing. # Returns nothing.
def transform def transform
self.content = converter.convert(content) converters.reduce(content) do |output, converter|
begin
converter.convert output
rescue => e rescue => e
Jekyll.logger.error "Conversion error:", "There was an error converting" + Jekyll.logger.error "Conversion error:", "#{converter.class} encountered an error converting '#{path}'."
" '#{path}'."
raise e raise e
end end
end
end
# Determine the extension depending on content_type. # Determine the extension depending on content_type.
# #
# Returns the String extension for the output file. # Returns the String extension for the output file.
# e.g. ".html" for an HTML output file. # e.g. ".html" for an HTML output file.
def output_ext def output_ext
converter.output_ext(ext) if converters.all? { |c| c.is_a?(Jekyll::Converters::Identity) }
ext
else
converters.map {|c|
c.output_ext(ext) unless c.is_a?(Jekyll::Converters::Identity)
}.compact.uniq.join("")
end
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. # Returns the Converter instance.
def converter def converters
@converter ||= site.converters.find { |c| c.matches(ext) } @converters ||= site.converters.select { |c| c.matches(ext) }.sort
end end
# Render Liquid in the content # Render Liquid in the content
@ -159,7 +168,7 @@ module Jekyll
# #
# Returns true if the layout is invalid, false if otherwise # Returns true if the layout is invalid, false if otherwise
def invalid_layout?(layout) def invalid_layout?(layout)
!data["layout"].nil? && data["layout"] != "none" && layout.nil? && !(self.is_a? Jekyll::Excerpt) !data["layout"].nil? && layout.nil? && !(self.is_a? Jekyll::Excerpt)
end end
# Recursively render layouts # Recursively render layouts
@ -205,11 +214,11 @@ module Jekyll
info = { :filters => [Jekyll::Filters], :registers => { :site => site, :page => payload['page'] } } info = { :filters => [Jekyll::Filters], :registers => { :site => site, :page => payload['page'] } }
# 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["highlighter_prefix"] = converter.highlighter_prefix payload["highlighter_prefix"] = converters.first.highlighter_prefix
payload["highlighter_suffix"] = converter.highlighter_suffix payload["highlighter_suffix"] = converters.first.highlighter_suffix
self.content = render_liquid(content, payload, info) if render_with_liquid? self.content = render_liquid(content, payload, info) if render_with_liquid?
transform self.content = transform
# output keeps track of what will finally be written # output keeps track of what will finally be written
self.output = content self.output = content

View File

@ -56,6 +56,15 @@ module Jekyll
PRIORITIES[other.priority] <=> PRIORITIES[self.priority] PRIORITIES[other.priority] <=> PRIORITIES[self.priority]
end end
# Spaceship is priority [higher -> lower]
#
# other - The class to be compared.
#
# Returns -1, 0, 1.
def <=>(other)
self.class <=> other.class
end
# Initialize a new plugin. This should be overridden by the subclass. # Initialize a new plugin. This should be overridden by the subclass.
# #
# config - The Hash of configuration options. # config - The Hash of configuration options.

View File

@ -342,9 +342,7 @@ class TestPost < Test::Unit::TestCase
should "transform textile" do should "transform textile" do
@post.process(@real_file) @post.process(@real_file)
@post.read_yaml(@source, @real_file) @post.read_yaml(@source, @real_file)
@post.transform assert_equal "<h1>{{ page.title }}</h1>\n<p>Best <strong>post</strong> ever</p>", @post.transform
assert_equal "<h1>{{ page.title }}</h1>\n<p>Best <strong>post</strong> ever</p>", @post.content
end end
context "#excerpt" do context "#excerpt" do
@ -642,41 +640,41 @@ class TestPost < Test::Unit::TestCase
should "process .md as markdown under default configuration" do should "process .md as markdown under default configuration" do
post = setup_post '2011-04-12-md-extension.md' post = setup_post '2011-04-12-md-extension.md'
conv = post.converter conv = post.converters.first
assert conv.kind_of? Jekyll::Converters::Markdown assert conv.kind_of? Jekyll::Converters::Markdown
end end
should "process .text as identity under default configuration" do should "process .text as identity under default configuration" do
post = setup_post '2011-04-12-text-extension.text' post = setup_post '2011-04-12-text-extension.text'
conv = post.converter conv = post.converters.first
assert conv.kind_of? Jekyll::Converters::Identity assert conv.kind_of? Jekyll::Converters::Identity
end end
should "process .text as markdown under alternate configuration" do should "process .text as markdown under alternate configuration" do
@site.config['markdown_ext'] = 'markdown,mdw,mdwn,md,text' @site.config['markdown_ext'] = 'markdown,mdw,mdwn,md,text'
post = setup_post '2011-04-12-text-extension.text' post = setup_post '2011-04-12-text-extension.text'
conv = post.converter conv = post.converters.first
assert conv.kind_of? Jekyll::Converters::Markdown assert conv.kind_of? Jekyll::Converters::Markdown
end end
should "process .md as markdown under alternate configuration" do should "process .md as markdown under alternate configuration" do
@site.config['markdown_ext'] = 'markdown,mkd,mkdn,md,text' @site.config['markdown_ext'] = 'markdown,mkd,mkdn,md,text'
post = setup_post '2011-04-12-text-extension.text' post = setup_post '2011-04-12-text-extension.text'
conv = post.converter conv = post.converters.first
assert conv.kind_of? Jekyll::Converters::Markdown assert conv.kind_of? Jekyll::Converters::Markdown
end end
should "process .mkdn under text if it is not in the markdown config" do should "process .mkdn under text if it is not in the markdown config" do
@site.config['markdown_ext'] = 'markdown,mkd,md,text' @site.config['markdown_ext'] = 'markdown,mkd,md,text'
post = setup_post '2013-08-01-mkdn-extension.mkdn' post = setup_post '2013-08-01-mkdn-extension.mkdn'
conv = post.converter conv = post.converters.first
assert conv.kind_of? Jekyll::Converters::Identity assert conv.kind_of? Jekyll::Converters::Identity
end end
should "process .text as textile under alternate configuration" do should "process .text as textile under alternate configuration" do
@site.config['textile_ext'] = 'textile,text' @site.config['textile_ext'] = 'textile,text'
post = setup_post '2011-04-12-text-extension.text' post = setup_post '2011-04-12-text-extension.text'
conv = post.converter conv = post.converters.first
assert conv.kind_of? Jekyll::Converters::Textile assert conv.kind_of? Jekyll::Converters::Textile
end end