To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / .svn / pristine / 47 / 47cd8b345d1020290ba00f3aec67bf352663d44c.svn-base @ 1297:0a574315af3e

History | View | Annotate | Download (1.38 KB)

1
module CodeRay
2
module Encoders
3
  
4
  # = XML Encoder
5
  #
6
  # Uses REXML. Very slow.
7
  class XML < Encoder
8
    
9
    register_for :xml
10
    
11
    FILE_EXTENSION = 'xml'
12
    
13
    autoload :REXML, 'rexml/document'
14
    
15
    DEFAULT_OPTIONS = {
16
      :tab_width => 8,
17
      :pretty => -1,
18
      :transitive => false,
19
    }
20
    
21
  protected
22
    def setup options
23
      super
24
      
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
30
    
31
    def finish options
32
      @doc.write @out, options[:pretty], options[:transitive], true
33
      
34
      super
35
    end
36
    
37
  public
38
    def text_token text, kind
39
      if kind == :space
40
        token = @node
41
      else
42
        token = @node.add_element kind.to_s
43
      end
44
      text.scan(/(\x20+)|(\t+)|(\n)|[^\x20\t\n]+/) do |space, tab, nl|
45
        case
46
        when space
47
          token << REXML::Text.new(space, true)
48
        when tab
49
          token << REXML::Text.new(tab, true)
50
        when nl
51
          token << REXML::Text.new(nl, true)
52
        else
53
          token << REXML::Text.new($&)
54
        end
55
      end
56
    end
57
    
58
    def begin_group kind
59
      @node = @node.add_element kind.to_s
60
    end
61
    
62
    def end_group kind
63
      if @node == @root
64
        raise 'no token to close!'
65
      end
66
      @node = @node.parent
67
    end
68
    
69
  end
70
  
71
end
72
end