Class: Plurimath::XmlEngine::Oga::Dumper
- Inherits:
-
Object
- Object
- Plurimath::XmlEngine::Oga::Dumper
- Defined in:
- lib/plurimath/xml_engine/oga/dumper.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
-
#out ⇒ Object
readonly
Returns the value of attribute out.
Class Method Summary collapse
Instance Method Summary collapse
- #dump(node = @tree) ⇒ Object
-
#initialize(tree, indent: nil) ⇒ Dumper
constructor
A new instance of Dumper.
Constructor Details
#initialize(tree, indent: nil) ⇒ Dumper
Returns a new instance of Dumper.
6 7 8 9 10 11 |
# File 'lib/plurimath/xml_engine/oga/dumper.rb', line 6 def initialize(tree, indent: nil) @tree = tree @indent = indent @depth = 0 @out = "" end |
Instance Attribute Details
#out ⇒ Object (readonly)
Returns the value of attribute out.
38 39 40 |
# File 'lib/plurimath/xml_engine/oga/dumper.rb', line 38 def out @out end |
Class Method Details
.entities(text, attr = false) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/plurimath/xml_engine/oga/dumper.rb', line 48 def self.entities(text,attr=false) text.to_s.chars.map(&:ord).map do |i| if i == ORD_AMP "&" elsif i == ORD_LT "<" elsif i == ORD_GT ">" elsif i == ORD_QUOT && attr """ 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
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/plurimath/xml_engine/oga/dumper.rb', line 13 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 |