Skip to content

Commit d75a661

Browse files
committed
fixing tests for updated highlighting rules
Also fixing a bug where the `::` was not matching strongly enough, which was causing `::class` to be seen as a `::` operator (correct) but then the `class` was seen as a class declaration.
1 parent 3ab693b commit d75a661

File tree

7 files changed

+54
-10
lines changed

7 files changed

+54
-10
lines changed

src/Templates/highlight.php/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Custom Highlight Templates
2+
3+
The highlight.php library takes its highlighting metadata/rules
4+
from highlight.js. That library builds the highlighting metadata/rules
5+
in Node and then outputs them as JSON:
6+
7+
* Source Language files: https://github.com/highlightjs/highlight.js/tree/master/src/languages
8+
* Final Language files: https://github.com/scrivo/highlight.php/tree/master/Highlight/languages
9+
10+
In a few cases, we've extended the language rules, which (in theory)
11+
should make it back upstream to highlight.js. These files began
12+
as copies of the .json files (which were then prettified) then extended.
13+
14+
A few things we've learned about how the language files work:
15+
16+
* `begin` is the regex that marks the beginning of something
17+
* `end` is optional. Without it, `begin` will be used, and as
18+
soon as it finds a non-matching character, it will stop.
19+
If you have a situation where using begin is causing
20+
over-matching, then you can use end to tell it exactly where
21+
to stop.
22+
* `excludeEnd` can be used to match an entire string with `begin`
23+
and `end`, but then only apply the class name to the part
24+
matched by `start`. This was useful with `::` where we wanted
25+
to match `::` THEN some valid string (to avoid over-matching
26+
`::` in other situations). But, we only wanted the class name
27+
applied to the `start` part (the `::` part).
28+
* `contains` the way for building embedded rules. `function` is
29+
a nice example, which outlines the `start` and `end` and then
30+
also outlines some items that will be embedded inside of it.

