Skip to content

Commit

Permalink
Avoid duplicated output using highlight tags
Browse files Browse the repository at this point in the history
While using Rouge and an `highlight` tag, the output was duplicated
since the `output` variable in the Liquid tag definition was equal to
the highlighter's prefix value and the `<<` method changes its receiver.

Therefore, we should simply define an empty string and append the prefix
if it is present.
  • Loading branch information
robin850 committed Apr 27, 2014
1 parent cdeaa15 commit 3a61088
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
9 changes: 9 additions & 0 deletions features/site_configuration.feature
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,15 @@ Feature: Site configuration
And I should see "Hello world!" in "_site/index.html"
And I should see "class=\"highlight\"" in "_site/index.html"

Scenario: Rouge renders code block once
Given I have a configuration file with "highlighter" set to "rouge"
And I have a _posts directory
And I have the following post:
| title | date | layout | content |
| foo | 2014-04-27 11:34 | default | {% highlight text %} test {% endhighlight %} |
When I run jekyll build
Then I should not see "highlight(.*)highlight" in "_site/2014/04/27/foo.html"

Scenario: Set time and no future dated posts
Given I have a _layouts directory
And I have a page layout that contains "Page Layout: {{ site.posts.size }} on {{ site.time | date: "%Y-%m-%d" }}"
Expand Down
4 changes: 2 additions & 2 deletions features/step_definitions/jekyll_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,15 @@ def file_content_from_hash(input_hash)
end

Then /^I should see "(.*)" in "(.*)"$/ do |text, file|
assert_match Regexp.new(text), file_contents(file)
assert_match Regexp.new(text, Regexp::MULTILINE), file_contents(file)
end

Then /^I should see exactly "(.*)" in "(.*)"$/ do |text, file|
assert_equal text, file_contents(file).strip
end

Then /^I should not see "(.*)" in "(.*)"$/ do |text, file|
assert_no_match Regexp.new(text), file_contents(file)
assert_no_match Regexp.new(text, Regexp::MULTILINE), file_contents(file)
end

Then /^I should see escaped "(.*)" in "(.*)"$/ do |text, file|
Expand Down
3 changes: 2 additions & 1 deletion lib/jekyll/tags/highlight.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,9 @@ def render_rouge(context, code)
formatter = Rouge::Formatters::HTML.new(line_numbers: linenos, wrap: false)

pre = "<pre>#{formatter.format(lexer.lex(code))}</pre>"
output = ""

output = context["highlighter_prefix"] || ""
output << context["highlighter_prefix"] if context["highlighter_prefix"]
output << "<div class=\"highlight\">"
output << add_code_tags(pre, @lang)
output << "</div>"
Expand Down

0 comments on commit 3a61088

Please sign in to comment.