Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MathML Reader : Support for Semantics #4

Merged
merged 1 commit into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'));
}
}
Loading