Skip to content

Commit 09bd229

Browse files
committed
add best practice / fix class directive
1 parent 474fcbe commit 09bd229

File tree

11 files changed

+113
-14
lines changed

11 files changed

+113
-14
lines changed

_build/notes.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@ Notes
2121
- add `&nbsp` instead of simple space to lines in code blocks
2222
- test if there is only one <h1> in each page
2323
- we're assuming there is only one toctree per page...
24+
- `rst` does not exist in highlight php
25+
- `varnish` = C ? (highlight php)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace SymfonyDocs\Directive;
4+
5+
use Doctrine\RST\Nodes\Node;
6+
use Doctrine\RST\Nodes\WrapperNode;
7+
use Doctrine\RST\Parser;
8+
use Doctrine\RST\SubDirective;
9+
10+
class BestPracticeDirective extends SubDirective
11+
{
12+
public function getName(): string
13+
{
14+
return 'best-practice';
15+
}
16+
17+
/**
18+
* @param string[] $options
19+
*/
20+
public function processSub(
21+
Parser $parser,
22+
?Node $document,
23+
string $variable,
24+
string $data,
25+
array $options
26+
): ?Node {
27+
return new WrapperNode(
28+
$document,
29+
'<div class="admonition-best-practice admonition-wrapper"><div class="best-practice"></div><div class="admonition admonition-best-practice"><p class="admonition-title">Best Practice</p>',
30+
'</div></div>'
31+
);
32+
}
33+
}

_build/src/Directive/ClassDirective.php

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
namespace SymfonyDocs\Directive;
44

55
use Doctrine\RST\Nodes\Node;
6-
use Doctrine\RST\Nodes\RawNode;
76
use Doctrine\RST\Parser;
87
use Doctrine\RST\SubDirective;
98

109
class ClassDirective extends SubDirective
1110
{
12-
public function getName() : string
11+
public function getName(): string
1312
{
1413
return 'class';
1514
}
@@ -23,17 +22,9 @@ public function processSub(
2322
string $variable,
2423
string $data,
2524
array $options
26-
) : ?Node {
27-
$dOMDocument = new \DOMDocument();
28-
$dOMDocument->loadHTML((string) $document, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
25+
): ?Node {
26+
$document->setClass($data);
2927

30-
/** @var \DOMElement $firstNode */
31-
$firstNode = $dOMDocument->childNodes[0];
32-
$firstNodeClass = $firstNode->getAttribute('class');
33-
$firstNode->setAttribute('class', trim(sprintf('%s %s', $firstNodeClass, $data)));
34-
35-
return new RawNode($dOMDocument->saveHTML());
28+
return $document;
3629
}
37-
38-
3930
}

_build/src/HtmlKernel.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Doctrine\RST\Directive;
66
use Doctrine\RST\Factory;
77
use Doctrine\RST\HTML\Kernel;
8+
use SymfonyDocs\Directive\BestPracticeDirective;
89
use SymfonyDocs\Directive\CautionDirective;
910
use SymfonyDocs\Directive\ClassDirective;
1011
use SymfonyDocs\Directive\CodeBlockDirective;
@@ -89,6 +90,7 @@ public function getDirectives(): array
8990
new SidebarDirective(),
9091
new TipDirective(),
9192
new VersionAddedDirective(),
93+
new BestPracticeDirective(),
9294
]
9395
);
9496
}

_build/src/Nodes/CodeNode.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ class CodeNode extends Base
2020
'php-annotations' => 'php',
2121
'text' => 'text',
2222
'terminal' => 'bash',
23+
'markdown' => 'markdown',
24+
'rst' => 'markdown',
25+
'php-standalone' => 'php',
26+
'php-symfony' => 'php',
27+
'varnish4' => 'c',
28+
'varnish3' => 'c',
2329
];
2430

2531
private const CODE_BLOCK_TEMPLATE = '<div class="literal-block notranslate">

