Skip to content

Commit

Permalink
WIP: Create shim that works for both Rouge 1 and Rouge 2
Browse files Browse the repository at this point in the history
  • Loading branch information
parkr committed Mar 2, 2017
1 parent 1f8338a commit 3bc8e49
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 31 deletions.
2 changes: 1 addition & 1 deletion lib/jekyll/converters/markdown/redcarpet_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def block_code(code, lang)

protected
def rouge_formatter(_lexer)
Jekyll::Utils::RougeFormatter.html(:wrap => false)
Jekyll::Utils::Rouge.html_formatter(:wrap => false)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/jekyll/tags/highlight.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def render_pygments(code, is_safe)
end

def render_rouge(code)
formatter = Jekyll::Utils::RougeFormatter.html(
formatter = Jekyll::Utils::Rouge.html_formatter(
:line_numbers => @highlight_options[:linenos],
:wrap => false,
:css_class => "highlight",
Expand Down
2 changes: 1 addition & 1 deletion lib/jekyll/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Utils
autoload :Ansi, "jekyll/utils/ansi"
autoload :Exec, "jekyll/utils/exec"
autoload :Platforms, "jekyll/utils/platforms"
autoload :RougeFormatter, "jekyll/utils/rouge_formatter"
autoload :Rouge, "jekyll/utils/rouge"
autoload :WinTZ, "jekyll/utils/win_tz"

# Constants for use in #slugify
Expand Down
19 changes: 19 additions & 0 deletions lib/jekyll/utils/rouge.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Jekyll
module Utils
module Rouge

def self.html_formatter(*args)
Jekyll::External.require_with_graceful_fail("rouge")
html_formatter = ::Rouge::Formatters::HTML.new(*args)
return html_formatter if old_api?

::Rouge::Formatters::HTMLPygments.new(html_formatter)
end

def self.old_api?
::Rouge.version.to_s < "2"
end

end
end
end
16 changes: 0 additions & 16 deletions lib/jekyll/utils/rouge_formatter.rb

This file was deleted.

2 changes: 1 addition & 1 deletion test/test_kramdown.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class TestKramdown < JekyllUnitTest
"bold_every" => 8,
"css" => :class,
"css_class" => "highlight",
"formatter" => Jekyll::Utils::RougeFormatter.html.class,
"formatter" => Jekyll::Utils::Rouge.html_formatter.class,
},
},
}
Expand Down
45 changes: 34 additions & 11 deletions test/test_tags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,8 @@ def highlight_block_with_opts(options_string)
)
end

should "render markdown with rouge with line numbers" do
should "render markdown with rouge 2 with line numbers" do
skip "Skipped because using an older version of Rouge" if Utils::Rouge.old_api?
assert_match(
%(<table class="rouge-table"><tbody>) +
%(<tr><td class="gutter gl">) +
Expand All @@ -327,6 +328,18 @@ def highlight_block_with_opts(options_string)
@result
)
end

should "render markdown with rouge 1 with line numbers" do
skip "Skipped because using a newer version of Rouge" unless Utils::Rouge.old_api?
assert_match(
%(<table style="border-spacing: 0"><tbody>) +
%(<tr><td class="gutter gl" style="text-align: right">) +
%(<pre class="lineno">1</pre></td>) +
%(<td class="code"><pre>test<span class="w">\n</span></pre></td></tr>) +
%(</tbody></table>),
@result
)
end
end

context "post content has highlight with file reference" do
Expand Down Expand Up @@ -416,16 +429,26 @@ def highlight_block_with_opts(options_string)
EOS
end

should "should stop highlighting at boundary" do
assert_match(
%(<p>This is not yet highlighted</p>\n\n<figure class="highlight">) +
%(<pre><code class="language-php" data-lang="php">) +
%(<table class="rouge-table"><tbody><tr><td class="gutter gl">) +
%(<pre class="lineno">1\n</pre></td><td class="code"><pre>test</pre></td>) +
%(</tr></tbody></table></code></pre></figure>\n\n) +
%(<p>This should not be highlighted, right?</p>),
@result
)
should "should stop highlighting at boundary with rouge 1" do
skip "Skipped because using an older version of Rouge" if Utils::Rouge.old_api?
expected = <<-EOS
<p>This is not yet highlighted</p>
<figure class="highlight"><pre><code class="language-php" data-lang="php"><table style="border-spacing: 0"><tbody><tr><td class="gutter gl" style="text-align: right"><pre class="lineno">1</pre></td><td class="code"><pre>test<span class="w">
</span></pre></td></tr></tbody></table></code></pre></figure>
<p>This should not be highlighted, right?</p>
EOS
assert_match(expected, @result)
end

should "should stop highlighting at boundary with rouge 2" do
skip "Skipped because using a newer version of Rouge" unless Utils::Rouge.old_api?
expected = <<-EOS
<p>This is not yet highlighted</p>\n
<figure class="highlight"><pre><code class="language-php" data-lang="php"><table style="border-spacing: 0"><tbody><tr><td class="gutter gl" style="text-align: right"><pre class="lineno">1</pre></td><td class="code"><pre>test<span class="w">
</span></pre></td></tr></tbody></table></code></pre></figure>\n
<p>This should not be highlighted, right?</p>
EOS
assert_match(expected, @result)
end
end

Expand Down

0 comments on commit 3bc8e49

Please sign in to comment.