# 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   return nil unless lang || opts[:default_lang] || opts[:guess_lang]
    27
    28   lexer = ::Rouge::Lexer.find_fancy(lang || opts[:default_lang], text)
    29   return nil if opts[:disable] || !lexer || (lexer.tag == "plaintext" && !opts[:guess_lang])
    30
    31   opts[:css_class] ||= 'highlight' # For backward compatibility when using Rouge 2.0
    32   formatter = formatter_class(opts).new(opts)
    33   formatter.format(lexer.lex(text))
    34 end

formatter_class(opts = {}) click to toggle source

       # File lib/kramdown/converter/syntax_highlighter/rouge.rb
    67 def self.formatter_class(opts = {})
    68   case formatter = opts[:formatter]
    69   when Class
    70     formatter
    71   when /\A[[:upper:]][[:alnum:]_]*\z/
    72     ::Rouge::Formatters.const_get(formatter, false)
    73   else
    74     # Available in Rouge 2.0 or later
    75     ::Rouge::Formatters::HTMLLegacy
    76   end
    77 rescue NameError
    78   # Fallback to Rouge 1.x
    79   ::Rouge::Formatters::HTML
    80 end

normalize_keys(hash) click to toggle source

       # File lib/kramdown/converter/syntax_highlighter/rouge.rb
    59 def self.normalize_keys(hash)
    60   return if hash.empty?
    61
    62   hash.keys.each do |k|
    63     hash[k.kind_of?(String) ? Kramdown::Options.str_to_sym(k) : k] = hash.delete(k)
    64   end
    65 end

options(converter, type) click to toggle source

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

prepare_options(converter) click to toggle source

       # File lib/kramdown/converter/syntax_highlighter/rouge.rb
    41 def self.prepare_options(converter)
    42   return if converter.data.key?(:syntax_highlighter_rouge)
    43
    44   cache = converter.data[:syntax_highlighter_rouge] = {}
    45
    46   opts = converter.options[:syntax_highlighter_opts].dup
    47
    48   span_opts = opts.delete(:span)&.dup || {}
    49   block_opts = opts.delete(:block)&.dup || {}
    50   normalize_keys(span_opts)
    51   normalize_keys(block_opts)
    52
    53   cache[:span] = opts.merge(span_opts)
    54   cache[:span][:wrap] = false
    55
    56   cache[:block] = opts.merge(block_opts)
    57 end