From bf149a0b97187259cccb5323d3396f313b3461b0 Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Mon, 9 Feb 2015 21:42:50 -0800 Subject: [PATCH] highlight: fix problem with linenos and rouge. Found by @EdMcBane in https://github.com/jekyll/jekyll/pull/3435 The strange regexp we were doing to replace the
bits in the Pygments output were wreaking havoc on Rouge output because Rouge uses
's to wrap line numbers.

To be consistent, the output from render_* should *not* include
the wrapping 
and
 tags. It should just be what was
inside. We can then wrap it in our own custom tags without using
any regular expressions, as God intended. Death to regular
expressions and HTML manipulation!
---
 lib/jekyll/tags/highlight.rb | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)

diff --git a/lib/jekyll/tags/highlight.rb b/lib/jekyll/tags/highlight.rb
index 5af5f2bd..5589d38f 100644
--- a/lib/jekyll/tags/highlight.rb
+++ b/lib/jekyll/tags/highlight.rb
@@ -77,8 +77,6 @@ eos
       def render_pygments(code, is_safe)
         Jekyll::External.require_with_graceful_fail('pygments')
 
-        @options[:encoding] = 'utf-8'
-
         highlighted_code = Pygments.highlight(
           code,
           :lexer   => @lang,
@@ -96,26 +94,26 @@ eos
           raise ArgumentError.new("Pygments.rb returned an unacceptable value when attempting to highlight some code.")
         end
 
-        highlighted_code
+        highlighted_code.sub('
', '').sub('
', '') end def render_rouge(code) Jekyll::External.require_with_graceful_fail('rouge') formatter = Rouge::Formatters::HTML.new(line_numbers: @options[:linenos], wrap: false) lexer = Rouge::Lexer.find_fancy(@lang, code) || Rouge::Lexers::PlainText - code = formatter.format(lexer.lex(code)) - "
#{code}
" + formatter.format(lexer.lex(code)) end def render_codehighlighter(code) - "
#{h(code).strip}
" + h(code).strip end def add_code_tag(code) - # Add nested tags to code blocks - code = code.sub(/
\n*/,'
')
-        code = code.sub(/\n*<\/pre>/,"
") - code.strip + code_attributes = [ + "class=\"language-#{@lang.to_s.gsub('+', '-')}\"", + "data-lang=\"#{@lang.to_s}\"" + ].join(" ") + "
#{code.chomp}
" end end