Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 8 commits into from Aug 17, 2017
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .travis.yml
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions Gemfile
Expand Up @@ -3,6 +3,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"

Expand Down
3 changes: 2 additions & 1 deletion jekyll.gemspec
Expand Up @@ -38,6 +38,7 @@ Gem::Specification.new do |s|
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
2 changes: 1 addition & 1 deletion lib/jekyll/converters/markdown/redcarpet_parser.rb
Expand Up @@ -55,7 +55,7 @@ def block_code(code, lang)

protected
def rouge_formatter(_lexer)
Rouge::Formatters::HTML.new(:wrap => false)
Jekyll::Utils::Rouge.html_formatter(:wrap => false)
end
end

Expand Down
8 changes: 5 additions & 3 deletions lib/jekyll/tags/highlight.rb
Expand Up @@ -109,10 +109,12 @@ def render_pygments(code, is_safe)
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))
Expand Down
1 change: 1 addition & 0 deletions lib/jekyll/utils.rb
Expand Up @@ -5,6 +5,7 @@ module Utils
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
Expand Down
19 changes: 19 additions & 0 deletions lib/jekyll/utils/rouge.rb
@@ -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
10 changes: 7 additions & 3 deletions test/test_kramdown.rb
Expand Up @@ -16,7 +16,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,
},
},
}
Expand Down Expand Up @@ -59,8 +62,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

Expand Down
33 changes: 28 additions & 5 deletions test/test_tags.rb
Expand Up @@ -318,7 +318,20 @@ 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">) +
%(<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">) +
Expand Down Expand Up @@ -417,13 +430,23 @@ def highlight_block_with_opts(options_string)
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)
Expand Down