Skip to content

Commit 6ea0c81

Browse files
authored
MathML Writer : Write Semantics (#20)
1 parent c3ecbf3 commit 6ea0c81

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed

docs/changes/0.3.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Enhancements
44

55
- Added support for PHP 8.4 by [@Progi1984](https://github/Progi1984) in [#18](https://github.com/PHPOffice/Math/pull/18)
6+
- 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)
67

78
## Bug fixes
89

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Math is an open source project licensed under the terms of [MIT](https://github.
4848
| **Simple** | Fraction | :material-check: | :material-check: |
4949
| | Superscript | :material-check: | |
5050
| **Architectural** | Row | :material-check: | :material-check: |
51-
| | Semantics | | |
51+
| | Semantics | :material-check: | |
5252

5353
## Contributing
5454

src/Math/Writer/MathML.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,26 @@ protected function writeElementItem(Element\AbstractElement $element): void
4040
{
4141
$tagName = $this->getElementTagName($element);
4242

43+
// Element\AbstractGroupElement
44+
if ($element instanceof Element\Semantics) {
45+
$this->output->startElement($tagName);
46+
// Write elements
47+
foreach ($element->getElements() as $childElement) {
48+
$this->writeElementItem($childElement);
49+
}
50+
51+
// Write annotations
52+
foreach ($element->getAnnotations() as $encoding => $annotation) {
53+
$this->output->startElement('annotation');
54+
$this->output->writeAttribute('encoding', $encoding);
55+
$this->output->text($annotation);
56+
$this->output->endElement();
57+
}
58+
$this->output->endElement();
59+
60+
return;
61+
}
62+
4363
// Element\AbstractGroupElement
4464
if ($element instanceof Element\AbstractGroupElement) {
4565
$this->output->startElement($tagName);
@@ -121,6 +141,9 @@ protected function getElementTagName(Element\AbstractElement $element): string
121141
if ($element instanceof Element\Operator) {
122142
return 'mo';
123143
}
144+
if ($element instanceof Element\Semantics) {
145+
return 'semantics';
146+
}
124147

125148
throw new NotImplementedException(sprintf(
126149
'%s : The element of the class `%s` has no tag name',

tests/Math/Writer/MathMLTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,35 @@ public function testWriteFraction(): void
8080
$this->assertIsSchemaMathMLValid($output);
8181
}
8282

83+
public function testWriteSemantics(): void
84+
{
85+
$opTimes = new Element\Operator('⁢');
86+
87+
$math = new Math();
88+
89+
$semantics = new Element\Semantics();
90+
$semantics->add(new Element\Identifier('y'));
91+
$semantics->addAnnotation('application/x-tex', ' y ');
92+
93+
$math->add($semantics);
94+
95+
$writer = new MathML();
96+
$output = $writer->write($math);
97+
98+
$expected = '<?xml version="1.0" encoding="UTF-8"?>'
99+
. PHP_EOL
100+
. '<!DOCTYPE math PUBLIC "-//W3C//DTD MathML 2.0//EN" "http://www.w3.org/Math/DTD/mathml2/mathml2.dtd">'
101+
. '<math xmlns="http://www.w3.org/1998/Math/MathML">'
102+
. '<semantics>'
103+
. '<mi>y</mi>'
104+
. '<annotation encoding="application/x-tex"> y </annotation>'
105+
. '</semantics>'
106+
. '</math>'
107+
. PHP_EOL;
108+
$this->assertEquals($expected, $output);
109+
$this->assertIsSchemaMathMLValid($output);
110+
}
111+
83112
public function testWriteNotImplemented(): void
84113
{
85114
$this->expectException(NotImplementedException::class);

0 commit comments

Comments
 (0)