Sha256: f5183d33c0cb351f7e152c5bee66f47f8db032dc28e28b5f2500160d8c6a44dc
Contents?: true
Size: 1.17 KB
Versions: 4
Compression:
Stored size: 1.17 KB
Contents
# frozen_string_literal: true 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? node.inner_html = html klass = node['class'] scope = context[:scope] || "highlight-#{lang}" klass = [klass, scope].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
4 entries across 4 versions & 1 rubygems