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



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/plurimath/xml_engine/oga.rb', line 96

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



76
77
78
79
80
81
82
83
84
# File 'lib/plurimath/xml_engine/oga.rb', line 76

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



86
87
88
89
90
91
92
93
94
# File 'lib/plurimath/xml_engine/oga.rb', line 86

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



114
115
116
117
118
# File 'lib/plurimath/xml_engine/oga.rb', line 114

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

#locate(xpath) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/plurimath/xml_engine/oga.rb', line 120

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



133
134
135
# File 'lib/plurimath/xml_engine/oga.rb', line 133

def name
  @wrapped.name
end

#name=(new_name) ⇒ Object



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

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.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/plurimath/xml_engine/oga.rb', line 58

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