Skip to content

Commit b59ff9e

Browse files
jaapiolinawolf
authored andcommitted
[FEATURE] add youtube directive
This new directive allows users to embed youtube video's in their docs.
1 parent 50d8b00 commit b59ff9e

File tree

8 files changed

+116
-2
lines changed

8 files changed

+116
-2
lines changed

packages/guides-restructured-text/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+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<iframe src="{{ node.url }}"
2+
{%- if node.hasOption('width') %} width="{{ node.option('width') }}"{% endif -%}
3+
{%- if node.hasOption('height') %} height="{{ node.option('height') }}"{% endif -%}
4+
{%- if node.hasOption('title') %} title="{{ node.option('title') }}"{% endif -%}
5+
{%- if node.hasOption('allow') %} allow="{{ node.option('allow') }}"{% endif -%}
6+
{%- if node.hasOption('allowfullscreen') and node.option('allowfullscreen') %} allowfullscreen{% endif -%}
7+
>
8+
</iframe>

packages/guides/resources/template/html/template.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use phpDocumentor\Guides\Nodes\DefinitionListNode;
1111
use phpDocumentor\Guides\Nodes\DefinitionLists\DefinitionNode;
1212
use phpDocumentor\Guides\Nodes\DocumentNode;
13+
use phpDocumentor\Guides\Nodes\EmbeddedFrame;
1314
use phpDocumentor\Guides\Nodes\FieldListNode;
1415
use phpDocumentor\Guides\Nodes\FigureNode;
1516
use phpDocumentor\Guides\Nodes\FootnoteNode;
@@ -76,6 +77,7 @@
7677
CitationNode::class => 'body/citation.html.twig',
7778
FootnoteNode::class => 'body/footnote.html.twig',
7879
AnnotationListNode::class => 'body/annotation-list.html.twig',
80+
EmbeddedFrame::class => 'body/embedded-frame.html.twig',
7981
// Inline
8082
ImageInlineNode::class => 'inline/image.html.twig',
8183
InlineCompoundNode::class => 'inline/inline-node.html.twig',
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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\Nodes;
15+
16+
final class EmbeddedFrame extends TextNode
17+
{
18+
public function __construct(
19+
private readonly string $url,
20+
) {
21+
parent::__construct($url);
22+
}
23+
24+
public function getUrl(): string
25+
{
26+
return $this->url;
27+
}
28+
}
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
<!-- content start -->
2-
.. youtube:: dtRKsxzjKj0
2+
<div class="section" id="document-title">
3+
<h1>Document Title</h1>
34

5+
<iframe src="https://www.youtube-nocookie.com/embed/hdDD0SNJ-pk" width="560" height="315" title="PHP DocBlock - Adding Comments to Classes &amp; Methods" allow="encrypted-media; picture-in-picture; web-share" allowfullscreen>
6+
</iframe>
47

8+
</div>
59
<!-- content end -->
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
1+
==============
2+
Document Title
3+
==============
14

2-
.. youtube:: dtRKsxzjKj0
5+
.. youtube:: hdDD0SNJ-pk
6+
:title: PHP DocBlock - Adding Comments to Classes & Methods

tests/Integration/tests/directives/youtube/input/skip

Whitespace-only changes.

0 commit comments

Comments
 (0)