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

Conversation

javiereguiluz
Copy link
Collaborator

In Symfony Fast Track book we use things like this:

.. figure:: screenshots/foo.png
    :alt: /
    :align: center
    :figclass: with-browser foo

Current parser translates it to:

    <figure>
        <img src="" alt="/" align="center" figclass="with-browser foo">
    </figure>

But we need this:

<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!

public function render(): string
{
var_dump('here');exit;
return $this->templateRenderer->render('figure.html.twig', [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does figureNode->getCssClasses() not have anything here?

@javiereguiluz
Copy link
Collaborator Author

Something curious: in the getNodeRendererFactories() method of src/SymfonyHTMLFormat.php

$nodeRendererFactories[FigureNode::class] = new CallableNodeRendererFactory(
    function (FigureNode $node) {
        return new Renderers\FigureNodeRenderer(
            $node,
            $this->templateRenderer
        );
    }
);

If I put a die('here'); before that code, I see the here word. If I move the die() inside the callable (after function (FigureNode $node) {) then I no longer see it ... so the code never enters there.

@weaverryan
Copy link
Contributor

Hmm, yea. That is curious. I don't remember how that stuff works. However, most directives, I believe, (code being an exception) simply render the template in the directive with no node renderer involved. For example: https://github.com/symfony-tools/docs-builder/blob/main/src/Directive/TopicDirective.php

That's what I would try here

@javiereguiluz
Copy link
Collaborator Author

Thanks for the insights. I tried what you suggested, but it's not working. It doesn't even reach to this die('...') here: https://github.com/symfony-tools/docs-builder/pull/140/files#diff-c0cffc4acc0fb9de41d4fdd2590890c3fbe864e42a16622d724914873de3617fR25

use Doctrine\RST\Nodes\Node;
use Doctrine\RST\Parser;

class FigureDirective extends Directive
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change this to a SubDirective - THEN your processSub() should be called. A SubDirective is one where you want the "inside" of the directive to be parsed like the rest of RST - https://github.com/doctrine/rst-parser/blob/9107ba8b1dc8dc2dda2438cd38c609c74d2c33fe/lib/Directives/SubDirective.php#L12-L20

Apparently we even wrote in SubDirective that figure is an example of one: https://github.com/doctrine/rst-parser/blob/9107ba8b1dc8dc2dda2438cd38c609c74d2c33fe/lib/Directives/SubDirective.php#L59

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your continuous help 🙏 Sadly I still can't make this work.

@weaverryan
Copy link
Contributor

I've fixed this! It should really be fixed upstream, but I'm short on time and wanted to get it working for you.

@javiereguiluz
Copy link
Collaborator Author

Merged! Ryan, thanks a lot for fixing this.

@javiereguiluz javiereguiluz merged commit 3c5531c into symfony-tools:main Mar 7, 2022
@javiereguiluz javiereguiluz deleted the figure_css_class branch March 7, 2022 16:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants