Skip to content

Commit 84b235e

Browse files
committed
test every code blocks
1 parent 40a4acf commit 84b235e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+529
-34
lines changed

_build/notes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ Notes
1010
=====
1111

1212
- add some format check on references
13-
- toc tree bugs whith :glob:
13+
- toc tree bugs whith :glob: (issue opened)

_build/src/Nodes/CodeNode.php

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,21 @@
77

88
class CodeNode extends Base
99
{
10+
private const LANGUAGES_MAPPING = [
11+
'php' => 'php',
12+
'html' => 'html',
13+
'xml' => 'xml',
14+
'yaml' => 'yaml',
15+
'twig' => 'twig',
16+
'ini' => 'ini',
17+
'bash' => 'bash',
18+
'html+twig' => 'twig',
19+
'html+php' => 'html',
20+
'php-annotations' => 'php',
21+
'text' => 'text',
22+
'terminal' => 'bash',
23+
];
24+
1025
private const CODE_BLOCK_TEMPLATE = '<div class="literal-block notranslate">
1126
<div class="highlight-%s">
1227
<table class="highlighttable">
@@ -18,7 +33,7 @@ class CodeNode extends Base
1833
</td>
1934
<td class="code">
2035
<div class="highlight">
21-
<pre class="hljs">%s</pre>
36+
<pre class="hljs %s">%s</pre>
2237
</div>
2338
</td>
2439
</tr>
@@ -31,6 +46,7 @@ public function render(): string
3146
$nodeValue = $this->getValue();
3247
assert(is_string($nodeValue));
3348
$lines = $this->getLines($nodeValue);
49+
$code = implode("\n", $lines);
3450

3551
$lineNumbers = "";
3652
for ($i = 1; $i <= \count($lines); $i++) {
@@ -43,14 +59,21 @@ public function render(): string
4359

4460
$language = $this->getLanguage() ?? 'php';
4561

46-
$highLighter = new Highlighter();
47-
$highlightedCode = $highLighter->highlight($language, implode("\n", $lines));
62+
if (!isset(self::LANGUAGES_MAPPING[$language])) {
63+
throw new \RuntimeException(sprintf('Language "%s" is unknown', $language));
64+
}
65+
66+
if ('text' !== $language) {
67+
$highLighter = new Highlighter();
68+
$code = $highLighter->highlight(self::LANGUAGES_MAPPING[$language], $code)->value;
69+
}
4870

4971
return sprintf(
5072
self::CODE_BLOCK_TEMPLATE,
5173
$language,
5274
rtrim($lineNumbers),
53-
$highlightedCode->value
75+
self::LANGUAGES_MAPPING[$language],
76+
$code
5477
);
5578
}
5679

_build/tests/IntegrationTest.php

Lines changed: 69 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -71,87 +71,132 @@ public function testParseUnitBlock(string $blockName)
7171

7272
$expectedFile = sprintf('%s/fixtures/expected/blocks/%s.html', __DIR__, $blockName);
7373
$this->assertSame(
74-
$indenter->indent(file_get_contents($expectedFile)),
75-
$indenter->indent($document)
74+
// removes odd trailing space the indenter is adding
75+
str_replace(" \n", "\n", $indenter->indent(file_get_contents($expectedFile))),
76+
str_replace(" \n", "\n", $indenter->indent($document))
7677
);
7778
}
7879

