Sha256: 3b4237bc47f9302c7bd9363c06ce4e54615d42cb6171840c3e046ffb854e0793

Contents?: true

Size: 822 Bytes

Versions: 7

Compression:

Stored size: 822 Bytes

Contents

begin
  require 'linguist'
rescue LoadError
  raise LoadError, "You need to install 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|
          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

7 entries across 7 versions & 2 rubygems

Version Path
html-pipeline-0.0.14 lib/html/pipeline/syntax_highlight_filter.rb
html-pipeline-0.0.13 lib/html/pipeline/syntax_highlight_filter.rb
geothird-html-pipeline-0.0.12 lib/html/pipeline/syntax_highlight_filter.rb
html-pipeline-0.0.12 lib/html/pipeline/syntax_highlight_filter.rb
html-pipeline-0.0.11 lib/html/pipeline/syntax_highlight_filter.rb
html-pipeline-0.0.10 lib/html/pipeline/syntax_highlight_filter.rb
html-pipeline-0.0.8 lib/html/pipeline/syntax_highlight_filter.rb