Skip to content

Commit 09ea5cc

Browse files
committed
feature #120 Add support for an alternative screencast directive (javiereguiluz, weaverryan)
This PR was merged into the main branch. Discussion ---------- Add support for an alternative screencast directive In #119 we added the `.. screencast::` directive, but Symfony Docs still use the old `.. admonition:: Screencast` syntax. We cannot update Symfony Docs yet, so we need to support both. I thought that the simple code of this PR would be enough, but it's not working. Anyone knows why? Thanks! Commits ------- add05d8 Fixing test to not rely on screencast style admonition 18dd3dc Fixing initialization or Parser in test f080e8b Add support for an alternative screencast directive
2 parents 5bfe60d + add05d8 commit 09ea5cc

File tree

7 files changed

+59
-9
lines changed

7 files changed

+59
-9
lines changed

src/DocsKernel.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
use Doctrine\RST\ErrorManager;
1616
use Doctrine\RST\Event\PostBuildRenderEvent;
1717
use Doctrine\RST\Event\PreNodeRenderEvent;
18+
use Doctrine\RST\Event\PreParseDocumentEvent;
1819
use Doctrine\RST\Kernel;
20+
use SymfonyDocsBuilder\Listener\AdmonitionListener;
1921
use SymfonyDocsBuilder\Listener\AssetsCopyListener;
2022
use SymfonyDocsBuilder\Listener\CopyImagesListener;
2123

@@ -42,6 +44,11 @@ public function initBuilder(Builder $builder): void
4244

4345
private function initializeListeners(EventManager $eventManager, ErrorManager $errorManager)
4446
{
47+
$eventManager->addEventListener(
48+
PreParseDocumentEvent::PRE_PARSE_DOCUMENT,
49+
new AdmonitionListener()
50+
);
51+
4552
$eventManager->addEventListener(
4653
PreNodeRenderEvent::PRE_NODE_RENDER,
4754
new CopyImagesListener($this->buildConfig, $errorManager)

src/Listener/AdmonitionListener.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Docs Builder package.
7+
* (c) Ryan Weaver <ryan@symfonycasts.com>
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace SymfonyDocsBuilder\Listener;
13+
14+
use Doctrine\RST\Event\PreParseDocumentEvent;
15+
16+
final class AdmonitionListener
17+
{
18+
public function preParseDocument(PreParseDocumentEvent $event)
19+
{
20+
// TODO: remove this temporary fix when Symfony Docs are updated to use the new '.. screencast::' directive
21+
$event->setContents(str_replace('.. admonition:: Screencast', '.. screencast::', $event->getContents()));
22+
}
23+
}

tests/IntegrationTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace SymfonyDocsBuilder\Tests;
1111

12+
use Doctrine\RST\Builder;
1213
use Doctrine\RST\Configuration;
1314
use Doctrine\RST\Parser;
1415
use Gajus\Dindent\Indenter;
@@ -90,9 +91,10 @@ public function testParseUnitBlock(string $blockName)
9091
$configuration = new Configuration();
9192
$configuration->setCustomTemplateDirs([__DIR__.'/Templates']);
9293

93-
$parser = new Parser(
94-
KernelFactory::createKernel($this->createBuildConfig(sprintf('%s/fixtures/source/blocks', __DIR__)))
95-
);
94+
$kernel = KernelFactory::createKernel($this->createBuildConfig(sprintf('%s/fixtures/source/blocks', __DIR__)));
95+
// necessary because this initializes some listeners on the kernel
96+
$builder = new Builder($kernel);
97+
$parser = new Parser($kernel);
9698

9799
$sourceFile = sprintf('%s/fixtures/source/blocks/%s.rst', __DIR__, $blockName);
98100

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
<div class="admonition admonition-default screencast_class">
1+
<div class="admonition admonition-default some_admonition_class">
22
<p class="admonition-title">
3-
<span>Screencast</span>
4-
</p><p>Do you prefer video tutorials? Check out the the screencasts.</p>
3+
<span>Some Admonition</span>
4+
</p><p>Do you prefer admonitions? Well then... enjoy this one!</p>
55
</div>

tests/fixtures/expected/blocks/directives/screencast.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,15 @@
99
<a href="https://symfonycasts.com/screencast/symfony" class="reference external" rel="external noopener noreferrer" target="_blank">Stellar Development with Symfony</a>
1010
screencast series.</p>
1111
</div>
12+
13+
<div class="admonition admonition-screencast ">
14+
<p class="admonition-title">
15+
<svg xmlns="http://www.w3.org/2000/svg" fill="none" width="24" height="24" viewbox="0 0 24 24" stroke="currentColor">
16+
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 10l4.553-2.276A1 1 0 0121 8.618v6.764a1 1 0 01-1.447.894L15 14M5 18h8a2 2 0 002-2V8a2 2 0 00-2-2H5a2 2 0 00-2 2v8a2 2 0 002 2z"></path>
17+
</svg>
18+
<span>Screencast</span>
19+
</p>
20+
<p>Do you prefer video tutorials? Check out the
21+
<a href="https://symfonycasts.com/screencast/symfony" class="reference external" rel="external noopener noreferrer" target="_blank">Stellar Development with Symfony</a>
22+
screencast series.</p>
23+
</div>
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.. admonition:: Screencast
2-
:class: screencast_class
1+
.. admonition:: Some Admonition
2+
:class: some_admonition_class
33

4-
Do you prefer video tutorials? Check out the the screencasts.
4+
Do you prefer admonitions? Well then... enjoy this one!

tests/fixtures/source/blocks/directives/screencast.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,9 @@
33
Do you prefer video tutorials? Check out the
44
`Stellar Development with Symfony <https://symfonycasts.com/screencast/symfony>`_
55
screencast series.
6+
7+
.. admonition:: Screencast
8+
9+
Do you prefer video tutorials? Check out the
10+
`Stellar Development with Symfony <https://symfonycasts.com/screencast/symfony>`_
11+
screencast series.

0 commit comments

Comments
 (0)