Skip to content

Commit 3c5531c

Browse files
committed
feature #140 Add support for figures with custom CSS classes (javiereguiluz, weaverryan)
This PR was merged into the main branch. Discussion ---------- Add support for figures with custom CSS classes In Symfony Fast Track book we use things like this: ```rst .. figure:: screenshots/foo.png :alt: / :align: center :figclass: with-browser foo ``` Current parser translates it to: ```html <figure> <img src="" alt="/" align="center" figclass="with-browser foo"> </figure> ``` But we need this: ```html <div class="with-browser foo"> <figure> <img src="" alt="/" align="center" figclass="with-browser foo"> </figure> </div> ``` We need that because `with-browser` class adds a "fake browser" window wrapping the image. But that fake browser is made with CSS `::before` and `::after` pseudo-elements, which don't work for `<figure>` or `<img>` and that's why we need the wrapping `<div>`. Sadly, and as usual, I don't know what I'm doing and it doesn't work, so I'd need your help. Thanks! Commits ------- e2e384c unneeded logic 61761e5 cleanup and notes b7f351b Overriding FigureDirective to fix lack of support for figclass ba60661 - a81fc94 - b63440f - 676e8c7 Add support for figures with custom CSS classes
2 parents 06ce934 + e2e384c commit 3c5531c

File tree

5 files changed

+98
-0
lines changed

5 files changed

+98
-0
lines changed

src/Directive/FigureDirective.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Docs Builder package.
5+
* (c) Ryan Weaver <ryan@symfonycasts.com>
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
namespace SymfonyDocsBuilder\Directive;
11+
12+
use Doctrine\RST\Directives\SubDirective;
13+
use Doctrine\RST\Nodes\Node;
14+
use Doctrine\RST\Parser;
15+
16+
/**
17+
* Overridden to handle "figclass" properly.
18+
*/
19+
class FigureDirective extends SubDirective
20+
{
21+
public function getName(): string
22+
{
23+
return 'figure';
24+
}
25+
26+
/**
27+
* @param string[] $options
28+
*/
29+
public function processSub(
30+
Parser $parser,
31+
?Node $document,
32+
string $variable,
33+
string $data,
34+
array $options
35+
): ?Node {
36+
$environment = $parser->getEnvironment();
37+
38+
$url = $environment->relativeUrl($data);
39+
40+
if ($url === null) {
41+
throw new \Exception(sprintf('Could not get relative url for %s', $data));
42+
}
43+
44+
$nodeFactory = $parser->getNodeFactory();
45+
46+
/* Start Custom Code */
47+
$figClass = $options['figclass'] ?? null;
48+
unset($options['figclass']);
49+
/* End Custom Code */
50+
51+
$figureNode = $parser->getNodeFactory()->createFigureNode(
52+
$nodeFactory->createImageNode($url, $options),
53+
$document
54+
);
55+
56+
/* Start Custom Code */
57+
if ($figClass) {
58+
$figureNode->setClasses(explode(' ', $figClass));
59+
}
60+
/* End Custom Code */
61+
62+
return $figureNode;
63+
}
64+
}

src/KernelFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ private static function getDirectives(): array
8080
new SymfonyDirectives\DangerDirective(),
8181
new SymfonyDirectives\DeprecatedDirective(),
8282
new SymfonyDirectives\ErrorDirective(),
83+
new SymfonyDirectives\FigureDirective(),
8384
new SymfonyDirectives\HintDirective(),
8485
new SymfonyDirectives\ImportantDirective(),
8586
new SymfonyDirectives\IndexDirective(),
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{#
2+
Overridden to fix lack of figclass support (class attribute on <figure)
3+
#}
4+
{% apply spaceless %}
5+
<figure
6+
{% if figureNode.classes is not empty %}
7+
class="{{ figureNode.classesString }}"
8+
{% endif %}
9+
>
10+
{{ figureNode.image.render()|raw }}
11+
12+
{% if figureNode.document %}
13+
{% set caption = figureNode.document.render()|trim %}
14+
15+
{% if caption %}
16+
<figcaption>{{ caption|raw }}</figcaption>
17+
{% endif %}
18+
{% endif %}
19+
</figure>
20+
{% endapply %}

tests/fixtures/expected/blocks/nodes/figure.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,9 @@
1515
<div class="with-browser">
1616
<img src="" alt="A typical exception page in the development environment" align="center" class="some-class with-browser another-class">
1717
</div>
18+
19+
<p>And RST figures use a different syntax to define their custom CSS classes:</p>
20+
21+
<figure class="with-browser foo">
22+
<img src="" alt="/" align="center">
23+
</figure>

tests/fixtures/source/blocks/nodes/figure.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,10 @@ Some images use a special CSS class to wrap a fake browser around them:
1515
:alt: A typical exception page in the development environment
1616
:align: center
1717
:class: some-class with-browser another-class
18+
19+
And RST figures use a different syntax to define their custom CSS classes:
20+
21+
.. figure:: images/logo.png
22+
:alt: /
23+
:align: center
24+
:figclass: with-browser foo

0 commit comments

Comments
 (0)