Skip to content

Add support for figures with custom CSS classes #140

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 7 commits into from
Mar 7, 2022
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
64 changes: 64 additions & 0 deletions src/Directive/FigureDirective.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

/*
* This file is part of the Docs Builder package.
* (c) Ryan Weaver <ryan@symfonycasts.com>
* 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;
}
}
1 change: 1 addition & 0 deletions src/KernelFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
20 changes: 20 additions & 0 deletions src/Templates/default/html/figure.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{#
Overridden to fix lack of figclass support (class attribute on <figure)
#}
{% apply spaceless %}
<figure
{% if figureNode.classes is not empty %}
class="{{ figureNode.classesString }}"
{% endif %}
>
{{ figureNode.image.render()|raw }}

{% if figureNode.document %}
{% set caption = figureNode.document.render()|trim %}

{% if caption %}
<figcaption>{{ caption|raw }}</figcaption>
{% endif %}
{% endif %}
</figure>
{% endapply %}
6 changes: 6 additions & 0 deletions tests/fixtures/expected/blocks/nodes/figure.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,9 @@
<div class="with-browser">
<img src="" alt="A typical exception page in the development environment" align="center" class="some-class with-browser another-class">
</div>

<p>And RST figures use a different syntax to define their custom CSS classes:</p>

<figure class="with-browser foo">
<img src="" alt="/" align="center">
</figure>
7 changes: 7 additions & 0 deletions tests/fixtures/source/blocks/nodes/figure.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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