2007-08-16 00:20:18 +04:00
|
|
|
module CodeRay
|
|
|
|
module Encoders
|
2011-10-08 17:34:30 +04:00
|
|
|
|
2007-08-16 00:20:18 +04:00
|
|
|
# = XML Encoder
|
|
|
|
#
|
|
|
|
# Uses REXML. Very slow.
|
|
|
|
class XML < Encoder
|
2011-10-08 17:34:30 +04:00
|
|
|
|
2007-08-16 00:20:18 +04:00
|
|
|
register_for :xml
|
2011-10-08 17:34:30 +04:00
|
|
|
|
2007-08-16 00:20:18 +04:00
|
|
|
FILE_EXTENSION = 'xml'
|
2011-10-08 17:34:30 +04:00
|
|
|
|
|
|
|
autoload :REXML, 'rexml/document'
|
|
|
|
|
2007-08-16 00:20:18 +04:00
|
|
|
DEFAULT_OPTIONS = {
|
|
|
|
:tab_width => 8,
|
|
|
|
:pretty => -1,
|
|
|
|
:transitive => false,
|
|
|
|
}
|
2011-10-08 17:34:30 +04:00
|
|
|
|
2007-08-16 00:20:18 +04:00
|
|
|
protected
|
|
|
|
def setup options
|
2011-10-08 17:34:30 +04:00
|
|
|
super
|
|
|
|
|
2007-08-16 00:20:18 +04:00
|
|
|
@doc = REXML::Document.new
|
|
|
|
@doc << REXML::XMLDecl.new
|
|
|
|
@tab_width = options[:tab_width]
|
|
|
|
@root = @node = @doc.add_element('coderay-tokens')
|
|
|
|
end
|
2011-10-08 17:34:30 +04:00
|
|
|
|
2007-08-16 00:20:18 +04:00
|
|
|
def finish options
|
|
|
|
@doc.write @out, options[:pretty], options[:transitive], true
|
2011-10-08 17:34:30 +04:00
|
|
|
|
|
|
|
super
|
2007-08-16 00:20:18 +04:00
|
|
|
end
|
|
|
|
|
2011-10-08 17:34:30 +04:00
|
|
|
public
|
2007-08-16 00:20:18 +04:00
|
|
|
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
|
2011-10-08 17:34:30 +04:00
|
|
|
|
|
|
|
def begin_group kind
|
2007-08-16 00:20:18 +04:00
|
|
|
@node = @node.add_element kind.to_s
|
|
|
|
end
|
2011-10-08 17:34:30 +04:00
|
|
|
|
|
|
|
def end_group kind
|
2007-08-16 00:20:18 +04:00
|
|
|
if @node == @root
|
|
|
|
raise 'no token to close!'
|
|
|
|
end
|
|
|
|
@node = @node.parent
|
|
|
|
end
|
2011-10-08 17:34:30 +04:00
|
|
|
|
2007-08-16 00:20:18 +04:00
|
|
|
end
|
2011-10-08 17:34:30 +04:00
|
|
|
|
2007-08-16 00:20:18 +04:00
|
|
|
end
|
|
|
|
end
|