[WIP] Add shim that works for both Rouge 1 and Rouge 2 (#5919)

Merge pull request 5919
This commit is contained in:
Parker Moore 2017-08-17 11:16:18 -04:00 committed by jekyllbot
parent 6b8de2a757
commit 4c15b9e5e4
9 changed files with 105 additions and 15 deletions

View File

@ -13,6 +13,8 @@ rvm:
matrix:
include:
- rvm: *ruby1
env: TEST_SUITE=test ROUGE=1.11.1
- rvm: *ruby1
env: TEST_SUITE=fmt
- rvm: *ruby1

View File

@ -5,6 +5,8 @@ gemspec :name => "jekyll"
gem "rake", "~> 12.0"
gem "rouge", ENV["ROUGE"] if ENV["ROUGE"]
# Dependency of jekyll-mentions. RubyGems in Ruby 2.1 doesn't shield us from this.
gem "activesupport", "~> 4.2", :groups => [:test_legacy, :site] if RUBY_VERSION < "2.2.2"
@ -71,7 +73,7 @@ group :jekyll_optional_dependencies do
gem "jekyll-gist"
gem "jekyll-paginate"
gem "jekyll-redirect-from"
gem "kramdown", "~> 1.9"
gem "kramdown", "~> 1.14"
gem "mime-types", "~> 3.0"
gem "rdoc", "~> 5.0"
gem "toml", "~> 0.1.0"

View File

@ -35,10 +35,11 @@ Gem::Specification.new do |s|
s.add_runtime_dependency("colorator", "~> 1.0")
s.add_runtime_dependency("jekyll-sass-converter", "~> 1.0")
s.add_runtime_dependency("jekyll-watch", "~> 1.1")
s.add_runtime_dependency("kramdown", "~> 1.3")
s.add_runtime_dependency("kramdown", "~> 1.14")
s.add_runtime_dependency("liquid", "~> 4.0")
s.add_runtime_dependency("mercenary", "~> 0.3.3")
s.add_runtime_dependency("pathutil", "~> 0.9")
s.add_runtime_dependency("rouge", "~> #{ENV["ROUGE_VERSION"] || "1.7"}")
rouge_versions = ENV["ROUGE_VERSION"] ? ["~> #{ENV["ROUGE_VERSION"]}"] : [">= 1.7", "< 3"]
s.add_runtime_dependency("rouge", *rouge_versions)
s.add_runtime_dependency("safe_yaml", "~> 1.0")
end

View File

@ -55,7 +55,7 @@ class Jekyll::Converters::Markdown::RedcarpetParser
protected
def rouge_formatter(_lexer)
Rouge::Formatters::HTML.new(:wrap => false)
Jekyll::Utils::Rouge.html_formatter(:wrap => false)
end
end

View File

@ -111,10 +111,12 @@ eos
end
def render_rouge(code)
Jekyll::External.require_with_graceful_fail("rouge")
formatter = Rouge::Formatters::HTML.new(
formatter = Jekyll::Utils::Rouge.html_formatter(
:line_numbers => @highlight_options[:linenos],
:wrap => false
:wrap => false,
:css_class => "highlight",
:gutter_class => "gutter",
:code_class => "code"
)
lexer = Rouge::Lexer.find_fancy(@lang, code) || Rouge::Lexers::PlainText
formatter.format(lexer.lex(code))

View File

@ -7,6 +7,7 @@ module Jekyll
autoload :Ansi, "jekyll/utils/ansi"
autoload :Exec, "jekyll/utils/exec"
autoload :Platforms, "jekyll/utils/platforms"
autoload :Rouge, "jekyll/utils/rouge"
autoload :WinTZ, "jekyll/utils/win_tz"
# Constants for use in #slugify

19
lib/jekyll/utils/rouge.rb Normal file
View File

@ -0,0 +1,19 @@
module Jekyll
module Utils
module Rouge
def self.html_formatter(*args)
Jekyll::External.require_with_graceful_fail("rouge")
if old_api?
::Rouge::Formatters::HTML.new(*args)
else
::Rouge::Formatters::HTMLLegacy.new(*args)
end
end
def self.old_api?
::Rouge.version.to_s < "2"
end
end
end
end

View File

@ -17,7 +17,10 @@ class TestKramdown < JekyllUnitTest
"syntax_highlighter" => "rouge",
"syntax_highlighter_opts" => {
"bold_every" => 8, "css" => :class,
"bold_every" => 8,
"css" => :class,
"css_class" => "highlight",
"formatter" => Jekyll::Utils::Rouge.html_formatter.class,
},
},
}
@ -82,8 +85,9 @@ class TestKramdown < JekyllUnitTest
puts "Hello World"
~~~
MARKDOWN
selector = "div.highlighter-rouge>pre.highlight>code"
div_highlight = ""
div_highlight = ">div.highlight" unless Utils::Rouge.old_api?
selector = "div.highlighter-rouge#{div_highlight}>pre.highlight>code"
refute result.css(selector).empty?
end

View File

@ -319,7 +319,20 @@ EOS
)
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">) +
%(<pre class="lineno">1\n</pre></td>) +
%(<td class="code"><pre>test</pre></td></tr>) +
%(</tbody></table>),
@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">) +
@ -331,6 +344,42 @@ EOS
end
end
context "post content has raw tag" do
setup do
content = <<-CONTENT
---
title: This is a test
---
```liquid
{% raw %}
{{ site.baseurl }}{% link _collection/name-of-document.md %}
{% endraw %}
```
CONTENT
create_post(content)
end
should "render markdown with rouge 1" do
skip "Skipped because using a newer version of Rouge" unless Utils::Rouge.old_api?
assert_match(
%(<div class="language-liquid highlighter-rouge"><pre class="highlight"><code>),
@result
)
end
should "render markdown with rouge 2" do
skip "Skipped because using an older version of Rouge" if Utils::Rouge.old_api?
assert_match(
%(<div class="language-liquid highlighter-rouge">) +
%(<div class="highlight"><pre class="highlight"><code>),
@result
)
end
end
context "post content has highlight with file reference" do
setup do
fill_post("./jekyll.gemspec")
@ -418,13 +467,23 @@ This should not be highlighted, right?
EOS
end
should "should stop highlighting at boundary" do
should "should stop highlighting at boundary with rouge 2" do
skip "Skipped because using an older version of Rouge" if Utils::Rouge.old_api?
expected = <<-EOS
<p>This is not yet highlighted</p>
<p>This is not yet highlighted</p>\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
</pre></td><td class="code"><pre><span class="nx">test</span></pre></td></tr></tbody></table></code></pre></figure>\n
<p>This should not be highlighted, right?</p>
EOS
assert_match(expected, @result)
end
should "should stop highlighting at boundary with rouge 1" 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>
</span></pre></td></tr></tbody></table></code></pre></figure>\n
<p>This should not be highlighted, right?</p>
EOS
assert_match(expected, @result)