Skip to content

Commit dae1810

Browse files
committed
[FEATURE] Support math directive
resolves: #928 releases: main, 1.0
1 parent b95c614 commit dae1810

File tree

7 files changed

+151
-0
lines changed

7 files changed

+151
-0
lines changed

packages/guides-restructured-text/resources/config/guides-restructured-text.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
use phpDocumentor\Guides\RestructuredText\Directives\LaTeXMain;
3535
use phpDocumentor\Guides\RestructuredText\Directives\ListTableDirective;
3636
use phpDocumentor\Guides\RestructuredText\Directives\LiteralincludeDirective;
37+
use phpDocumentor\Guides\RestructuredText\Directives\MathDirective;
3738
use phpDocumentor\Guides\RestructuredText\Directives\MenuDirective;
3839
use phpDocumentor\Guides\RestructuredText\Directives\MetaDirective;
3940
use phpDocumentor\Guides\RestructuredText\Directives\NoteDirective;
@@ -213,6 +214,7 @@
213214
CodeNodeOptionMapper::class,
214215
),
215216
])
217+
->set(MathDirective::class)
216218
->set(MetaDirective::class)
217219
->set(NoteDirective::class)
218220
->set(OptionDirective::class)
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of phpDocumentor.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*
11+
* @link https://phpdoc.org
12+
*/
13+
14+
namespace phpDocumentor\Guides\RestructuredText\Directives;
15+
16+
use phpDocumentor\Guides\Nodes\MathNode;
17+
use phpDocumentor\Guides\Nodes\Node;
18+
use phpDocumentor\Guides\RestructuredText\Parser\BlockContext;
19+
use phpDocumentor\Guides\RestructuredText\Parser\Directive;
20+
use Psr\Log\LoggerInterface;
21+
22+
/**
23+
* Renders a code block, example:
24+
*
25+
* .. code-block:: php
26+
*
27+
* <?php
28+
*
29+
* echo "Hello world!\n";
30+
*
31+
* @link https://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html#directive-code-block
32+
*/
33+
final class MathDirective extends BaseDirective
34+
{
35+
public function __construct(
36+
private readonly LoggerInterface $logger,
37+
) {
38+
}
39+
40+
public function getName(): string
41+
{
42+
return 'math';
43+
}
44+
45+
/** {@inheritDoc} */
46+
public function getAliases(): array
47+
{
48+
return [];
49+
}
50+
51+
/** {@inheritDoc} */
52+
public function process(
53+
BlockContext $blockContext,
54+
Directive $directive,
55+
): Node|null {
56+
if ($blockContext->getDocumentIterator()->isEmpty()) {
57+
$this->logger->warning('The math directive has no content. Did you properly indent the code? ', $blockContext->getLoggerInformation());
58+
59+
return null;
60+
}
61+
62+
return new MathNode(
63+
$blockContext->getDocumentIterator()->toArray(),
64+
);
65+
}
66+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<math{%- if node.class %} class="{{ node.class }}"{% endif %}>{{ node.value|raw }}</math>

packages/guides/resources/template/html/template.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
use phpDocumentor\Guides\Nodes\ListItemNode;
3535
use phpDocumentor\Guides\Nodes\ListNode;
3636
use phpDocumentor\Guides\Nodes\LiteralBlockNode;
37+
use phpDocumentor\Guides\Nodes\MathNode;
3738
use phpDocumentor\Guides\Nodes\Metadata\AddressNode;
3839
use phpDocumentor\Guides\Nodes\Metadata\AuthorNode;
3940
use phpDocumentor\Guides\Nodes\Metadata\AuthorsNode;
@@ -75,6 +76,7 @@
7576
ListNode::class => 'body/list/list.html.twig',
7677
ListItemNode::class => 'body/list/list-item.html.twig',
7778
LiteralBlockNode::class => 'body/literal-block.html.twig',
79+
MathNode::class => 'body/math.html.twig',
7880
CitationNode::class => 'body/citation.html.twig',
7981
FootnoteNode::class => 'body/footnote.html.twig',
8082
AnnotationListNode::class => 'body/annotation-list.html.twig',
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of phpDocumentor.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*
11+
* @link https://phpdoc.org
12+
*/
13+
14+
namespace phpDocumentor\Guides\Nodes;
15+
16+
use function implode;
17+
18+
final class MathNode extends TextNode
19+
{
20+
/** @param string[] $lines */
21+
public function __construct(array $lines)
22+
{
23+
parent::__construct(implode("\n", $lines));
24+
}
25+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<!-- content start -->
2+
<div class="section" id="directive-tests">
3+
<h1>Directive tests</h1>
4+
5+
<math><mrow>
6+
<mi>α</mi>
7+
<mi>t</mi>
8+
<mo stretchy="false">(</mo>
9+
<mi>i</mi>
10+
<mo stretchy="false">)</mo>
11+
<mo>=</mo>
12+
<mi>P</mi>
13+
<mo stretchy="false">(</mo>
14+
<mi>O</mi>
15+
<msub>
16+
<mi>t</mi>
17+
<mo>,</mo>
18+
<mi>i</mi>
19+
</msub>
20+
<mo>=</mo>
21+
<mi>S</mi>
22+
<mi>i</mi>
23+
<mi>λ</mi>
24+
<mo stretchy="false">)</mo>
25+
</mrow></math>
26+
27+
</div>
28+
29+
<!-- content end -->
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
Directive tests
2+
===============
3+
4+
.. math::
5+
6+
<mrow>
7+
<mi>α</mi>
8+
<mi>t</mi>
9+
<mo stretchy="false">(</mo>
10+
<mi>i</mi>
11+
<mo stretchy="false">)</mo>
12+
<mo>=</mo>
13+
<mi>P</mi>
14+
<mo stretchy="false">(</mo>
15+
<mi>O</mi>
16+
<msub>
17+
<mi>t</mi>
18+
<mo>,</mo>
19+
<mi>i</mi>
20+
</msub>
21+
<mo>=</mo>
22+
<mi>S</mi>
23+
<mi>i</mi>
24+
<mi>λ</mi>
25+
<mo stretchy="false">)</mo>
26+
</mrow>

0 commit comments

Comments
 (0)