Skip to content

Commit

Permalink
[WIP] Add shim that works for both Rouge 1 and Rouge 2 (#5919)
Browse files Browse the repository at this point in the history
Merge pull request 5919
  • Loading branch information
parkr authored and jekyllbot committed Aug 17, 2017
1 parent 6b8de2a commit 4c15b9e
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 15 deletions.
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
4 changes: 3 additions & 1 deletion Gemfile
Expand Up @@ -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"

Expand Down Expand Up @@ -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"
Expand Down
5 changes: 3 additions & 2 deletions jekyll.gemspec
Expand Up @@ -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
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 @@ -111,10 +111,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 @@ -7,6 +7,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 @@ -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,
},
},
}
Expand Down Expand Up @@ -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

Expand Down
69 changes: 64 additions & 5 deletions test/test_tags.rb
Expand Up @@ -319,7 +319,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 All @@ -331,6 +344,42 @@ def highlight_block_with_opts(options_string)
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")
Expand Down Expand Up @@ -418,13 +467,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

0 comments on commit 4c15b9e

Please sign in to comment.