class HTML::Pipeline::SyntaxHighlightFilter

HTML Filter that syntax highlights code blocks wrapped in <pre lang=“…”>.

Public Instance Methods

call() click to toggle source
# File lib/html/pipeline/syntax_highlight_filter.rb, line 12
def call
  doc.search('pre').each do |node|
    next unless lang = node['lang']
    next unless lexer = Pygments::Lexer[lang]
    text = node.inner_text

    html = highlight_with_timeout_handling(lexer, text)
    next if html.nil?

    node.replace(html)
  end
  doc
end
highlight_with_timeout_handling(lexer, text) click to toggle source
# File lib/html/pipeline/syntax_highlight_filter.rb, line 26
def highlight_with_timeout_handling(lexer, text)
  lexer.highlight(text)
rescue Timeout::Error => boom
  nil
end