From d004bc4ea507189111e7d93f4e64a09815a6e185 Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Wed, 6 Aug 2014 16:20:24 -0400 Subject: [PATCH] Allow Convertibles to be converted by >= 1 converters. --- lib/jekyll/convertible.rb | 33 +++++++++++++++++++++------------ lib/jekyll/plugin.rb | 9 +++++++++ test/test_post.rb | 16 +++++++--------- 3 files changed, 37 insertions(+), 21 deletions(-) diff --git a/lib/jekyll/convertible.rb b/lib/jekyll/convertible.rb index 39b7ca9c..6d8e1417 100644 --- a/lib/jekyll/convertible.rb +++ b/lib/jekyll/convertible.rb @@ -62,11 +62,14 @@ module Jekyll # # Returns nothing. def transform - self.content = converter.convert(content) - rescue => e - Jekyll.logger.error "Conversion error:", "There was an error converting" + - " '#{path}'." - raise e + converters.reduce(content) do |output, converter| + begin + converter.convert output + rescue => e + Jekyll.logger.error "Conversion error:", "#{converter.class} encountered an error converting '#{path}'." + raise e + end + end end # Determine the extension depending on content_type. @@ -74,15 +77,21 @@ module Jekyll # Returns the String extension for the output file. # e.g. ".html" for an HTML output file. 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 # Determine which converter to use based on this convertible's # extension. # # Returns the Converter instance. - def converter - @converter ||= site.converters.find { |c| c.matches(ext) } + def converters + @converters ||= site.converters.select { |c| c.matches(ext) }.sort end # Render Liquid in the content @@ -159,7 +168,7 @@ module Jekyll # # Returns true if the layout is invalid, false if otherwise 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 # Recursively render layouts @@ -205,11 +214,11 @@ module Jekyll info = { :filters => [Jekyll::Filters], :registers => { :site => site, :page => payload['page'] } } # render and transform content (this becomes the final content of the object) - payload["highlighter_prefix"] = converter.highlighter_prefix - payload["highlighter_suffix"] = converter.highlighter_suffix + payload["highlighter_prefix"] = converters.first.highlighter_prefix + payload["highlighter_suffix"] = converters.first.highlighter_suffix self.content = render_liquid(content, payload, info) if render_with_liquid? - transform + self.content = transform # output keeps track of what will finally be written self.output = content diff --git a/lib/jekyll/plugin.rb b/lib/jekyll/plugin.rb index 0e5c9bea..0207314c 100644 --- a/lib/jekyll/plugin.rb +++ b/lib/jekyll/plugin.rb @@ -56,6 +56,15 @@ module Jekyll PRIORITIES[other.priority] <=> PRIORITIES[self.priority] 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. # # config - The Hash of configuration options. diff --git a/test/test_post.rb b/test/test_post.rb index bf9b741c..78764efe 100644 --- a/test/test_post.rb +++ b/test/test_post.rb @@ -342,9 +342,7 @@ class TestPost < Test::Unit::TestCase should "transform textile" do @post.process(@real_file) @post.read_yaml(@source, @real_file) - @post.transform - - assert_equal "

{{ page.title }}

\n

Best post ever

", @post.content + assert_equal "

{{ page.title }}

\n

Best post ever

", @post.transform end context "#excerpt" do @@ -642,41 +640,41 @@ class TestPost < Test::Unit::TestCase should "process .md as markdown under default configuration" do post = setup_post '2011-04-12-md-extension.md' - conv = post.converter + conv = post.converters.first assert conv.kind_of? Jekyll::Converters::Markdown end should "process .text as identity under default configuration" do post = setup_post '2011-04-12-text-extension.text' - conv = post.converter + conv = post.converters.first assert conv.kind_of? Jekyll::Converters::Identity end should "process .text as markdown under alternate configuration" do @site.config['markdown_ext'] = 'markdown,mdw,mdwn,md,text' post = setup_post '2011-04-12-text-extension.text' - conv = post.converter + conv = post.converters.first assert conv.kind_of? Jekyll::Converters::Markdown end should "process .md as markdown under alternate configuration" do @site.config['markdown_ext'] = 'markdown,mkd,mkdn,md,text' post = setup_post '2011-04-12-text-extension.text' - conv = post.converter + conv = post.converters.first assert conv.kind_of? Jekyll::Converters::Markdown end should "process .mkdn under text if it is not in the markdown config" do @site.config['markdown_ext'] = 'markdown,mkd,md,text' post = setup_post '2013-08-01-mkdn-extension.mkdn' - conv = post.converter + conv = post.converters.first assert conv.kind_of? Jekyll::Converters::Identity end should "process .text as textile under alternate configuration" do @site.config['textile_ext'] = 'textile,text' post = setup_post '2011-04-12-text-extension.text' - conv = post.converter + conv = post.converters.first assert conv.kind_of? Jekyll::Converters::Textile end