Sha256: 398d130360b6c76e8bb4b3106d2fc21a9ee4d0d26eb2588a9fdf88731deff756

Contents?: true

Size: 1.33 KB

Versions: 15

Compression:

Stored size: 1.33 KB

Contents

module CodeRay
module Encoders

  # = XML Encoder
  #
  # Uses REXML. Very slow.
  class XML < Encoder

    include Streamable
    register_for :xml

    FILE_EXTENSION = 'xml'

    require 'rexml/document'

    DEFAULT_OPTIONS = {
      :tab_width => 8,
      :pretty => -1,
      :transitive => false,
    }

  protected

    def setup options
      @doc = REXML::Document.new
      @doc << REXML::XMLDecl.new
      @tab_width = options[:tab_width]
      @root = @node = @doc.add_element('coderay-tokens')
    end

    def finish options
      @out = ''
      @doc.write @out, options[:pretty], options[:transitive], true
      @out
    end
    
    def text_token text, kind
      if kind == :space
        token = @node
      else
        token = @node.add_element kind.to_s
      end
      text.scan(/(\x20+)|(\t+)|(\n)|[^\x20\t\n]+/) do |space, tab, nl|
        case
        when space
          token << REXML::Text.new(space, true)
        when tab
          token << REXML::Text.new(tab, true)
        when nl
          token << REXML::Text.new(nl, true)
        else
          token << REXML::Text.new($&)
        end
      end
    end

    def open_token kind
      @node = @node.add_element kind.to_s
    end

    def close_token kind
      if @node == @root
        raise 'no token to close!'
      end
      @node = @node.parent
    end

  end

end
end

Version data entries

15 entries across 15 versions & 3 rubygems

Version Path
raldred-coderay-0.9.3431 ./lib/coderay/encoders/xml.rb
raldred-coderay-0.9.3551 ./lib/coderay/encoders/xml.rb
coderay-0.9.8 ./lib/coderay/encoders/xml.rb
coderay-0.9.8.pre ./lib/coderay/encoders/xml.rb
coderay-0.9.7 ./lib/coderay/encoders/xml.rb
coderay-0.9.7.pre ./lib/coderay/encoders/xml.rb
coderay-0.9.6 ./lib/coderay/encoders/xml.rb
coderay-0.9.6.pre2 ./lib/coderay/encoders/xml.rb
coderay-0.9.6.pre ./lib/coderay/encoders/xml.rb
coderay-0.9.5 ./lib/coderay/encoders/xml.rb
coderay-0.9.4 ./lib/coderay/encoders/xml.rb
coderay-0.9.3 ./lib/coderay/encoders/xml.rb
coderay-0.9.2 ./lib/coderay/encoders/xml.rb
coderay-0.9.1 ./lib/coderay/encoders/xml.rb
coderay-beta-0.9.1 ./lib/coderay/encoders/xml.rb