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| default = context[:highlight] && context[:highlight].to_s next unless lang = node['lang'] || default next unless lexer = lexer_for(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)
click to toggle source
# 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
lexer_for(lang)
click to toggle source
# File lib/html/pipeline/syntax_highlight_filter.rb, line 38 def lexer_for(lang) (Linguist::Language[lang] && Linguist::Language[lang].lexer) || Pygments::Lexer[lang] end