Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for the preprocess_html flag. #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [109/80]

end
end