This module holds the Encoder class and its subclasses. For example, the HTML encoder is named CodeRay::Encoders::HTML can be found in coderay/encoders/html.

Encoders also provides methods and constants for the register mechanism and the [] method that returns the Encoder class belonging to the given format.

Methods
Classes and Modules
Class CodeRay::Encoders::CommentFilter
Class CodeRay::Encoders::Count
Class CodeRay::Encoders::Debug
Class CodeRay::Encoders::Div
Class CodeRay::Encoders::Encoder
Class CodeRay::Encoders::Filter
Class CodeRay::Encoders::HTML
Class CodeRay::Encoders::JSON
Class CodeRay::Encoders::LinesOfCode
Class CodeRay::Encoders::Null
Class CodeRay::Encoders::Page
Class CodeRay::Encoders::Span
Class CodeRay::Encoders::Statistic
Class CodeRay::Encoders::Terminal
Class CodeRay::Encoders::Text
Class CodeRay::Encoders::TokenKindFilter
Class CodeRay::Encoders::XML
Class CodeRay::Encoders::YAML
Constants
TRANSPARENT_TOKEN_KINDS = Set[ :delimiter, :modifier, :content, :escape, :inline_delimiter, ]
Public Class methods
token_path_to_hint(hint, kinds)

Generate a hint about the given kinds in a hint style.

hint may be :info, :info_long or :debug.

# File lib/coderay/encoders/html.rb, line 148
    def self.token_path_to_hint hint, kinds
      kinds = Array kinds
      title =
        case hint
        when :info
          kinds = kinds[1..-1] if TRANSPARENT_TOKEN_KINDS.include? kinds.first
          TOKEN_KIND_TO_INFO[kinds.first]
        when :info_long
          kinds.reverse.map { |kind| TOKEN_KIND_TO_INFO[kind] }.join('/')
        when :debug
          kinds.inspect
        end
      title ? " title=\"#{title}\"" : ''
    end
Public Instance methods
begin_group(kind)

token groups, eg. strings

# File lib/coderay/encoders/html.rb, line 256
    def begin_group kind
      @out << (@span_for_kind[@last_opened ? [kind, *@opened] : kind] || '<span>')
      @opened << kind
      @last_opened = kind if @set_last_opened
    end
begin_line(kind)

whole lines to be highlighted, eg. a deleted line in a diff

# File lib/coderay/encoders/html.rb, line 274
    def begin_line kind
      if style = @span_for_kind[@last_opened ? [kind, *@opened] : kind]
        if style['class="']
          @out << style.sub('class="', 'class="line ')
        else
          @out << style.sub('>', ' class="line">')
        end
      else
        @out << '<span class="line">'
      end
      @opened << kind
      @last_opened = kind if @options[:css] == :style
    end
end_group(kind)
# File lib/coderay/encoders/html.rb, line 262
    def end_group kind
      if $CODERAY_DEBUG && (@opened.empty? || @opened.last != kind)
        warn 'Malformed token stream: Trying to close a token (%p) ' \
          'that is not open. Open are: %p.' % [kind, @opened[1..-1]]
      end
      if @opened.pop
        @out << '</span>'
        @last_opened = @opened.last if @last_opened
      end
    end
end_line(kind)
# File lib/coderay/encoders/html.rb, line 288
    def end_line kind
      if $CODERAY_DEBUG && (@opened.empty? || @opened.last != kind)
        warn 'Malformed token stream: Trying to close a line (%p) ' \
          'that is not open. Open are: %p.' % [kind, @opened[1..-1]]
      end
      if @opened.pop
        @out << '</span>'
        @last_opened = @opened.last if @last_opened
      end
    end
finish(options)
# File lib/coderay/encoders/html.rb, line 219
    def finish options
      unless @opened.empty?
        warn '%d tokens still open: %p' % [@opened.size, @opened] if $CODERAY_DEBUG
        @out << '</span>' while @opened.pop
        @last_opened = nil
      end
      
      @out.extend Output
      @out.css = @css
      if options[:line_numbers]
        Numbering.number! @out, options[:line_numbers], options
      end
      @out.wrap! options[:wrap]
      @out.apply_title! options[:title]
      
      if defined?(@real_out) && @real_out
        @real_out << @out
        @out = @real_out
      end
      
      super
    end
setup(options)
# File lib/coderay/encoders/html.rb, line 163
    def setup options
      super
      
      if options[:wrap] || options[:line_numbers]
        @real_out = @out
        @out = ''
      end
      
      @HTML_ESCAPE = HTML_ESCAPE.dup
      @HTML_ESCAPE["\t"] = ' ' * options[:tab_width]
      
      @opened = []
      @last_opened = nil
      @css = CSS.new options[:style]
      
      hint = options[:hint]
      if hint && ![:debug, :info, :info_long].include?(hint)
        raise ArgumentError, "Unknown value %p for :hint; \
          expected :info, :info_long, :debug, false, or nil." % hint
      end
      
      css_classes = TokenKinds
      case options[:css]
      when :class
        @span_for_kind = Hash.new do |h, k|
          if k.is_a? ::Symbol
            kind = k_dup = k
          else
            kind = k.first
            k_dup = k.dup
          end
          if kind != :space && (hint || css_class = css_classes[kind])
            title = HTML.token_path_to_hint hint, k if hint
            css_class ||= css_classes[kind]
            h[k_dup] = "<span#{title}#{" class=\"#{css_class}\"" if css_class}>"
          else
            h[k_dup] = nil
          end
        end
      when :style
        @span_for_kind = Hash.new do |h, k|
          kind = k.is_a?(Symbol) ? k : k.first
          h[k.is_a?(Symbol) ? k : k.dup] =
            if kind != :space && (hint || css_classes[kind])
              title = HTML.token_path_to_hint hint, k if hint
              style = @css.get_style Array(k).map { |c| css_classes[c] }
              "<span#{title}#{" style=\"#{style}\"" if style}>"
            end
        end
      else
        raise ArgumentError, "Unknown value %p for :css." % options[:css]
      end
      
      @set_last_opened = options[:hint] || options[:css] == :style
    end
text_token(text, kind)
# File lib/coderay/encoders/html.rb, line 244
    def text_token text, kind
      if text =~ /#{HTML_ESCAPE_PATTERN}/o
        text = text.gsub(/#{HTML_ESCAPE_PATTERN}/o) { |m| @HTML_ESCAPE[m] }
      end
      if style = @span_for_kind[@last_opened ? [kind, *@opened] : kind]
        @out << style << text << '</span>'
      else
        @out << text
      end
    end