# module Kramdown::Converter::SyntaxHighlighter::Coderay

Uses `Coderay` to highlight code blocks and code spans.

### Constants

AVAILABLE

  Highlighting via coderay 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/coderay.rb
    23 def self.call(converter, text, lang, type, call_opts)
    24   return nil unless converter.options[:enable_coderay]
    25
    26   if type == :span && lang
    27     ::CodeRay.scan(text, lang.to_sym).html(options(converter, :span)).chomp
    28   elsif type == :block && (lang || options(converter, :default_lang))
    29     lang ||= call_opts[:default_lang] = options(converter, :default_lang)
    30     ::CodeRay.scan(text, lang.to_s.gsub(/-/, '_').to_sym).html(options(converter, :block)).chomp << "\n"
    31   else
    32     nil
    33   end
    34 rescue
    35   converter.warning("There was an error using CodeRay: #{$!.message}")
    36   nil
    37 end

options(converter, type) click to toggle source

       # File lib/kramdown/converter/syntax_highlighter/coderay.rb
    39 def self.options(converter, type)
    40   prepare_options(converter)
    41   converter.data[:syntax_highlighter_coderay][type]
    42 end

prepare_options(converter) click to toggle source

       # File lib/kramdown/converter/syntax_highlighter/coderay.rb
    44 def self.prepare_options(converter)
    45   return if converter.data.key?(:syntax_highlighter_coderay)
    46
    47   cache = converter.data[:syntax_highlighter_coderay] = {}
    48
    49   opts = converter.options[:syntax_highlighter_opts].dup
    50   span_opts = (opts.delete(:span) || {}).dup
    51   block_opts = (opts.delete(:block) || {}).dup
    52   [span_opts, block_opts].each do |hash|
    53     hash.keys.each do |k|
    54       hash[k.kind_of?(String) ? Kramdown::Options.str_to_sym(k) : k] = hash.delete(k)
    55     end
    56   end
    57
    58   cache[:default_lang] = converter.options[:coderay_default_lang] || opts.delete(:default_lang)
    59   cache[:span] = {
    60     :css => converter.options[:coderay_css]
    61   }.update(opts).update(span_opts).update(:wrap => :span)
    62   cache[:block] = {
    63     :wrap => converter.options[:coderay_wrap],
    64     :line_numbers => converter.options[:coderay_line_numbers],
    65     :line_number_start => converter.options[:coderay_line_number_start],
    66     :tab_width => converter.options[:coderay_tab_width],
    67     :bold_every => converter.options[:coderay_bold_every],
    68     :css => converter.options[:coderay_css]
    69   }.update(opts).update(block_opts)
    70
    71   [:css, :wrap, :line_numbers].each do |key|
    72     [:block, :span].each do |type|
    73       cache[type][key] = cache[type][key].to_sym if cache[type][key].kind_of?(String)
    74     end
    75   end
    76 end