Class: Kwaff::KwaffTranslator

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

Overview

translate Kwaff::Document into Kwaff 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 = KwaffTranslator.new(toppings)
xml_str    = translator.translate(document)

if you want to convert XML string to Kwaff string, use Kwaff::Rexml::revert() or Kwaff::Rexml::KwaffTranslator.

Instance Attribute Summary

Attributes inherited from Translator

#toppings

Instance Method Summary collapse

Methods inherited from Translator

#translate

Constructor Details

#initialize(toppings = {}) ⇒ KwaffTranslator

Returns a new instance of KwaffTranslator.



182
183
184
185
186
187
188
# File 'lib/kwaff/translator.rb', line 182

def initialize(toppings={})
   super(toppings)
   @toppings = toppings
   @space    = toppings[:indent_width] || '  '
   @newline  = toppings[:newline] || "\n"
   @output   = ''
end

Instance Method Details

#translate_comment(comment, level = 0) ⇒ Object



240
241
242
243
244
245
# File 'lib/kwaff/translator.rb', line 240

def translate_comment(comment, level=0)
   @output ||= ""
   @output << @space * level
   @output << "# #{comment.str}#{@newline}"		## *pattern*
   return @output
end

#translate_document(document, level = 0) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/kwaff/translator.rb', line 217

def translate_document(document, level=0)
   headers = document.headers
   if headers
      #if headers[:doctype]
      #    headers[:doctype] = headers[:doctype].gsub(/\n(.)/, ' \1')
      #end
      doctype = headers[:doctype]
      if doctype && doctype.is_a?(DocType)
         doctype2 = DocType.find { |dt| dt.public_id && dt.public_id == doctype.public_id }
         if doctype2 != nil && (nickname = DocType.nickname(doctype2)) != nil
            headers[:doctype] = nickname
         end
      end
      headers.each do |key, value|
         @output ||= ""
         @output << "?#{key} = #{value.to_s}"		## *pattern*
         @output << @newline if @output[-1] != ?\n
      end
   end
   translate_element(document.root, level) if document.root
   return @output
end

#translate_element(elem, level = 0) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/kwaff/translator.rb', line 190

def translate_element(elem, level=0)
   ## check value
   children = elem.children
   value = nil
   if children.length == 1 && children[0].is_a?(Text)
      text = children[0]
      str  = text.str
      value = str if !str.index(?\n)
   end

   ## create string
   @output << (@space * level) if level > 0
   @output << '* ' << elem.tag				## *pattern*
   @output << " = #{value}" if value
   @output << @newline
   elem.attrs.each do |aname, avalue|
      @output << (@space * (level+1))
      @output << "- #{aname} = #{avalue}#{@newline}"	## *pattern*
   end
   if !value && children.length > 0
      children.each do |child|
         translate_element(child, level+1)
      end
   end
   return @output
end

#translate_text(text, level = 0) ⇒ Object



247
248
249
250
251
252
# File 'lib/kwaff/translator.rb', line 247

def translate_text(text, level=0)
   @output ||= ""
   @output << @space * level
   @output << ". #{text.str}#{@newline}"			## *pattern*
   return @output
end