File tree Expand file tree Collapse file tree 2 files changed +25
-4
lines changed Expand file tree Collapse file tree 2 files changed +25
-4
lines changed Original file line number Diff line number Diff 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 )
Original file line number Diff line number Diff 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'> 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
100109end
You can’t perform that action at this time.
0 commit comments