Merge pull request #3436 from jekyll/fix-highlight-madness
This commit is contained in:
commit
b81f6ed38c
|
@ -77,8 +77,6 @@ eos
|
||||||
def render_pygments(code, is_safe)
|
def render_pygments(code, is_safe)
|
||||||
Jekyll::External.require_with_graceful_fail('pygments')
|
Jekyll::External.require_with_graceful_fail('pygments')
|
||||||
|
|
||||||
@options[:encoding] = 'utf-8'
|
|
||||||
|
|
||||||
highlighted_code = Pygments.highlight(
|
highlighted_code = Pygments.highlight(
|
||||||
code,
|
code,
|
||||||
:lexer => @lang,
|
:lexer => @lang,
|
||||||
|
@ -96,26 +94,26 @@ eos
|
||||||
raise ArgumentError.new("Pygments.rb returned an unacceptable value when attempting to highlight some code.")
|
raise ArgumentError.new("Pygments.rb returned an unacceptable value when attempting to highlight some code.")
|
||||||
end
|
end
|
||||||
|
|
||||||
highlighted_code
|
highlighted_code.sub('<div class="highlight"><pre>', '').sub('</pre></div>', '')
|
||||||
end
|
end
|
||||||
|
|
||||||
def render_rouge(code)
|
def render_rouge(code)
|
||||||
Jekyll::External.require_with_graceful_fail('rouge')
|
Jekyll::External.require_with_graceful_fail('rouge')
|
||||||
formatter = Rouge::Formatters::HTML.new(line_numbers: @options[:linenos], wrap: false)
|
formatter = Rouge::Formatters::HTML.new(line_numbers: @options[:linenos], wrap: false)
|
||||||
lexer = Rouge::Lexer.find_fancy(@lang, code) || Rouge::Lexers::PlainText
|
lexer = Rouge::Lexer.find_fancy(@lang, code) || Rouge::Lexers::PlainText
|
||||||
code = formatter.format(lexer.lex(code))
|
formatter.format(lexer.lex(code))
|
||||||
"<div class=\"highlight\"><pre>#{code}</pre></div>"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def render_codehighlighter(code)
|
def render_codehighlighter(code)
|
||||||
"<div class=\"highlight\"><pre>#{h(code).strip}</pre></div>"
|
h(code).strip
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_code_tag(code)
|
def add_code_tag(code)
|
||||||
# Add nested <code> tags to code blocks
|
code_attributes = [
|
||||||
code = code.sub(/<pre>\n*/,'<pre><code class="language-' + @lang.to_s.gsub("+", "-") + '" data-lang="' + @lang.to_s + '">')
|
"class=\"language-#{@lang.to_s.gsub('+', '-')}\"",
|
||||||
code = code.sub(/\n*<\/pre>/,"</code></pre>")
|
"data-lang=\"#{@lang.to_s}\""
|
||||||
code.strip
|
].join(" ")
|
||||||
|
"<div class=\"highlight\"><pre><code #{code_attributes}>#{code.chomp}</code></pre></div>"
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -7,7 +7,7 @@ class TestTags < Test::Unit::TestCase
|
||||||
def create_post(content, override = {}, converter_class = Jekyll::Converters::Markdown)
|
def create_post(content, override = {}, converter_class = Jekyll::Converters::Markdown)
|
||||||
stub(Jekyll).configuration do
|
stub(Jekyll).configuration do
|
||||||
site_configuration({
|
site_configuration({
|
||||||
"highlighter" => "pygments"
|
"highlighter" => "rouge"
|
||||||
}.merge(override))
|
}.merge(override))
|
||||||
end
|
end
|
||||||
site = Site.new(Jekyll.configuration)
|
site = Site.new(Jekyll.configuration)
|
||||||
|
@ -136,63 +136,65 @@ CONTENT
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "post content has highlight tag" do
|
context "with the pygments highlighter" do
|
||||||
setup do
|
context "post content has highlight tag" do
|
||||||
fill_post("test")
|
setup do
|
||||||
|
fill_post("test", {'highlighter' => 'pygments'})
|
||||||
|
end
|
||||||
|
|
||||||
|
should "not cause a markdown error" do
|
||||||
|
assert_no_match /markdown\-html\-error/, @result
|
||||||
|
end
|
||||||
|
|
||||||
|
should "render markdown with pygments" do
|
||||||
|
assert_match %{<pre><code class="language-text" data-lang="text">test</code></pre>}, @result
|
||||||
|
end
|
||||||
|
|
||||||
|
should "render markdown with pygments with line numbers" do
|
||||||
|
assert_match %{<pre><code class="language-text" data-lang="text"><span class="lineno">1</span> test</code></pre>}, @result
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
should "not cause a markdown error" do
|
context "post content has highlight with file reference" do
|
||||||
assert_no_match /markdown\-html\-error/, @result
|
setup do
|
||||||
|
fill_post("./jekyll.gemspec", {'highlighter' => 'pygments'})
|
||||||
|
end
|
||||||
|
|
||||||
|
should "not embed the file" do
|
||||||
|
assert_match %{<pre><code class="language-text" data-lang="text">./jekyll.gemspec</code></pre>}, @result
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
should "render markdown with pygments" do
|
context "post content has highlight tag with UTF character" do
|
||||||
assert_match %{<pre><code class="language-text" data-lang="text">test</code></pre>}, @result
|
setup do
|
||||||
|
fill_post("Æ", {'highlighter' => 'pygments'})
|
||||||
|
end
|
||||||
|
|
||||||
|
should "render markdown with pygments line handling" do
|
||||||
|
assert_match %{<pre><code class="language-text" data-lang="text">Æ</code></pre>}, @result
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
should "render markdown with pygments with line numbers" do
|
context "post content has highlight tag with preceding spaces & lines" do
|
||||||
assert_match %{<pre><code class="language-text" data-lang="text"><span class="lineno">1</span> test</code></pre>}, @result
|
setup do
|
||||||
end
|
code = <<-EOS
|
||||||
end
|
|
||||||
|
|
||||||
context "post content has highlight with file reference" do
|
|
||||||
setup do
|
|
||||||
fill_post("./jekyll.gemspec")
|
|
||||||
end
|
|
||||||
|
|
||||||
should "not embed the file" do
|
|
||||||
assert_match %{<pre><code class="language-text" data-lang="text">./jekyll.gemspec</code></pre>}, @result
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context "post content has highlight tag with UTF character" do
|
|
||||||
setup do
|
|
||||||
fill_post("Æ")
|
|
||||||
end
|
|
||||||
|
|
||||||
should "render markdown with pygments line handling" do
|
|
||||||
assert_match %{<pre><code class="language-text" data-lang="text">Æ</code></pre>}, @result
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context "post content has highlight tag with preceding spaces & lines" do
|
|
||||||
setup do
|
|
||||||
fill_post <<-EOS
|
|
||||||
|
|
||||||
|
|
||||||
[,1] [,2]
|
[,1] [,2]
|
||||||
[1,] FALSE TRUE
|
[1,] FALSE TRUE
|
||||||
[2,] FALSE TRUE
|
[2,] FALSE TRUE
|
||||||
EOS
|
EOS
|
||||||
|
fill_post(code, {'highlighter' => 'pygments'})
|
||||||
|
end
|
||||||
|
|
||||||
|
should "only strip the preceding newlines" do
|
||||||
|
assert_match %{<pre><code class=\"language-text\" data-lang=\"text\"> [,1] [,2]}, @result
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
should "only strip the preceding newlines" do
|
context "post content has highlight tag with preceding spaces & lines in several places" do
|
||||||
assert_match %{<pre><code class=\"language-text\" data-lang=\"text\"> [,1] [,2]}, @result
|
setup do
|
||||||
end
|
code = <<-EOS
|
||||||
end
|
|
||||||
|
|
||||||
context "post content has highlight tag with preceding spaces & lines in several places" do
|
|
||||||
setup do
|
|
||||||
fill_post <<-EOS
|
|
||||||
|
|
||||||
|
|
||||||
[,1] [,2]
|
[,1] [,2]
|
||||||
|
@ -203,34 +205,133 @@ EOS
|
||||||
|
|
||||||
|
|
||||||
EOS
|
EOS
|
||||||
|
fill_post(code, {'highlighter' => 'pygments'})
|
||||||
|
end
|
||||||
|
|
||||||
|
should "only strip the newlines which precede and succeed the entire block" do
|
||||||
|
assert_match "<pre><code class=\"language-text\" data-lang=\"text\"> [,1] [,2]\n\n\n[1,] FALSE TRUE\n[2,] FALSE TRUE</code></pre>", @result
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
should "only strip the newlines which precede and succeed the entire block" do
|
context "post content has highlight tag with preceding spaces & Windows-style newlines" do
|
||||||
assert_match "<pre><code class=\"language-text\" data-lang=\"text\"> [,1] [,2]\n\n\n[1,] FALSE TRUE\n[2,] FALSE TRUE</code></pre>", @result
|
setup do
|
||||||
end
|
fill_post "\r\n\r\n\r\n [,1] [,2]", {'highlighter' => 'pygments'}
|
||||||
end
|
end
|
||||||
|
|
||||||
context "post content has highlight tag with preceding spaces & Windows-style newlines" do
|
should "only strip the preceding newlines" do
|
||||||
setup do
|
assert_match %{<pre><code class=\"language-text\" data-lang=\"text\"> [,1] [,2]}, @result
|
||||||
fill_post "\r\n\r\n\r\n [,1] [,2]"
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
should "only strip the preceding newlines" do
|
context "post content has highlight tag with only preceding spaces" do
|
||||||
assert_match %{<pre><code class=\"language-text\" data-lang=\"text\"> [,1] [,2]}, @result
|
setup do
|
||||||
end
|
code = <<-EOS
|
||||||
end
|
|
||||||
|
|
||||||
context "post content has highlight tag with only preceding spaces" do
|
|
||||||
setup do
|
|
||||||
fill_post <<-EOS
|
|
||||||
[,1] [,2]
|
[,1] [,2]
|
||||||
[1,] FALSE TRUE
|
[1,] FALSE TRUE
|
||||||
[2,] FALSE TRUE
|
[2,] FALSE TRUE
|
||||||
EOS
|
EOS
|
||||||
|
fill_post(code, {'highlighter' => 'pygments'})
|
||||||
|
end
|
||||||
|
|
||||||
|
should "only strip the preceding newlines" do
|
||||||
|
assert_match %{<pre><code class=\"language-text\" data-lang=\"text\"> [,1] [,2]}, @result
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "with the rouge highlighter" do
|
||||||
|
context "post content has highlight tag" do
|
||||||
|
setup do
|
||||||
|
fill_post("test")
|
||||||
|
end
|
||||||
|
|
||||||
|
should "render markdown with rouge" do
|
||||||
|
assert_match %{<pre><code class="language-text" data-lang="text">test</code></pre>}, @result
|
||||||
|
end
|
||||||
|
|
||||||
|
should "render markdown with rouge with line numbers" do
|
||||||
|
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
|
end
|
||||||
|
|
||||||
should "only strip the preceding newlines" do
|
context "post content has highlight with file reference" do
|
||||||
assert_match %{<pre><code class=\"language-text\" data-lang=\"text\"> [,1] [,2]}, @result
|
setup do
|
||||||
|
fill_post("./jekyll.gemspec")
|
||||||
|
end
|
||||||
|
|
||||||
|
should "not embed the file" do
|
||||||
|
assert_match %{<pre><code class="language-text" data-lang="text">./jekyll.gemspec</code></pre>}, @result
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "post content has highlight tag with UTF character" do
|
||||||
|
setup do
|
||||||
|
fill_post("Æ")
|
||||||
|
end
|
||||||
|
|
||||||
|
should "render markdown with pygments line handling" do
|
||||||
|
assert_match %{<pre><code class="language-text" data-lang="text">Æ</code></pre>}, @result
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "post content has highlight tag with preceding spaces & lines" do
|
||||||
|
setup do
|
||||||
|
fill_post <<-EOS
|
||||||
|
|
||||||
|
|
||||||
|
[,1] [,2]
|
||||||
|
[1,] FALSE TRUE
|
||||||
|
[2,] FALSE TRUE
|
||||||
|
EOS
|
||||||
|
end
|
||||||
|
|
||||||
|
should "only strip the preceding newlines" do
|
||||||
|
assert_match %{<pre><code class=\"language-text\" data-lang=\"text\"> [,1] [,2]}, @result
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "post content has highlight tag with preceding spaces & lines in several places" do
|
||||||
|
setup do
|
||||||
|
fill_post <<-EOS
|
||||||
|
|
||||||
|
|
||||||
|
[,1] [,2]
|
||||||
|
|
||||||
|
|
||||||
|
[1,] FALSE TRUE
|
||||||
|
[2,] FALSE TRUE
|
||||||
|
|
||||||
|
|
||||||
|
EOS
|
||||||
|
end
|
||||||
|
|
||||||
|
should "only strip the newlines which precede and succeed the entire block" do
|
||||||
|
assert_match "<pre><code class=\"language-text\" data-lang=\"text\"> [,1] [,2]\n\n\n[1,] FALSE TRUE\n[2,] FALSE TRUE</code></pre>", @result
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "post content has highlight tag with preceding spaces & Windows-style newlines" do
|
||||||
|
setup do
|
||||||
|
fill_post "\r\n\r\n\r\n [,1] [,2]"
|
||||||
|
end
|
||||||
|
|
||||||
|
should "only strip the preceding newlines" do
|
||||||
|
assert_match %{<pre><code class=\"language-text\" data-lang=\"text\"> [,1] [,2]}, @result
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "post content has highlight tag with only preceding spaces" do
|
||||||
|
setup do
|
||||||
|
fill_post <<-EOS
|
||||||
|
[,1] [,2]
|
||||||
|
[1,] FALSE TRUE
|
||||||
|
[2,] FALSE TRUE
|
||||||
|
EOS
|
||||||
|
end
|
||||||
|
|
||||||
|
should "only strip the preceding newlines" do
|
||||||
|
assert_match %{<pre><code class=\"language-text\" data-lang=\"text\"> [,1] [,2]}, @result
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue