Class: Plurimath::XMLEngine::Oga::Node

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

Direct Known Subclasses

Document

Instance Method Summary collapse

Methods inherited from Wrapper

#==, #initialize, #unwrap

Constructor Details

This class inherits a constructor from Plurimath::XMLEngine::Oga::Wrapper

Instance Method Details

#<<(other) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/plurimath/xml_engine/oga.rb', line 101

def <<(other)
  other = other.unwrap if other.respond_to? :unwrap

  case other
  when String
    text = other
    # Here we tap into the internal representation due to some likely
    # bug in Oga
    other = ::Oga::XML::Text.new
    other.instance_variable_set(:@from_plurimath, true)
    other.instance_variable_set(:@text, text)
    other.instance_variable_set(:@decoded, true)
  end

  @wrapped.children << other.dup
  self
end

#[](attr) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/plurimath/xml_engine/oga.rb', line 81

def [](attr)
  attr = attr.to_s

  @wrapped.attributes.each do |e|
    return e.value if [e.name, e.name.split(":").last].include? attr
  end

  nil
end

#[]=(attr, value) ⇒ Object



91
92
93
94
95
96
97
98
99
# File 'lib/plurimath/xml_engine/oga.rb', line 91

def []=(attr, value)
  # Here we tap into the internal representation due to some likely
  # bug in Oga
  attr = ::Oga::XML::Attribute.new(name: attr.to_s)
  attr.element = @wrapped
  attr.instance_variable_set(:@value, value.to_s)
  attr.instance_variable_set(:@decoded, true)
  @wrapped.attributes << attr
end

#attributesObject



119
120
121
122
123
# File 'lib/plurimath/xml_engine/oga.rb', line 119

def attributes
  @wrapped.attributes.to_h do |e|
    [e.name.split(":").last, e.value]
  end
end

#locate(xpath) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/plurimath/xml_engine/oga.rb', line 125

def locate(xpath)
  @wrapped.xpath(xpath).map do |i|
    case i
    when ::Oga::XML::Text
      i.text
    when ::Oga::XML::Attribute
      i.value
    else
      Node.new(i)
    end
  end
end

#nameObject



138
139
140
# File 'lib/plurimath/xml_engine/oga.rb', line 138

def name
  @wrapped.name
end

#name=(new_name) ⇒ Object



142
143
144
# File 'lib/plurimath/xml_engine/oga.rb', line 142

def name=(new_name)
  @wrapped.name = new_name
end

#nodesObject

Ox removes text nodes that are whitespace-only. There exists a weird edge case on which Plurimath depends: <mi> <!– xxx –> &#x3C0;<!–GREEK SMALL LETTER PI–> </mi> If the last text node of an element that does not contain other elements is a whitespace, it preserves it. The first one can be safely removed.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/plurimath/xml_engine/oga.rb', line 63

def nodes
  children = @wrapped.children
  length = children.length
  preserve_last = true
  children.map.with_index do |i,idx|
    if preserve_last && idx == length-1 && i.is_a?(::Oga::XML::Text)
      i.text
    elsif i.is_a? ::Oga::XML::Text
      remove_indentation(i)
    elsif i.is_a? ::Oga::XML::Comment
      Node.new(i)
    else
      preserve_last = false
      Node.new(i)
    end
  end.compact
end