diff --git a/src/Directive/FigureDirective.php b/src/Directive/FigureDirective.php new file mode 100644 index 0000000..43cfdd2 --- /dev/null +++ b/src/Directive/FigureDirective.php @@ -0,0 +1,64 @@ + + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace SymfonyDocsBuilder\Directive; + +use Doctrine\RST\Directives\SubDirective; +use Doctrine\RST\Nodes\Node; +use Doctrine\RST\Parser; + +/** + * Overridden to handle "figclass" properly. + */ +class FigureDirective extends SubDirective +{ + public function getName(): string + { + return 'figure'; + } + + /** + * @param string[] $options + */ + public function processSub( + Parser $parser, + ?Node $document, + string $variable, + string $data, + array $options + ): ?Node { + $environment = $parser->getEnvironment(); + + $url = $environment->relativeUrl($data); + + if ($url === null) { + throw new \Exception(sprintf('Could not get relative url for %s', $data)); + } + + $nodeFactory = $parser->getNodeFactory(); + + /* Start Custom Code */ + $figClass = $options['figclass'] ?? null; + unset($options['figclass']); + /* End Custom Code */ + + $figureNode = $parser->getNodeFactory()->createFigureNode( + $nodeFactory->createImageNode($url, $options), + $document + ); + + /* Start Custom Code */ + if ($figClass) { + $figureNode->setClasses(explode(' ', $figClass)); + } + /* End Custom Code */ + + return $figureNode; + } +} diff --git a/src/KernelFactory.php b/src/KernelFactory.php index 3a17b47..f886634 100644 --- a/src/KernelFactory.php +++ b/src/KernelFactory.php @@ -80,6 +80,7 @@ private static function getDirectives(): array new SymfonyDirectives\DangerDirective(), new SymfonyDirectives\DeprecatedDirective(), new SymfonyDirectives\ErrorDirective(), + new SymfonyDirectives\FigureDirective(), new SymfonyDirectives\HintDirective(), new SymfonyDirectives\ImportantDirective(), new SymfonyDirectives\IndexDirective(), diff --git a/src/Templates/default/html/figure.html.twig b/src/Templates/default/html/figure.html.twig new file mode 100644 index 0000000..e1786c0 --- /dev/null +++ b/src/Templates/default/html/figure.html.twig @@ -0,0 +1,20 @@ +{# +Overridden to fix lack of figclass support (class attribute on + {{ figureNode.image.render()|raw }} + + {% if figureNode.document %} + {% set caption = figureNode.document.render()|trim %} + + {% if caption %} +
{{ caption|raw }}
+ {% endif %} + {% endif %} + +{% endapply %} diff --git a/tests/fixtures/expected/blocks/nodes/figure.html b/tests/fixtures/expected/blocks/nodes/figure.html index 16042ca..d50411d 100644 --- a/tests/fixtures/expected/blocks/nodes/figure.html +++ b/tests/fixtures/expected/blocks/nodes/figure.html @@ -15,3 +15,9 @@
A typical exception page in the development environment
+ +

And RST figures use a different syntax to define their custom CSS classes:

+ +
+ / +
diff --git a/tests/fixtures/source/blocks/nodes/figure.rst b/tests/fixtures/source/blocks/nodes/figure.rst index aef414f..b1d6361 100644 --- a/tests/fixtures/source/blocks/nodes/figure.rst +++ b/tests/fixtures/source/blocks/nodes/figure.rst @@ -15,3 +15,10 @@ Some images use a special CSS class to wrap a fake browser around them: :alt: A typical exception page in the development environment :align: center :class: some-class with-browser another-class + +And RST figures use a different syntax to define their custom CSS classes: + +.. figure:: images/logo.png + :alt: / + :align: center + :figclass: with-browser foo