Class: Kwaff::XmlTranslator

Inherits:
Translator show all
Defined in:
lib/kwaff/translator.rb

Overview

translate Kwaff::Document into XML string

ex.

kwaff_str  = File.open('file.kwaff') { |f| f.read }
parser     = Kwaff::Parser.new(kwaff_str)
document   = parser.parse_document()
toppings   = { :newline => parser.newline, :output_indent_width=>2 }
translator = XmlTranslator.new(toppings)
xml_str    = translator.translate(document)

Instance Attribute Summary

Attributes inherited from Translator

#toppings

Instance Method Summary collapse

Methods inherited from Translator

#translate

Constructor Details

#initialize(toppings = {}) ⇒ XmlTranslator

Returns a new instance of XmlTranslator.



68
69
70
71
72
73
74
# File 'lib/kwaff/translator.rb', line 68

def initialize(toppings={})
   super(toppings)
   @newline = toppings[:newline] || "\n"
   @output = ""
   width = toppings[:output_indent_width]
   @space = width ? ' ' * width.to_i : '  '
end

Instance Method Details

#translate_comment(comment, level = 0) ⇒ Object



126
127
128
129
130
131
132
# File 'lib/kwaff/translator.rb', line 126

def translate_comment(comment, level=0)
   n = comment.empty_lines()
   n.times { @output << @newline } if n
   @output << @space * level
   @output << "<!-- #{comment.str} -->#{@newline}"
   return @output
end

#translate_document(document, level = 0) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/kwaff/translator.rb', line 108

def translate_document(document, level=0)
   headers = document.headers()
   xmlversion = headers[:xmlversion] || '1.0'
   encoding = headers[:encoding] ? " encoding=\"#{headers[:encoding]}\"" : ''
   doctype = headers[:doctype]
   @output << "<?xml version=\"#{xmlversion}\"#{encoding}?>#{@newline}"
   @output << doctype.to_s if doctype
   @output << "\n" if @output[-1] != ?\n
   if document.root
      translate_element(document.root, level)
      #if (n = document.empty_lines) != nil && n > 0
      #   @output.sub!(/^\s*<\/.*?>\r?\n?\z/, (@newline * n) + '\&')
      #end
   end
   return @output
end

#translate_element(elem, level = 0) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/kwaff/translator.rb', line 77

def translate_element(elem, level=0)
   n = elem.empty_lines()
   n.times { @output << @newline } if n
   
   @output << @space * level if @output[-1] == ?\n
   @output << "<" << elem.tag
   elem.attrs.each do |aname, avalue|
      @output << " #{aname}=\"#{_escape_attrval(avalue)}\""
   end
   
   children = elem.children
   if children.empty?
      @output << "/>#{@newline}"
   elsif children.length == 1 && children[0].is_a?(Text)
      text = children[0]
      str  = text.escape? ? _escape(text.str) : text.str
      @output << ">#{str}</#{elem.tag}>#{@newline}"
   else
      @output << ">#{@newline}"
      children.each do |child|
         translate(child, level+1)
      end
      n = children[0].empty_lines
      n.times { @output << @newline } if n && n > 0
      @output << @space * level if @output[-1] == ?\n
      @output << "</#{elem.tag}>#{@newline}"
   end
   return @output
end

#translate_text(text, level = 0) ⇒ Object



135
136
137
138
139
140
141
142
# File 'lib/kwaff/translator.rb', line 135

def translate_text(text, level=0)
   n = text.empty_lines()
   n.times { @output << @newline } if n
   #@output << @space * level
   str = text.escape? ? _escape(text.str) : text.str
   @output << "#{str}"
   return @output
end