src/Templates/highlight.php/php.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,9 @@
117117
},
118118
{
119119
"className": "operator",
120-
"begin": "(::|->)"
120+
"begin": "(::|->)",
121+
"end": "[a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*",
122+
"excludeEnd": true
121123
},
122124
{
123125
"className": "function",

tests/IntegrationTest.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function testIntegration(string $folder)
5050
->files()
5151
->depth('>=0');
5252

53-
$indenter = new Indenter();
53+
$indenter = $this->createIndenter();
5454
foreach ($finder as $expectedFile) {
5555
$relativePath = $expectedFile->getRelativePathname();
5656
$actualFilename = __DIR__.'/_output/'.$relativePath;
@@ -126,7 +126,7 @@ public function testParseUnitBlock(string $blockName)
126126

127127
$document = $parser->parseFile($sourceFile)->renderDocument();
128128

129-
$indenter = new Indenter();
129+
$indenter = $this->createIndenter();
130130

131131
$expectedFile = sprintf('%s/fixtures/expected/blocks/%s.html', __DIR__, $blockName);
132132
$this->assertSame(
@@ -291,4 +291,13 @@ private function createBuildContext(string $sourceDir): BuildContext
291291

292292
return $buildContext;
293293
}
294+
295+
private function createIndenter(): Indenter
296+
{
297+
$indenter = new Indenter();
298+
// indent spans - easier to debug failures
299+
$indenter->setElementType('span', Indenter::ELEMENT_TYPE_BLOCK);
300+
301+
return $indenter;
302+
}
294303
}

tests/fixtures/expected/blocks/code-blocks/php-annotations.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
</span>
2828
{
2929
<span class="hljs-comment">/** * <span class="hljs-doctag">@Assert</span>\Iban( * message="This is not a valid International Bank Account Number (IBAN)." * ) */</span>
30-
<span class="hljs-keyword">protected</span> $bankAccountNumber;
30+
<span class="hljs-keyword">protected</span> <span class="hljs-variable">$bankAccountNumber</span>;
3131
}
3232
</pre>
3333
</div>

tests/fixtures/expected/blocks/code-blocks/php.html

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@
2424
<pre class="hljs php">
2525
<span class="hljs-comment">// config/routes.php</span>
2626
<span class="hljs-keyword">namespace</span> <span class="hljs-title">Symfony</span>\<span class="hljs-title">Component</span>\<span class="hljs-title">Routing</span>\<span class="hljs-title">Loader</span>\<span class="hljs-title">Configurator</span>; <span class="hljs-keyword">return</span>
27-
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-params">(RoutingConfigurator $routes)</span> </span>
28-
{ $routes-&gt;add(<span class="hljs-string">'about_us'</span>, [<span class="hljs-string">'nl'</span> =&gt; <span class="hljs-string">'/over-ons'</span>, <span class="hljs-string">'en'</span> =&gt; <span class="hljs-string">'/about-us'</span>]) -&gt;controller(<span class="hljs-string">'App\Controller\CompanyController::about'</span>);
27+
<span class="hljs-function">
28+
<span class="hljs-keyword">function</span>
29+
<span class="hljs-params">(RoutingConfigurator <span class="hljs-variable">$routes</span>)</span>
30+
</span>
31+
{ <span class="hljs-variable">$routes</span><span class="hljs-operator">-&gt;</span>add(<span class="hljs-string">'about_us'</span>, [<span class="hljs-string">'nl'</span> =&gt; <span class="hljs-string">'/over-ons'</span>, <span class="hljs-string">'en'</span> =&gt; <span class="hljs-string">'/about-us'</span>]) <span class="hljs-operator">-&gt;</span>controller(<span class="hljs-string">'App\Controller\CompanyController::about'</span>);
2932
};</pre>
3033
</div>
3134
</td>

tests/fixtures/expected/blocks/nodes/literal.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
<pre class="hljs php">
2626
<span class="hljs-comment">// config/routes.php</span>
2727
<span class="hljs-keyword">namespace</span> <span class="hljs-title">Symfony</span>\<span class="hljs-title">Component</span>\<span class="hljs-title">Routing</span>\<span class="hljs-title">Loader</span>\<span class="hljs-title">Configurator</span>; <span class="hljs-keyword">return</span>
28-
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-params">(RoutingConfigurator $routes)</span> </span>
29-
{ $routes-&gt;add(<span class="hljs-string">'about_us'</span>, [<span class="hljs-string">'nl'</span> =&gt; <span class="hljs-string">'/over-ons'</span>, <span class="hljs-string">'en'</span> =&gt; <span class="hljs-string">'/about-us'</span>]) -&gt;controller(<span class="hljs-string">'App\Controller\CompanyController::about'</span>);
28+
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-params">(RoutingConfigurator <span class="hljs-variable">$routes</span>)</span> </span>
29+
{ <span class="hljs-variable">$routes</span><span class="hljs-operator">-&gt;</span>add(<span class="hljs-string">'about_us'</span>, [<span class="hljs-string">'nl'</span> =&gt; <span class="hljs-string">'/over-ons'</span>, <span class="hljs-string">'en'</span> =&gt; <span class="hljs-string">'/about-us'</span>]) <span class="hljs-operator">-&gt;</span>controller(<span class="hljs-string">'App\Controller\CompanyController::about'</span>);
3030
};</pre>
3131
</div>
3232
</td>

tests/fixtures/expected/main/datetime.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ <h3>placeholder<a class="headerlink" href="#placeholder" title="Permalink to thi
113113
</td>
114114
<td class="code">
115115
<div class="highlight">
116-
<pre class="hljs php"><span class="hljs-keyword">use</span> <span class="hljs-title">Symfony</span>\<span class="hljs-title">Component</span>\<span class="hljs-title">Form</span>\<span class="hljs-title">Extension</span>\<span class="hljs-title">Core</span>\<span class="hljs-title">Type</span>\<span class="hljs-title">DateTimeType</span>; $builder-&gt;add(<span class="hljs-string">'startDateTime'</span>, DateTimeType::class, <span class="hljs-keyword">array</span>( <span class="hljs-string">'placeholder'</span> =&gt; <span class="hljs-string">'Select a value'</span>,
116+
<pre class="hljs php"><span class="hljs-keyword">use</span> <span class="hljs-title">Symfony</span>\<span class="hljs-title">Component</span>\<span class="hljs-title">Form</span>\<span class="hljs-title">Extension</span>\<span class="hljs-title">Core</span>\<span class="hljs-title">Type</span>\<span class="hljs-title">DateTimeType</span>; <span class="hljs-variable">$builder</span><span class="hljs-operator">-&gt;</span>add(<span class="hljs-string">'startDateTime'</span>, DateTimeType<span class="hljs-operator">::</span>class, <span class="hljs-keyword">array</span>( <span class="hljs-string">'placeholder'</span> =&gt; <span class="hljs-string">'Select a value'</span>,
117117
));</pre>
118118
</div>
119119
</td>
@@ -307,7 +307,7 @@ <h3>error_bubbling<a class="headerlink" href="#error-bubbling" title="Permalink
307307
<td class="code">
308308
<div class="highlight">
309309
<pre class="hljs php"><span class="hljs-comment">// app/config/config.php</span>
310-
$container-&gt;loadFromExtension(<span class="hljs-string">'framework'</span>, <span class="hljs-keyword">array</span>( <span class="hljs-string">'secret'</span> =&gt; <span class="hljs-string">'%secret%'</span>, <span class="hljs-string">'router'</span> =&gt; <span class="hljs-keyword">array</span>( <span class="hljs-string">'resource'</span> =&gt; <span class="hljs-string">'%kernel.root_dir%/config/routing.php'</span>, ), <span class="hljs-comment">// ...</span>
310+
<span class="hljs-variable">$container</span><span class="hljs-operator">-&gt;</span>loadFromExtension(<span class="hljs-string">'framework'</span>, <span class="hljs-keyword">array</span>( <span class="hljs-string">'secret'</span> =&gt; <span class="hljs-string">'%secret%'</span>, <span class="hljs-string">'router'</span> =&gt; <span class="hljs-keyword">array</span>( <span class="hljs-string">'resource'</span> =&gt; <span class="hljs-string">'%kernel.root_dir%/config/routing.php'</span>, ), <span class="hljs-comment">// ...</span>
311311
)); <span class="hljs-comment">// ...</span></pre>
312312
</div>
313313
</td>

0 commit comments

Comments
 (0)