Skip to content

Commit

Permalink
MathML Reader : Support for Semantics
Browse files Browse the repository at this point in the history
  • Loading branch information
Progi1984 committed Sep 21, 2023
1 parent 07315c1 commit 26eede9
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/Math/Element/Semantics.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace PhpOffice\Math\Element;

class Semantics extends AbstractGroupElement
{
/**
* @var array<string, string>
*/
protected $annotations = [];

public function addAnnotation(string $encoding, string $annotation): self
{
$this->annotations[$encoding] = $annotation;

return $this;
}

public function getAnnotation(string $encoding): ?string
{
return $this->annotations[$encoding] ?? null;
}

/**
* @return array<string, string>
*/
public function getAnnotations(): array
{
return $this->annotations;
}
}
13 changes: 13 additions & 0 deletions src/Math/Reader/MathML.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ protected function parseNode(?DOMNode $nodeRowElement, $parent): void
{
$this->xpath = new DOMXpath($this->dom);
foreach ($this->xpath->query('*', $nodeRowElement) ?: [] as $nodeElement) {
if ($parent instanceof Element\Semantics
&& $nodeElement instanceof DOMElement
&& $nodeElement->nodeName == 'annotation') {
$parent->addAnnotation(
$nodeElement->getAttribute('encoding'),
trim($nodeElement->nodeValue)
);

continue;
}

$element = $this->getElement($nodeElement);
$parent->add($element);

Expand Down Expand Up @@ -103,6 +114,8 @@ protected function getElement(DOMNode $nodeElement): Element\AbstractElement
}

return $element;
case 'semantics':
return new Element\Semantics();
default:
throw new Exception(sprintf(
'%s : The tag `%s` is not implemented',
Expand Down
45 changes: 45 additions & 0 deletions tests/Math/Reader/MathMLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,49 @@ public function testReadFraction(): void
$this->assertInstanceOf(Element\Identifier::class, $denominator);
$this->assertEquals('d', $denominator->getValue());
}

/**
* @covers \MathML::read
*/
public function testReadSemantics(): void
{
$content = '<?xml version="1.0" encoding="UTF-8"?>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block">
<semantics>
<mrow>
<mfrac>
<mi>π</mi>
<mn>2</mn>
</mfrac>
<mo stretchy="false">+</mo>
<mrow>
<mi>a</mi>
<mo stretchy="false">∗</mo>
<mn>2</mn>
</mrow>
</mrow>
<annotation encoding="StarMath 5.0">{π} over {2} + { a } * 2 </annotation>
</semantics>
</math>';

$reader = new MathML();
$math = $reader->read($content);
$this->assertInstanceOf(Math::class, $math);

$elements = $math->getElements();
$this->assertCount(1, $elements);
$this->assertInstanceOf(Element\Semantics::class, $elements[0]);

/** @var Element\Semantics $element */
$element = $elements[0];

// Check MathML
$subElements = $element->getElements();
$this->assertCount(1, $subElements);
$this->assertInstanceOf(Element\Row::class, $subElements[0]);

// Check Annotation
$this->assertCount(1, $element->getAnnotations());
$this->assertEquals('{π} over {2} + { a } * 2', $element->getAnnotation('StarMath 5.0'));
}
}

0 comments on commit 26eede9

Please sign in to comment.