Skip to content

Commit e43d396

Browse files
committed
Add support for the preprocess_html flag.
This adds support for replacing consecutive spaces/nbsp to be converted to plain spaces for use with the Rouge lexer.
1 parent 09f767f commit e43d396

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

lib/html/pipeline/rouge_filter.rb

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ def call
1313
default = must_str(context[:highlight])
1414
next unless lang = node["lang"] || default
1515
next unless lexer = lexer_for(lang)
16-
node.css("br").each { |br| br.replace("\n") } if replace_br
17-
text = node.inner_text
16+
text = preprocess_html(node)
1817
html = highlight_with(lexer, text)
1918
next if html.nil?
2019

@@ -28,6 +27,15 @@ def call
2827
doc
2928
end
3029

30+
def preprocess_html(node)
31+
node.css("br").each { |br| br.replace("\n") } if replace_br?
32+
result = node.inner_text
33+
return result unless preprocess_html?
34+
35+
result.gsub!("\u00A0", ' ')
36+
result
37+
end
38+
3139
def highlight_with(lexer, text)
3240
formatter.format(lexer.lex(text))
3341
end
@@ -40,8 +48,12 @@ def line_numbers
4048
context[:line_numbers] || false
4149
end
4250

43-
def replace_br
44-
context[:replace_br] || false
51+
def replace_br?
52+
context[:replace_br] || preprocess_html?
53+
end
54+
55+
def preprocess_html?
56+
context[:preprocess_html] || false
4557
end
4658

4759
def formatter(css_class: default_css_class)

test/rouge_filter_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,13 @@ def test_replacing_br
9797
assert_equal "<pre class=\"highlight highlight-ruby\"><code>"\
9898
"<span class=\"n\">hello</span>\n<span class=\"n\">world</span></code></pre>\n", doc.to_html
9999
end
100+
101+
def test_preprocess_html
102+
filter = RougeFilter.new \
103+
"<pre lang='ruby'> &nbsp;hello<br>world</pre>", preprocess_html: true
104+
105+
doc = filter.call
106+
assert_equal "<pre class=\"highlight highlight-ruby\"><code> "\
107+
"<span class=\"n\">hello</span>\n<span class=\"n\">world</span></code></pre>\n", doc.to_html
108+
end
100109
end

0 commit comments

Comments
 (0)