Sha256: de2d2abfc17586cde15147f54b5d80bc6f5112a0d1663ef7d48803150f70b30d

Contents?: true

Size: 677 Bytes

Versions: 4

Compression:

Stored size: 677 Bytes

Contents

require 'linguist'

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|
          next unless lang = node['lang']
          next unless lexer = Pygments::Lexer[lang]
          text = node.inner_text

          html = highlight_with_timeout_handling(lexer, text)
          next if html.nil?

          node.replace(html)
        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

4 entries across 4 versions & 2 rubygems

Version Path
html-pipeline-no-charlock-0.0.6 lib/html/pipeline/syntax_highlight_filter.rb
html-pipeline-0.0.6 lib/html/pipeline/syntax_highlight_filter.rb
html-pipeline-0.0.5 lib/html/pipeline/syntax_highlight_filter.rb
html-pipeline-0.0.4 lib/html/pipeline/syntax_highlight_filter.rb