Sha256: 574f00ff318add66438bf8cf7a55cdaf8743c8bed6aa64e8ad309b8ec7d4c0b6
Contents?: true
Size: 1.12 KB
Versions: 1
Compression:
Stored size: 1.12 KB
Contents
HTML::Pipeline.require_dependency('rouge', 'SyntaxHighlightFilter') module HTML class Pipeline # HTML Filter that syntax highlights code blocks wrapped # in <pre lang="...">. class SyntaxHighlightFilter < Filter def initialize(*args) super(*args) @formatter = Rouge::Formatters::HTML.new end 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(text, lang) next if html.nil? next unless (node = node.replace(html).first) klass = node['class'] klass = [klass, "highlight-#{lang}"].compact.join ' ' node['class'] = klass end doc end def highlight_with_timeout_handling(text, lang) Rouge.highlight(text, lang, @formatter) rescue Timeout::Error => _ nil end def lexer_for(lang) Rouge::Lexer.find(lang) end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
html-pipeline-2.7.2 | lib/html/pipeline/syntax_highlight_filter.rb |