Sha256: cd7222b6cc3a25b9d06cbd41d7f38ff8b80141b6ce5e2abdb5a5a392f69b711e

Contents?: true

Size: 1.29 KB

Versions: 4

Compression:

Stored size: 1.29 KB

Contents

module CodeRay
	module Encoders

		# = Debug Encoder
		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

4 entries across 4 versions & 1 rubygems

Version Path
coderay-0.5.0.121 ./lib/coderay/encoders/xml.rb
coderay-0.5.0.115 ./lib/coderay/encoders/xml.rb
coderay-0.5.0.100 ./lib/coderay/encoders/xml.rb
coderay-0.4.5.73 ./lib/coderay/encoders/xml.rb