Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use phpDocumentor\Guides\RestructuredText\Directives\LaTeXMain;
use phpDocumentor\Guides\RestructuredText\Directives\ListTableDirective;
use phpDocumentor\Guides\RestructuredText\Directives\LiteralincludeDirective;
use phpDocumentor\Guides\RestructuredText\Directives\MathDirective;
use phpDocumentor\Guides\RestructuredText\Directives\MenuDirective;
use phpDocumentor\Guides\RestructuredText\Directives\MetaDirective;
use phpDocumentor\Guides\RestructuredText\Directives\NoteDirective;
Expand Down Expand Up @@ -213,6 +214,7 @@
CodeNodeOptionMapper::class,
),
])
->set(MathDirective::class)
->set(MetaDirective::class)
->set(NoteDirective::class)
->set(OptionDirective::class)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @link https://phpdoc.org
*/

namespace phpDocumentor\Guides\RestructuredText\Directives;

use phpDocumentor\Guides\Nodes\MathNode;
use phpDocumentor\Guides\Nodes\Node;
use phpDocumentor\Guides\RestructuredText\Parser\BlockContext;
use phpDocumentor\Guides\RestructuredText\Parser\Directive;
use Psr\Log\LoggerInterface;

/**
* Renders a code block, example:
*
* .. code-block:: php
*
* <?php
*
* echo "Hello world!\n";
*
* @link https://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html#directive-code-block
*/
final class MathDirective extends BaseDirective
{
public function __construct(
private readonly LoggerInterface $logger,
) {
}

public function getName(): string
{
return 'math';
}

/** {@inheritDoc} */
public function getAliases(): array
{
return [];
}

/** {@inheritDoc} */
public function process(
BlockContext $blockContext,
Directive $directive,
): Node|null {
if ($blockContext->getDocumentIterator()->isEmpty()) {
$this->logger->warning('The math directive has no content. Did you properly indent the code? ', $blockContext->getLoggerInformation());

return null;
}

return new MathNode(
$blockContext->getDocumentIterator()->toArray(),
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<math{%- if node.class %} class="{{ node.class }}"{% endif %}>{{ node.value|raw }}</math>
2 changes: 2 additions & 0 deletions packages/guides/resources/template/html/template.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use phpDocumentor\Guides\Nodes\ListItemNode;
use phpDocumentor\Guides\Nodes\ListNode;
use phpDocumentor\Guides\Nodes\LiteralBlockNode;
use phpDocumentor\Guides\Nodes\MathNode;
use phpDocumentor\Guides\Nodes\Metadata\AddressNode;
use phpDocumentor\Guides\Nodes\Metadata\AuthorNode;
use phpDocumentor\Guides\Nodes\Metadata\AuthorsNode;
Expand Down Expand Up @@ -75,6 +76,7 @@
ListNode::class => 'body/list/list.html.twig',
ListItemNode::class => 'body/list/list-item.html.twig',
LiteralBlockNode::class => 'body/literal-block.html.twig',
MathNode::class => 'body/math.html.twig',
CitationNode::class => 'body/citation.html.twig',
FootnoteNode::class => 'body/footnote.html.twig',
AnnotationListNode::class => 'body/annotation-list.html.twig',
Expand Down
25 changes: 25 additions & 0 deletions packages/guides/src/Nodes/MathNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @link https://phpdoc.org
*/

namespace phpDocumentor\Guides\Nodes;

use function implode;

final class MathNode extends TextNode
{
/** @param string[] $lines */
public function __construct(array $lines)
{
parent::__construct(implode("\n", $lines));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!-- content start -->
<div class="section" id="directive-tests">
<h1>Directive tests</h1>

<math><mrow>
<mi>α</mi>
<mi>t</mi>
<mo stretchy="false">(</mo>
<mi>i</mi>
<mo stretchy="false">)</mo>
<mo>=</mo>
<mi>P</mi>
<mo stretchy="false">(</mo>
<mi>O</mi>
<msub>
<mi>t</mi>
<mo>,</mo>
<mi>i</mi>
</msub>
<mo>=</mo>
<mi>S</mi>
<mi>i</mi>
<mi>λ</mi>
<mo stretchy="false">)</mo>
</mrow></math>

</div>

<!-- content end -->
26 changes: 26 additions & 0 deletions tests/Integration/tests/directives/directive-math/input/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Directive tests
===============

.. math::

<mrow>
<mi>α</mi>
<mi>t</mi>
<mo stretchy="false">(</mo>
<mi>i</mi>
<mo stretchy="false">)</mo>
<mo>=</mo>
<mi>P</mi>
<mo stretchy="false">(</mo>
<mi>O</mi>
<msub>
<mi>t</mi>
<mo>,</mo>
<mi>i</mi>
</msub>
<mo>=</mo>
<mi>S</mi>
<mi>i</mi>
<mi>λ</mi>
<mo stretchy="false">)</mo>
</mrow>