Skip to content

Commit 00ba70f

Browse files
committed
clean admonition directives
1 parent 1e24a90 commit 00ba70f

40 files changed

+122
-198
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
abstract class AbstractAdmonitionDirective extends SubDirective
11+
{
12+
/** @var string */
13+
private $name;
14+
15+
/** @var string */
16+
private $text;
17+
18+
public function __construct(string $name, string $text)
19+
{
20+
$this->name = $name;
21+
$this->text = $text;
22+
}
23+
24+
final public function processSub(
25+
Parser $parser,
26+
?Node $document,
27+
string $variable,
28+
string $data,
29+
array $options
30+
): ?Node {
31+
return new WrapperNode(
32+
$document,
33+
sprintf(
34+
'<div class="admonition-%s admonition-wrapper"><div class="%s"></div><div class="admonition admonition-%s"><p class="admonition-title">%s</p>',
35+
$this->name,
36+
$this->name,
37+
$this->name,
38+
$this->text
39+
),
40+
'</div></div>'
41+
);
42+
}
43+
44+
final public function getName(): string
45+
{
46+
return $this->name;
47+
}
48+
}

_build/src/Directive/BestPracticeDirective.php

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,10 @@
22

33
namespace SymfonyDocs\Directive;
44

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
5+
class BestPracticeDirective extends AbstractAdmonitionDirective
116
{
12-
public function getName(): string
7+
public function __construct()
138
{
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-
);
9+
parent::__construct('best-practice', 'Best Practice');
3210
}
3311
}

_build/src/Directive/CautionDirective.php

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,10 @@
22

33
namespace SymfonyDocs\Directive;
44

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 CautionDirective extends SubDirective
5+
class CautionDirective extends AbstractAdmonitionDirective
116
{
12-
public function getName() : string
7+
public function __construct()
138
{
14-
return 'caution';
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($document, '<div class="admonition-wrapper"><div class="caution"></div><div class="admonition admonition-caution"><p class="admonition-title">Caution</p>', '</div></div>');
9+
parent::__construct('caution', 'Caution');
2810
}
2911
}

_build/src/Directive/ClassDirective.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use Doctrine\RST\Nodes\Node;
66
use Doctrine\RST\Parser;
77
use Doctrine\RST\SubDirective;
8+
use SymfonyDocs\Nodes\ListNode;
9+
use SymfonyDocs\Nodes\ParagraphNode;
810

911
class ClassDirective extends SubDirective
1012
{
@@ -13,16 +15,17 @@ public function getName(): string
1315
return 'class';
1416
}
1517

16-
/**
17-
* @param string[] $options
18-
*/
1918
public function processSub(
2019
Parser $parser,
2120
?Node $document,
2221
string $variable,
2322
string $data,
2423
array $options
2524
): ?Node {
25+
if (!$document instanceof ListNode && !$document instanceof ParagraphNode) {
26+
throw new \RuntimeException('Class could only be applied to paragraphs or lists');
27+
}
28+
2629
$document->setClass($data);
2730

2831
return $document;

_build/src/Directive/NoteDirective.php

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,10 @@
22

33
namespace SymfonyDocs\Directive;
44

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 NoteDirective extends SubDirective
5+
class NoteDirective extends AbstractAdmonitionDirective
116
{
12-
public function getName() : string
7+
public function __construct()
138
{
14-
return 'note';
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($document, '<div class="admonition-wrapper"><div class="note"></div><div class="admonition admonition-note"><p class="admonition-title">Note</p>', '</div></div>');
9+
parent::__construct('note', 'Note');
2810
}
2911
}

_build/src/Directive/SeeAlsoDirective.php

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,10 @@
22

33
namespace SymfonyDocs\Directive;
44

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 SeeAlsoDirective extends SubDirective
5+
class SeeAlsoDirective extends AbstractAdmonitionDirective
116
{
12-
public function getName(): string
7+
public function __construct()
138
{
14-
return 'seealso';
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($document, '<div class="admonition-wrapper"><div class="seealso"></div><div class="admonition admonition-seealso">', '</div></div>');
9+
parent::__construct('seealso', 'See also');
2810
}
2911
}

_build/src/Directive/TipDirective.php

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,10 @@
22

33
namespace SymfonyDocs\Directive;
44

5-
use Doctrine\RST\Nodes\Node;
6-
use Doctrine\RST\Nodes\RawNode;
7-
use Doctrine\RST\Nodes\WrapperNode;
8-
use Doctrine\RST\Parser;
9-
use Doctrine\RST\SubDirective;
10-
11-
class TipDirective extends SubDirective
5+
class TipDirective extends AbstractAdmonitionDirective
126
{
13-
public function getName() : string
7+
public function __construct()
148
{
15-
return 'tip';
16-
}
17-
18-
/**
19-
* @param string[] $options
20-
*/
21-
public function processSub(
22-
Parser $parser,
23-
?Node $document,
24-
string $variable,
25-
string $data,
26-
array $options
27-
) : ?Node {
28-
return new WrapperNode($document, '<div class="admonition-wrapper"><div class="tip"></div><div class="admonition admonition-tip"><p class="admonition-title">Tip</p>', '</div></div>');
9+
parent::__construct('tip', 'Tip');
2910
}
3011
}

_build/src/Nodes/CodeNode.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@ class CodeNode extends Base
2323
'markdown' => 'markdown',
2424
'rst' => 'markdown',
2525
'php-standalone' => 'php',
26-
'php-symfony' => 'php',
27-
'varnish4' => 'c',
28-
'varnish3' => 'c',
26+
'php-symfony' => 'php',
27+
'varnish4' => 'c',
28+
'varnish3' => 'c',
2929
];
3030

