Class: Plurimath::Omml::Parser
- Inherits:
-
Object
- Object
- Plurimath::Omml::Parser
- Defined in:
- lib/plurimath/omml/parser.rb
Constant Summary collapse
- CUSTOMIZABLE_TAGS =
%w[ eqArr mr r ].freeze
- SUPPORTED_FONTS =
{ "sans-serif-bi": "sans-serif-bold-italic", "double-struck": "double-struck", "sans-serif-i": "sans-serif-italic", "sans-serif-b": "bold-sans-serif", "sans-serif-p": "sans-serif", "fraktur-p": "fraktur", "fraktur-b": "bold-fraktur", "script-b": "bold-script", "script-p": "script", monospace: "monospace", bi: "bold-italic", p: "normal", i: "italic", b: "bold", }.freeze
Instance Attribute Summary collapse
-
#text ⇒ Object
Returns the value of attribute text.
Instance Method Summary collapse
- #customize_tags(node) ⇒ Object
-
#initialize(text) ⇒ Parser
constructor
A new instance of Parser.
- #organize_fonts(node) ⇒ Object
- #organize_table_td(node) ⇒ Object
- #parse ⇒ Object
- #parse_nodes(nodes) ⇒ Object
Constructor Details
#initialize(text) ⇒ Parser
Returns a new instance of Parser.
31 32 33 |
# File 'lib/plurimath/omml/parser.rb', line 31 def initialize(text) @text = text end |
Instance Attribute Details
#text ⇒ Object
Returns the value of attribute text.
7 8 9 |
# File 'lib/plurimath/omml/parser.rb', line 7 def text @text end |
Instance Method Details
#customize_tags(node) ⇒ Object
64 65 66 67 68 69 70 71 |
# File 'lib/plurimath/omml/parser.rb', line 64 def (node) case node.name when "r" organize_fonts(node) when "mr", "eqArr" organize_table_td(node) end end |
#organize_fonts(node) ⇒ Object
79 80 81 82 83 84 85 |
# File 'lib/plurimath/omml/parser.rb', line 79 def organize_fonts(node) attrs_arr = { val: [] } node.locate("rPr/*").each do |child| attrs_arr[:val] << child.attributes["val"] end node.attributes.merge! attrs_arr end |
#organize_table_td(node) ⇒ Object
73 74 75 76 77 |
# File 'lib/plurimath/omml/parser.rb', line 73 def organize_table_td(node) node.locate("e/*").each do |child_node| child_node.name = "mtd" if child_node.name == "r" end end |
#parse ⇒ Object
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/plurimath/omml/parser.rb', line 35 def parse nodes = Plurimath.xml_engine.load(text) @hash = { sequence: parse_nodes(nodes.nodes) } nodes = JSON.parse(@hash.to_json, symbolize_names: true) Math::Formula.new( Transform.new.apply( nodes, ), ) end |
#parse_nodes(nodes) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/plurimath/omml/parser.rb', line 46 def parse_nodes(nodes) nodes.map do |node| if node.is_a?(String) node == "" ? nil : node elsif !node.attributes.empty? { node.name => { attributes: node.attributes, value: parse_nodes(node.nodes), }, } else (node) if CUSTOMIZABLE_TAGS.include?(node.name) { node.name => parse_nodes(node.nodes) } end end end |