Sha256: df7c913b811f85b7ad2705dd070b1dceabd04a4250abf397da4e5053b4cc2ed7
Contents?: true
Size: 1.05 KB
Versions: 1
Compression:
Stored size: 1.05 KB
Contents
begin require 'linguist' rescue LoadError raise LoadError, "You need to install 'github-linguist' before using the SyntaxHighlightFilter. See README.md for details" end module HTML class Pipeline # HTML Filter that syntax highlights code blocks wrapped # in <pre lang="...">. class SyntaxHighlightFilter < Filter 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 def highlight_with_timeout_handling(lexer, text) lexer.highlight(text) rescue Timeout::Error => boom nil end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
html-pipeline-0.3.1 | lib/html/pipeline/syntax_highlight_filter.rb |