Class: Plurimath::XMLEngine::Oga::Dumper

Inherits:
Object
  • Object
show all
Defined in:
lib/plurimath/xml_engine/oga.rb

Overview

Dump the tree just as if we were Ox. This is a limited implementation.

Constant Summary collapse

ORD_AMP =
"&".ord
ORD_LT =
"<".ord
ORD_GT =
">".ord
ORD_APOS =
"'".ord
ORD_QUOT =
'"'.ord
ORD_NEWLINE =
"\n".ord
ORD_CARRIAGERETURN =
"\r".ord

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tree, indent: nil) ⇒ Dumper

Returns a new instance of Dumper.



161
162
163
164
165
166
# File 'lib/plurimath/xml_engine/oga.rb', line 161

def initialize(tree, indent: nil)
  @tree = tree
  @indent = indent
  @depth = 0
  @out = ""
end

Instance Attribute Details

#outObject (readonly)

Returns the value of attribute out.



193
194
195
# File 'lib/plurimath/xml_engine/oga.rb', line 193

def out
  @out
end

Class Method Details

.entities(text, attr = false) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/plurimath/xml_engine/oga.rb', line 203

def self.entities(text,attr=false)
  text.to_s.chars.map(&:ord).map do |i|
    if i == ORD_AMP
      "&amp;"
    elsif i == ORD_LT
      "&lt;"
    elsif i == ORD_GT
      "&gt;"
    elsif i == ORD_QUOT && attr
      "&quot;"
    elsif i == ORD_NEWLINE || i == ORD_CARRIAGERETURN
      i.chr("utf-8")
    elsif i < 0x20
      "&#x#{i.to_s(16).rjust(4, "0")};"
    else
      i.chr("utf-8")
    end
  end.join
end

Instance Method Details

#dump(node = @tree) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/plurimath/xml_engine/oga.rb', line 168

def dump(node = @tree)
  case node
  when Node
    nodes = node.nodes
    if nodes.length == 0
      line_break
      @out += "<#{node.unwrap.name}#{dump_attrs(node)}/>"
    else
      line_break
      @out += "<#{node.unwrap.name}#{dump_attrs(node)}>"
      @depth += 1
      nodes.each { |i| dump(i) }
      @depth -= 1
      line_break unless nodes.last.is_a?(::String)
      @out += "</#{node.unwrap.name}>"
    end
  when ::String
    @out += entities(node)
  end

  line_break if node.object_id == @tree.object_id

  self
end