Sha256: 24e8d26b69c003848c02ab4d4f047986bc49e28079f2cd5fecfc46f8dd17de43
Contents?: true
Size: 1.59 KB
Versions: 1
Compression:
Stored size: 1.59 KB
Contents
# encoding: utf-8 module Nanoc3::Filters class ColorizeSyntax < Nanoc3::Filter DEFAULT_COLORIZER = :coderay def run(content, params={}) require 'nokogiri' # Take colorizers from parameters @colorizers = Hash.new(DEFAULT_COLORIZER) (params[:colorizers] || {}).each_pair do |language, colorizer| @colorizers[language] = colorizer end # Colorize doc = Nokogiri::HTML.fragment(content) doc.css('pre > code[class*="language-"]').each do |element| # Get language match = element['class'].match(/(^| )language-([^ ]+)/) next if match.nil? language = match[2] # Highlight highlighted_code = highlight(element.inner_text, language, params) element.inner_html = highlighted_code end doc.to_s end private KNOWN_COLORIZERS = [ :coderay, :dummy, :pygmentize ] def highlight(code, language, params={}) colorizer = @colorizers[language] if KNOWN_COLORIZERS.include?(colorizer) send(colorizer, code, language, params[colorizer] || {}) else raise RuntimeError, "I don’t know how to highlight code using the “#{colorizer}” colorizer" end end def coderay(code, language, params={}) require 'coderay' ::CodeRay.scan(code, language).html(params) end def dummy(code, language, params={}) code end def pygmentize(code, language, params={}) IO.popen("pygmentize -l #{language} -f html", "r+") do |io| io.write(code) io.close_write return io.read end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
nanoc3-3.1.0b1 | lib/nanoc3/filters/colorize_syntax.rb |