class HTML::Pipeline::SyntaxHighlightFilter
HTML
Filter
that syntax highlights code blocks wrapped in <code lang=“…”>.
Public Instance Methods
call()
click to toggle source
# File lib/html/pipeline/syntax_highlight_filter.rb, line 9 def call doc.search('code').each do |node| next unless lang = node['class'] lexer = Pygments::Lexer[lang] if lexer text = node.inner_text html = highlight_with_timeout_handling(lexer, text) next if html.nil? node.child.replace(html) else node.remove_attribute 'class' end end doc end
highlight_with_timeout_handling(lexer, text)
click to toggle source
# File lib/html/pipeline/syntax_highlight_filter.rb, line 28 def highlight_with_timeout_handling(lexer, text) lexer.highlight(text, options: { nowrap: true, startinline: true }) rescue Timeout::Error nil end