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.



156
157
158
159
160
161
# File 'lib/plurimath/xml_engine/oga.rb', line 156

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

Instance Attribute Details

#outObject (readonly)

Returns the value of attribute out.



188
189
190
# File 'lib/plurimath/xml_engine/oga.rb', line 188

def out
  @out
end

Class Method Details

.entities(text, attr = false) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/plurimath/xml_engine/oga.rb', line 198

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



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/plurimath/xml_engine/oga.rb', line 163

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