7980
public function parserUnitBlockProvider()
8081
{
8182
yield 'tables' => [
82-
'documentName' => 'tables',
83+
'blockName' => 'tables',
8384
];
8485

8586
yield 'caution' => [
86-
'documentName' => 'caution',
87+
'blockName' => 'caution',
8788
];
8889

8990
yield 'note' => [
90-
'documentName' => 'note',
91+
'blockName' => 'note',
9192
];
9293

9394
yield 'seealso' => [
94-
'documentName' => 'seealso',
95+
'blockName' => 'seealso',
9596
];
9697

9798
yield 'tip' => [
98-
'documentName' => 'tip',
99+
'blockName' => 'tip',
99100
];
100101

101102
yield 'versionadded' => [
102-
'documentName' => 'versionadded',
103+
'blockName' => 'versionadded',
103104
];
104105

105106
yield 'class' => [
106-
'documentName' => 'class',
107+
'blockName' => 'class',
107108
];
108109

109110
yield 'configuration-block' => [
110-
'documentName' => 'configuration-block',
111-
];
112-
113-
yield 'code-block' => [
114-
'documentName' => 'code-block',
111+
'blockName' => 'configuration-block',
115112
];
116113

117114
yield 'sidebar' => [
118-
'documentName' => 'sidebar',
115+
'blockName' => 'sidebar',
119116
];
120117

121118
yield 'note-code-block-nested' => [
122-
'documentName' => 'note-code-block-nested',
119+
'blockName' => 'note-code-block-nested',
123120
];
124121

125122
yield 'sidebar-code-block-nested' => [
126-
'documentName' => 'sidebar-code-block-nested',
123+
'blockName' => 'sidebar-code-block-nested',
127124
];
128125

129126
yield 'literal' => [
130-
'documentName' => 'literal',
127+
'blockName' => 'literal',
131128
];
132129

133130
yield 'class-reference' => [
134-
'documentName' => 'class-reference',
131+
'blockName' => 'references/class',
135132
];
136133

137134
yield 'namespace-reference' => [
138-
'documentName' => 'namespace-reference',
135+
'blockName' => 'references/namespace',
139136
];
140137

141138
yield 'method-reference' => [
142-
'documentName' => 'method-reference',
139+
'blockName' => 'references/method',
143140
];
144141

145142
yield 'php-class-reference' => [
146-
'documentName' => 'php-class-reference',
143+
'blockName' => 'references/php-class',
147144
];
148145

149146
yield 'php-function-reference' => [
150-
'documentName' => 'php-function-reference',
147+
'blockName' => 'references/php-function',
151148
];
152149

153150
yield 'php-method-reference' => [
154-
'documentName' => 'php-method-reference',
151+
'blockName' => 'references/php-method',
152+
];
153+
154+
yield 'code-block-php' => [
155+
'blockName' => 'code-blocks/php',
156+
];
157+
158+
yield 'code-block-html' => [
159+
'blockName' => 'code-blocks/html',
160+
];
161+
162+
yield 'code-block-twig' => [
163+
'blockName' => 'code-blocks/twig',
164+
];
165+
166+
yield 'code-block-html-twig' => [
167+
'blockName' => 'code-blocks/html-twig',
168+
];
169+
170+
yield 'code-block-xml' => [
171+
'blockName' => 'code-blocks/xml',
172+
];
173+
174+
yield 'code-block-yaml' => [
175+
'blockName' => 'code-blocks/yaml',
176+
];
177+
178+
yield 'code-block-ini' => [
179+
'blockName' => 'code-blocks/ini',
180+
];
181+
182+
yield 'code-block-bash' => [
183+
'blockName' => 'code-blocks/bash',
184+
];
185+
186+
yield 'code-block-html-php' => [
187+
'blockName' => 'code-blocks/html-php',
188+
];
189+
190+
yield 'code-block-php-annotations' => [
191+
'blockName' => 'code-blocks/php-annotations',
192+
];
193+
194+
yield 'code-block-text' => [
195+
'blockName' => 'code-blocks/text',
196+
];
197+
198+
yield 'code-block-terminal' => [
199+
'blockName' => 'code-blocks/terminal',
155200
];
156201
}
157202

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
</head>
6+
<body>
7+
<div class="literal-block notranslate">
8+
<div class="highlight-bash">
9+
<table class="highlighttable">
10+
<tr>
11+
<td class="linenos">
12+
<div class="linenodiv">
13+
<pre> 1</pre>
14+
</div>
15+
</td>
16+
<td class="code">
17+
<div class="highlight">
18+
<pre class="hljs bash">git <span class="hljs-built_in">clone</span> [email protected]:symfony/symfony.git</pre>
19+
</div>
20+
</td>
21+
</tr>
22+
</table>
23+
</div>
24+
</div>
25+
</body>
26+
</html>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
</head>
6+
<body>
7+
<div class="literal-block notranslate">
8+
<div class="highlight-html+php">
9+
<table class="highlighttable">
10+
<tr>
11+
<td class="linenos">
12+
<div class="linenodiv">
13+
<pre> 1 2 3 4 5 6 7 8 9
14+
10
15+
11
16+
12</pre>
17+
</div>
18+
</td>
19+
<td class="code">
20+
<div class="highlight">
21+
<pre class="hljs html">
22+
<span class="hljs-comment">&lt;!-- views/layout.php --&gt;</span>
23+
<span class="hljs-meta">&lt;!doctype html&gt;</span>
24+
<span class="hljs-tag">&lt;<span class="hljs-name">html</span>&gt;</span>
25+
<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
26+
<span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>
27+
<span class="php"><span class="hljs-meta">&lt;?php</span> <span class="hljs-keyword">echo</span> <span class="hljs-string">'title'</span>; <span class="hljs-meta">?&gt;</span></span>
28+
<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
29+
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
30+
<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
31+
<span class="php"><span class="hljs-meta">&lt;?php</span> <span class="hljs-keyword">echo</span> <span class="hljs-string">'body'</span>; <span class="hljs-meta">?&gt;</span></span>
32+
<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
33+
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
34+
</pre>
35+
</div>
36+
</td>
37+
</tr>
38+
</table>
39+
</div>
40+
</div>
41+
</body>
42+
</html>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
</head>
6+
<body>
7+
<div class="literal-block notranslate">
8+
<div class="highlight-html+twig">
9+
<table class="highlighttable">
10+
<tr>
11+
<td class="linenos">
12+
<div class="linenodiv">
13+
<pre> 1 2</pre>
14+
</div>
15+
</td>
16+
<td class="code">
17+
<div class="highlight">
18+
<pre class="hljs twig">
19+
<span class="xml"></span><span class="hljs-comment">{# some code #}</span>
20+
<span class="xml">
21+
<span class="hljs-comment">&lt;!-- some code --&gt;</span></span>
22+
</pre>
23+
</div>
24+
</td>
25+
</tr>
26+
</table>
27+
</div>
28+
</div>
29+
</body>
30+
</html>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
</head>
6+
<body>
7+
<div class="literal-block notranslate">
8+
<div class="highlight-html">
9+
<table class="highlighttable">
10+
<tr>
11+
<td class="linenos">
12+
<div class="linenodiv">
13+
<pre> 1</pre>
14+
</div>
15+
</td>
16+
<td class="code">
17+
<div class="highlight">
18+
<pre class="hljs html"><span class="hljs-comment">&lt;!-- some code --&gt;</span></pre>
19+
</div>
20+
</td>
21+
</tr>
22+
</table>
23+
</div>
24+
</div>
25+
</body>
26+
</html>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
</head>
6+
<body>
7+
<div class="literal-block notranslate">
8+
<div class="highlight-ini">
9+
<table class="highlighttable">
10+
<tr>
11+
<td class="linenos">
12+
<div class="linenodiv">
13+
<pre> 1</pre>
14+
</div>
15+
</td>
16+
<td class="code">
17+
<div class="highlight">
18+
<pre class="hljs ini"><span class="hljs-attr">fetch</span> = +refs/notes/*:refs/notes/*</pre>
19+
</div>
20+
</td>
21+
</tr>
22+
</table>
23+
</div>
24+
</div>
25+
</body>
26+
</html>

0 commit comments

Comments
 (0)