Skip to content

Commit 22b2990

Browse files
jaapiolinawolf
authored andcommitted
[FEATURE] add youtube directive
This new directive allows users to embed youtube video's in their docs.
1 parent 9c04692 commit 22b2990

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

resources/config/guides-restructured-text.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
use phpDocumentor\Guides\RestructuredText\Directives\VersionAddedDirective;
5656
use phpDocumentor\Guides\RestructuredText\Directives\VersionChangedDirective;
5757
use phpDocumentor\Guides\RestructuredText\Directives\WarningDirective;
58+
use phpDocumentor\Guides\RestructuredText\Directives\YoutubeDirective;
5859
use phpDocumentor\Guides\RestructuredText\MarkupLanguageParser;
5960
use phpDocumentor\Guides\RestructuredText\Parser\DocumentParserContextFactory;
6061
use phpDocumentor\Guides\RestructuredText\Parser\InlineParser;
@@ -229,6 +230,7 @@
229230
->set(VersionAddedDirective::class)
230231
->set(VersionChangedDirective::class)
231232
->set(WarningDirective::class)
233+
->set(YoutubeDirective::class)
232234

233235

234236
->set(DefaultTextRoleFactory::class, DefaultTextRoleFactory::class)
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of phpDocumentor.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*
11+
* @link https://phpdoc.org
12+
*/
13+
14+
namespace phpDocumentor\Guides\RestructuredText\Directives;
15+
16+
use phpDocumentor\Guides\Nodes\EmbeddedFrame;
17+
use phpDocumentor\Guides\RestructuredText\Parser\BlockContext;
18+
use phpDocumentor\Guides\RestructuredText\Parser\Directive;
19+
20+
use function array_filter;
21+
22+
/**
23+
* This directive is used to embed a youtube video in the document.
24+
*
25+
* Basic usage
26+
*
27+
* ```rst
28+
* .. youtube:: dQw4w9WgXcQ
29+
* ```
30+
*
31+
* Options:
32+
*
33+
* - string title The title of the video
34+
* - int width The width of the video, default is 560
35+
* - int height The height of the video, default is 315
36+
* - string allow The allow attribute of the iframe, default is 'encrypted-media; picture-in-picture; web-share'
37+
* - bool allowfullscreen Whether the video should be allowed to go fullscreen, default is true
38+
*/
39+
final class YoutubeDirective extends BaseDirective
40+
{
41+
public function getName(): string
42+
{
43+
return 'youtube';
44+
}
45+
46+
public function process(
47+
BlockContext $blockContext,
48+
Directive $directive,
49+
): EmbeddedFrame {
50+
$node = new EmbeddedFrame(
51+
'https://www.youtube-nocookie.com/embed/' . $directive->getData(),
52+
);
53+
54+
return $node->withOptions(
55+
array_filter(
56+
[
57+
'width' => $directive->getOption('width')->getValue() ?? 560,
58+
'title' => $directive->getOption('title')->getValue(),
59+
'height' => $directive->getOption('height')->getValue() ?? 315,
60+
'allow' => $directive->getOption('allow')->getValue() ?? 'encrypted-media; picture-in-picture; web-share',
61+
'allowfullscreen' => (bool) ($directive->getOption('allowfullscreen')->getValue() ?? true),
62+
],
63+
),
64+
);
65+
}
66+
}

0 commit comments

Comments
 (0)