Sha256: edf71e393594bff4f5fadf279ad0b26f5df2a569c59d28a6e1615d59397ef22c

Contents?: true

Size: 1.33 KB

Versions: 47

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
      @out = ''
      @doc = REXML::Document.new
      @doc << REXML::XMLDecl.new
      @tab_width = options[:tab_width]
      @root = @node = @doc.add_element('coderay-tokens')
    end

    def finish options
      @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

47 entries across 47 versions & 3 rubygems

Version Path
redcar-dev-0.12.1dev-java lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/encoders/xml.rb
redcar-dev-0.12.0dev-java lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/encoders/xml.rb
redcar-0.11 lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/encoders/xml.rb
redcar-0.11.0dev lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/encoders/xml.rb
redcar-0.10 lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/encoders/xml.rb
redcar-0.9.2 lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/encoders/xml.rb
redcar-0.9.1 lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/encoders/xml.rb
redcar-0.9.0 lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/encoders/xml.rb
redcar-0.8.1 lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/encoders/xml.rb
redcar-0.8 lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/encoders/xml.rb
redcar-0.7 lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/encoders/xml.rb
redcar-0.6.1 lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/encoders/xml.rb
redcar-0.6 lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/encoders/xml.rb
redcar-0.6.1dev lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/encoders/xml.rb
redcar-0.5.1 lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/encoders/xml.rb
redcar-0.5 lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/encoders/xml.rb
redcar-0.5.6dev lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/encoders/xml.rb
redcar-0.5.5dev lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/encoders/xml.rb
redcar-0.5.4dev lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/encoders/xml.rb
redcar-0.5.3dev lib/openssl/mocha/vendor/coderay-0.7.4.215/lib/coderay/encoders/xml.rb