diff --git a/docs/changes/0.3.0.md b/docs/changes/0.3.0.md index 26dbcc0..501ae67 100644 --- a/docs/changes/0.3.0.md +++ b/docs/changes/0.3.0.md @@ -3,6 +3,7 @@ ## Enhancements - Added support for PHP 8.4 by [@Progi1984](https://github/Progi1984) in [#18](https://github.com/PHPOffice/Math/pull/18) +- MathML Writer : Write Semantics by [@codinglist](https://github/codinglist) & [@Progi1984](https://github/Progi1984) in [#19](https://github.com/PHPOffice/Math/pull/19) & [#20](https://github.com/PHPOffice/Math/pull/20) ## Bug fixes diff --git a/docs/index.md b/docs/index.md index 340b0dc..80b4333 100644 --- a/docs/index.md +++ b/docs/index.md @@ -48,7 +48,7 @@ Math is an open source project licensed under the terms of [MIT](https://github. | **Simple** | Fraction | :material-check: | :material-check: | | | Superscript | :material-check: | | | **Architectural** | Row | :material-check: | :material-check: | -| | Semantics | | | +| | Semantics | :material-check: | | ## Contributing diff --git a/src/Math/Writer/MathML.php b/src/Math/Writer/MathML.php index d8905f0..34a20dc 100644 --- a/src/Math/Writer/MathML.php +++ b/src/Math/Writer/MathML.php @@ -40,6 +40,26 @@ protected function writeElementItem(Element\AbstractElement $element): void { $tagName = $this->getElementTagName($element); + // Element\AbstractGroupElement + if ($element instanceof Element\Semantics) { + $this->output->startElement($tagName); + // Write elements + foreach ($element->getElements() as $childElement) { + $this->writeElementItem($childElement); + } + + // Write annotations + foreach ($element->getAnnotations() as $encoding => $annotation) { + $this->output->startElement('annotation'); + $this->output->writeAttribute('encoding', $encoding); + $this->output->text($annotation); + $this->output->endElement(); + } + $this->output->endElement(); + + return; + } + // Element\AbstractGroupElement if ($element instanceof Element\AbstractGroupElement) { $this->output->startElement($tagName); @@ -121,6 +141,9 @@ protected function getElementTagName(Element\AbstractElement $element): string if ($element instanceof Element\Operator) { return 'mo'; } + if ($element instanceof Element\Semantics) { + return 'semantics'; + } throw new NotImplementedException(sprintf( '%s : The element of the class `%s` has no tag name', diff --git a/tests/Math/Writer/MathMLTest.php b/tests/Math/Writer/MathMLTest.php index 06aac38..9072485 100644 --- a/tests/Math/Writer/MathMLTest.php +++ b/tests/Math/Writer/MathMLTest.php @@ -80,6 +80,35 @@ public function testWriteFraction(): void $this->assertIsSchemaMathMLValid($output); } + public function testWriteSemantics(): void + { + $opTimes = new Element\Operator('⁢'); + + $math = new Math(); + + $semantics = new Element\Semantics(); + $semantics->add(new Element\Identifier('y')); + $semantics->addAnnotation('application/x-tex', ' y '); + + $math->add($semantics); + + $writer = new MathML(); + $output = $writer->write($math); + + $expected = '' + . PHP_EOL + . '' + . '' + . '' + . 'y' + . ' y ' + . '' + . '' + . PHP_EOL; + $this->assertEquals($expected, $output); + $this->assertIsSchemaMathMLValid($output); + } + public function testWriteNotImplemented(): void { $this->expectException(NotImplementedException::class);