# module Kramdown::Converter::SyntaxHighlighter::Rouge

Uses `Rouge` which is CSS-compatible to Pygments to highlight code blocks and code spans.

### Constants

AVAILABLE

  Highlighting via `Rouge` is available if this constant is `true`.

### Public Class Methods

call(converter, text, lang, type, call_opts) click to toggle source

       # File lib/kramdown/converter/syntax_highlighter/rouge.rb
    23 def self.call(converter, text, lang, type, call_opts)
    24   opts = options(converter, type)
    25   call_opts[:default_lang] = opts[:default_lang]
    26   lexer = ::Rouge::Lexer.find_fancy(lang || opts[:default_lang], text)
    27   return nil if opts[:disable] || !lexer
    28   opts[:css_class] ||= 'highlight' # For backward compatibility when using Rouge 2.0
    29   formatter = formatter_class(opts).new(opts)
    30   formatter.format(lexer.lex(text))
    31 end

formatter_class(opts = {}) click to toggle source

       # File lib/kramdown/converter/syntax_highlighter/rouge.rb
    56 def self.formatter_class(opts = {})
    57   case formatter = opts[:formatter]
    58   when Class
    59     formatter
    60   when /\A[[:upper:]][[:alnum:]_]*\z/
    61     ::Rouge::Formatters.const_get(formatter, false)
    62   else
    63     # Available in Rouge 2.0 or later
    64     ::Rouge::Formatters::HTMLLegacy
    65   end
    66 rescue NameError
    67   # Fallback to Rouge 1.x
    68   ::Rouge::Formatters::HTML
    69 end

options(converter, type) click to toggle source

       # File lib/kramdown/converter/syntax_highlighter/rouge.rb
    33 def self.options(converter, type)
    34   prepare_options(converter)
    35   converter.data[:syntax_highlighter_rouge][type]
    36 end

prepare_options(converter) click to toggle source

       # File lib/kramdown/converter/syntax_highlighter/rouge.rb
    38 def self.prepare_options(converter)
    39   return if converter.data.key?(:syntax_highlighter_rouge)
    40
    41   cache = converter.data[:syntax_highlighter_rouge] = {}
    42
    43   opts = converter.options[:syntax_highlighter_opts].dup
    44   span_opts = (opts.delete(:span) || {}).dup
    45   block_opts = (opts.delete(:block) || {}).dup
    46   [span_opts, block_opts].each do |hash|
    47     hash.keys.each do |k|
    48       hash[k.kind_of?(String) ? Kramdown::Options.str_to_sym(k) : k] = hash.delete(k)
    49     end
    50   end
    51
    52   cache[:span] = opts.merge(span_opts).update(:wrap => false)
    53   cache[:block] = opts.merge(block_opts)
    54 end