Skip to content

Commit

Permalink
Add support for the preprocess_html flag.
Browse files Browse the repository at this point in the history
This adds support for replacing consecutive spaces/nbsp to be converted to plain spaces for use with the Rouge lexer.
  • Loading branch information
lowjoel committed May 30, 2016
1 parent 09f767f commit 12fa088
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
20 changes: 16 additions & 4 deletions lib/html/pipeline/rouge_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ def call
default = must_str(context[:highlight])
next unless lang = node["lang"] || default
next unless lexer = lexer_for(lang)
node.css("br").each { |br| br.replace("\n") } if replace_br
text = node.inner_text
text = preprocess_html(node)
html = highlight_with(lexer, text)
next if html.nil?

Expand All @@ -28,6 +27,15 @@ def call
doc
end

def preprocess_html(node)
node.css("br").each { |br| br.replace("\n") } if replace_br?
result = node.inner_text
return result unless preprocess_html?

result.tr!("\u00A0", ' ')
result
end

def highlight_with(lexer, text)
formatter.format(lexer.lex(text))
end
Expand All @@ -40,8 +48,12 @@ def line_numbers
context[:line_numbers] || false
end

def replace_br
context[:replace_br] || false
def replace_br?
context[:replace_br] || preprocess_html?
end

def preprocess_html?
context[:preprocess_html] || false
end

def formatter(css_class: default_css_class)
Expand Down
10 changes: 10 additions & 0 deletions test/rouge_filter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ def test_replacing_br

doc = filter.call
assert_equal "<pre class=\"highlight highlight-ruby\"><code>"\
"<span class=\"n\">hello</span>\n"\
"<span class=\"n\">world</span></code></pre>\n", doc.to_html
end

def test_preprocess_html
filter = RougeFilter.new \
"<pre lang='ruby'> &nbsp;hello<br>world</pre>", preprocess_html: true

doc = filter.call
assert_equal "<pre class=\"highlight highlight-ruby\"><code> "\
"<span class=\"n\">hello</span>\n<span class=\"n\">world</span></code></pre>\n", doc.to_html
end
end

0 comments on commit 12fa088

Please sign in to comment.