_build/src/Nodes/ListNode.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
class ListNode extends Base
88
{
9+
/** @var string */
10+
private $class = '';
11+
912
protected function createElement(string $text, string $prefix): string
1013
{
1114
return sprintf('<li>%s</li>', $text);
@@ -19,8 +22,28 @@ protected function createList(bool $ordered): array
1922
$keyword = $ordered ? 'ol' : 'ul';
2023

2124
return [
22-
sprintf('<%s class="simple">', $keyword),
25+
sprintf('<%s class="%s">', $keyword, trim('simple '.$this->class)),
2326
sprintf('</%s>', $keyword),
2427
];
2528
}
29+
30+
/**
31+
* @return string
32+
*/
33+
public function getClass(): ?string
34+
{
35+
return $this->class;
36+
}
37+
38+
/**
39+
* @param string $class
40+
*
41+
* @return $this
42+
*/
43+
public function setClass(?string $class)
44+
{
45+
$this->class = $class;
46+
47+
return $this;
48+
}
2649
}

_build/tests/IntegrationTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ public function parserUnitBlockProvider()
127127
'blockName' => 'tip',
128128
];
129129

130+
yield 'best-practice' => [
131+
'blockName' => 'best-practice',
132+
];
133+
130134
yield 'versionadded' => [
131135
'blockName' => 'versionadded',
132136
];
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
</head>
6+
<body>
7+
<div class="admonition-best-practice admonition-wrapper">
8+
<div class="best-practice"></div>
9+
<div class="admonition admonition-best-practice">
10+
<p class="admonition-title">Best Practice</p>
11+
<p>Use the bcrypt encoder for hashing your users' passwords.</p>
12+
</div>
13+
</div>
14+
</body>
15+
</html>

_build/tests/fixtures/expected/main/datetime.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ <h3 id="date-widget">date_widget<a class="headerlink" href="#date-widget" title=
8888
</p>
8989
</div>
9090
</div>
91+
<p>This is included documentation about the <code class="notranslate">date_widget</code> option.</p>
9192

9293
<h3 id="placeholder">placeholder<a class="headerlink" href="#placeholder" title="Permalink to this headline"></a></h3>
9394
<div class="versionadded">
@@ -131,6 +132,9 @@ <h3 id="placeholder">placeholder<a class="headerlink" href="#placeholder" title=
131132
<li><code class="notranslate">position</code></li>
132133
<li><code class="notranslate">toolbar</code></li>
133134
<li><code class="notranslate">verbose</code></li>
135+
<li>
136+
<a href="form/form_type.html#internal-reference" class="reference internal"><span>FormType Documentation</span></a>
137+
</li>
134138
</ul>
135139

136140
<h3 id="format">format<a class="headerlink" href="#format" title="Permalink to this headline"></a></h3>
@@ -156,6 +160,14 @@ <h3 id="format">format<a class="headerlink" href="#format" title="Permalink to t
156160
</div>
157161
</div>
158162

163+
<div class="admonition-best-practice admonition-wrapper">
164+
<div class="best-practice"></div>
165+
<div class="admonition admonition-best-practice">
166+
<p class="admonition-title">Best Practice</p>
167+
<p>Use the bcrypt encoder for hashing your users' passwords.</p>
168+
</div>
169+
</div>
170+
159171
<h3 id="time-widget">time_widget<a class="headerlink" href="#time-widget" title="Permalink to this headline"></a></h3>
160172

161173
<p><strong>type</strong>: <code class="notranslate">string</code> <strong>default</strong>: <code class="notranslate">choice</code></p>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
.. best-practice::
3+
4+
Use the bcrypt encoder for hashing your users' passwords.

_build/tests/fixtures/source/main/datetime.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ Date widget!
5858
the flow.
5959
:ref:`internal-reference`
6060

61+
.. include:: /_includes/date_widget.rst.inc
62+
6163
placeholder
6264
~~~~~~~~~~~
6365

@@ -92,6 +94,7 @@ Custom classes for links are also cool:
9294
* ``position``
9395
* ``toolbar``
9496
* ``verbose``
97+
* :ref:`internal-reference`
9598

9699
format
97100
~~~~~~
@@ -110,6 +113,10 @@ as a datetime string. See `Date/Time Format Syntax`_.
110113

111114
Using too many sidebars or caution directives can be distracting!
112115

116+
.. best-practice::
117+
118+
Use the bcrypt encoder for hashing your users' passwords.
119+
113120
time_widget
114121
~~~~~~~~~~~
115122

0 commit comments

Comments
 (0)