31-
private const CODE_BLOCK_TEMPLATE = '<div class="literal-block notranslate">
31+
private const CODE_BLOCK_TEMPLATE = <<< TEMPLATE
32+
<div class="literal-block notranslate">
3233
<div class="highlight-%s">
3334
<table class="highlighttable">
3435
<tr>
@@ -45,7 +46,8 @@ class CodeNode extends Base
4546
</tr>
4647
</table>
4748
</div>
48-
</div>';
49+
</div>
50+
TEMPLATE;
4951

5052
public function render(): string
5153
{

_build/tests/IntegrationTest.php

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -108,55 +108,59 @@ public function testParseUnitBlock(string $blockName)
108108
public function parserUnitBlockProvider()
109109
{
110110
yield 'tables' => [
111-
'blockName' => 'tables',
111+
'blockName' => 'nodes/tables',
112+
];
113+
114+
yield 'literal' => [
115+
'blockName' => 'nodes/literal',
116+
];
117+
118+
yield 'list' => [
119+
'blockName' => 'nodes/list',
112120
];
113121

114122
yield 'caution' => [
115-
'blockName' => 'caution',
123+
'blockName' => 'directives/caution',
116124
];
117125

118126
yield 'note' => [
119-
'blockName' => 'note',
127+
'blockName' => 'directives/note',
128+
];
129+
130+
yield 'note-code-block-nested' => [
131+
'blockName' => 'directives/note-code-block-nested',
120132
];
121133

122134
yield 'seealso' => [
123-
'blockName' => 'seealso',
135+
'blockName' => 'directives/seealso',
124136
];
125137

126138
yield 'tip' => [
127-
'blockName' => 'tip',
139+
'blockName' => 'directives/tip',
128140
];
129141

130142
yield 'best-practice' => [
131-
'blockName' => 'best-practice',
143+
'blockName' => 'directives/best-practice',
132144
];
133145

134146
yield 'versionadded' => [
135-
'blockName' => 'versionadded',
147+
'blockName' => 'directives/versionadded',
136148
];
137149

138150
yield 'class' => [
139-
'blockName' => 'class',
151+
'blockName' => 'directives/class',
140152
];
141153

142154
yield 'configuration-block' => [
143-
'blockName' => 'configuration-block',
155+
'blockName' => 'directives/configuration-block',
144156
];
145157

146158
yield 'sidebar' => [
147-
'blockName' => 'sidebar',
148-
];
149-
150-
yield 'note-code-block-nested' => [
151-
'blockName' => 'note-code-block-nested',
159+
'blockName' => 'directives/sidebar',
152160
];
153161

154162
yield 'sidebar-code-block-nested' => [
155-
'blockName' => 'sidebar-code-block-nested',
156-
];
157-
158-
yield 'literal' => [
159-
'blockName' => 'literal',
163+
'blockName' => 'directives/sidebar-code-block-nested',
160164
];
161165

162166
yield 'class-reference' => [
@@ -230,10 +234,6 @@ public function parserUnitBlockProvider()
230234
yield 'code-block-terminal' => [
231235
'blockName' => 'code-blocks/terminal',
232236
];
233-
234-
yield 'list' => [
235-
'blockName' => 'list',
236-
];
237237
}
238238

239239
public function testRefReferenceError()

_build/tests/fixtures/expected/blocks/code-block.html

Lines changed: 0 additions & 37 deletions
This file was deleted.

0 commit comments

Comments
 (0)