Class: HTML::Pipeline::SyntaxHighlightFilter
- Defined in:
- lib/html/pipeline/syntax_highlight_filter.rb
Overview
HTML Filter that syntax highlights code blocks wrapped in <pre lang=“…”>.
Instance Attribute Summary
Attributes inherited from Filter
Instance Method Summary collapse
Methods inherited from Filter
#base_url, call, #current_user, #doc, #has_ancestor?, #html, #initialize, #needs, #parse_html, #repository, #search_text_nodes, to_document, to_html, #validate
Constructor Details
This class inherits a constructor from HTML::Pipeline::Filter
Instance Method Details
#call ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/html/pipeline/syntax_highlight_filter.rb', line 12 def call doc.search('pre').each do |node| default = context[:highlight] && context[:highlight].to_s next unless lang = node['lang'] || default next unless lexer = Pygments::Lexer[lang] text = node.inner_text html = highlight_with_timeout_handling(lexer, text) next if html.nil? if (node = node.replace(html).first) klass = node["class"] klass = [klass, "highlight-#{lang}"].compact.join " " node["class"] = klass end end doc end |
#highlight_with_timeout_handling(lexer, text) ⇒ Object
32 33 34 35 36 |
# File 'lib/html/pipeline/syntax_highlight_filter.rb', line 32 def highlight_with_timeout_handling(lexer, text) lexer.highlight(text) rescue Timeout::Error => boom nil end |