Class CodeRay::Encoders::XML
In: lib/coderay/encoders/xml.rb
Parent: Encoder

XML Encoder

Uses REXML. Very slow.

Methods

Included Modules

Streamable

Constants

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

Protected Instance methods

[Source]

    # File lib/coderay/encoders/xml.rb, line 60
60:     def close_token kind
61:       if @node == @root
62:         raise 'no token to close!'
63:       end
64:       @node = @node.parent
65:     end

[Source]

    # File lib/coderay/encoders/xml.rb, line 31
31:     def finish options
32:       @doc.write @out, options[:pretty], options[:transitive], true
33:       @out
34:     end

[Source]

    # File lib/coderay/encoders/xml.rb, line 56
56:     def open_token kind
57:       @node = @node.add_element kind.to_s
58:     end

[Source]

    # File lib/coderay/encoders/xml.rb, line 24
24:     def setup options
25:       @doc = REXML::Document.new
26:       @doc << REXML::XMLDecl.new
27:       @tab_width = options[:tab_width]
28:       @root = @node = @doc.add_element('coderay-tokens')
29:     end

[Source]

    # File lib/coderay/encoders/xml.rb, line 36
36:     def text_token text, kind
37:       if kind == :space
38:         token = @node
39:       else
40:         token = @node.add_element kind.to_s
41:       end
42:       text.scan(/(\x20+)|(\t+)|(\n)|[^\x20\t\n]+/) do |space, tab, nl|
43:         case
44:         when space
45:           token << REXML::Text.new(space, true)
46:         when tab
47:           token << REXML::Text.new(tab, true)
48:         when nl
49:           token << REXML::Text.new(nl, true)
50:         else
51:           token << REXML::Text.new($&)
52:         end
53:       end
54:     end

[Validate]