Skip to content

MathML Writer : Write Semantics #20

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

Merged
merged 1 commit into from
May 27, 2025
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
1 change: 1 addition & 0 deletions docs/changes/0.3.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
23 changes: 23 additions & 0 deletions src/Math/Writer/MathML.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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',
Expand Down
29 changes: 29 additions & 0 deletions tests/Math/Writer/MathMLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<?xml version="1.0" encoding="UTF-8"?>'
. PHP_EOL
. '<!DOCTYPE math PUBLIC "-//W3C//DTD MathML 2.0//EN" "http://www.w3.org/Math/DTD/mathml2/mathml2.dtd">'
. '<math xmlns="http://www.w3.org/1998/Math/MathML">'
. '<semantics>'
. '<mi>y</mi>'
. '<annotation encoding="application/x-tex"> y </annotation>'
. '</semantics>'
. '</math>'
. PHP_EOL;
$this->assertEquals($expected, $output);
$this->assertIsSchemaMathMLValid($output);
}

public function testWriteNotImplemented(): void
{
$this->expectException(NotImplementedException::class);
Expand Down