Skip to content

Commit 4443a68

Browse files
committed
bug #179 🐛 Fix yaml number rendering (homersimpsons)
This PR was squashed before being merged into the main branch. Discussion ---------- :bug: Fix yaml number rendering Fixes #177 I copied the yaml.json file from https://github.com/scrivo/highlight.php/blob/d1c6b0956a2b0d3efc137e4d8c5bf5c5e220bac1/src/Highlight/languages/yaml.json#L84 and updated the "number" regex to add `_` where I think the symfony parser allow them: ```diff - "begin": "(-?)(\\b0[xX][a-fA-F0-9]+|(\\b\\d+(\\.\\d*)?|\\.\\d+)([eE][-+]?\\d+)?)\\b" + "begin": "(-?)(\\b0[xX][a-fA-F0-9_]+|(\\b\\d[\\d_]*(\\.[\\d_]*)?|\\.\\d[\\d_]*)([eE][-+]?\\d[\\d_]*)?)\\b" ``` There may be additional places where the symfony parser allow the `_` for numbers. I also updated the tests to add the example from the documentation. Commits ------- e7c0a06 🐛 Fix yaml number rendering
2 parents 6702f0d + e7c0a06 commit 4443a68

File tree

4 files changed

+170
-4
lines changed

4 files changed

+170
-4
lines changed

src/Renderers/CodeNodeRenderer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ private function configureHighlighter()
121121
if (false === self::$isHighlighterConfigured) {
122122
Highlighter::registerLanguage('php', __DIR__.'/../Templates/highlight.php/php.json', true);
123123
Highlighter::registerLanguage('twig', __DIR__.'/../Templates/highlight.php/twig.json', true);
124+
Highlighter::registerLanguage('yaml', __DIR__.'/../Templates/highlight.php/yaml.json', true);
124125
}
125126

126127
self::$isHighlighterConfigured = true;

src/Templates/highlight.php/yaml.json

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
{
2+
"case_insensitive": true,
3+
"aliases": [
4+
"yml",
5+
"YAML",
6+
"yaml"
7+
],
8+
"contains": [
9+
{
10+
"className": "attr",
11+
"variants": [
12+
{
13+
"begin": "\\w[\\w :\\\/.-]*:(?=[ \t]|$)"
14+
},
15+
{
16+
"begin": "\"\\w[\\w :\\\/.-]*\":(?=[ \t]|$)"
17+
},
18+
{
19+
"begin": "'\\w[\\w :\\\/.-]*':(?=[ \t]|$)"
20+
}
21+
]
22+
},
23+
{
24+
"className": "meta",
25+
"begin": "^---s*$",
26+
"relevance": 10
27+
},
28+
{
29+
"className": "string",
30+
"begin": "[\\|>]([0-9]?[+-])?[ ]*\\n( *)[\\S ]+\\n(\\2[\\S ]+\\n?)*"
31+
},
32+
{
33+
"begin": "<%[%=-]?",
34+
"end": "[%-]?%>",
35+
"subLanguage": "ruby",
36+
"excludeBegin": true,
37+
"excludeEnd": true,
38+
"relevance": 0
39+
},
40+
{
41+
"className": "type",
42+
"begin": "![a-zA-Z_]\\w*"
43+
},
44+
{
45+
"className": "type",
46+
"begin": "!![a-zA-Z_]\\w*"
47+
},
48+
{
49+
"className": "meta",
50+
"begin": "&[a-zA-Z_]\\w*$"
51+
},
52+
{
53+
"className": "meta",
54+
"begin": "\\*[a-zA-Z_]\\w*$"
55+
},
56+
{
57+
"className": "bullet",
58+
"begin": "\\-(?=[ ]|$)",
59+
"relevance": 0
60+
},
61+
{
62+
"className": "comment",
63+
"begin": "#",
64+
"end": "$",
65+
"contains": [
66+
{
67+
"begin": "\\b(a|an|the|are|I'm|isn't|don't|doesn't|won't|but|just|should|pretty|simply|enough|gonna|going|wtf|so|such|will|you|your|they|like|more)\\b"
68+
},
69+
{
70+
"className": "doctag",
71+
"begin": "(?:TODO|FIXME|NOTE|BUG|XXX):",
72+
"relevance": 0
73+
}
74+
]
75+
},
76+
{
77+
"beginKeywords": "true false yes no null",
78+
"keywords": {
79+
"literal": "true false yes no null"
80+
}
81+
},
82+
{
83+
"className": "number",
84+
"begin": "\\b([0-9][0-9][0-9][0-9])-([0-9][0-9]?)-([0-9][0-9]?)(([Tt]|[ \\t]+)([0-9][0-9]?):([0-9][0-9]):([0-9][0-9])(\\.([0-9]*))?([ \\t]*(Z|([-+])([0-9][0-9]?)(:([0-9][0-9]))?))?)?\\b"
85+
},
86+
{
87+
"className": "number",
88+
"begin": "(-?)(\\b0[xX][a-fA-F0-9_]+|(\\b\\d[\\d_]*(\\.[\\d_]*)?|\\.\\d[\\d_]*)([eE][-+]?\\d[\\d_]*)?)\\b"
89+
},
90+
{
91+
"className": "string",
92+
"relevance": 0,
93+
"variants": [
94+
{
95+
"begin": "'",
96+
"end": "'"
97+
},
98+
{
99+
"begin": "\"",
100+
"end": "\""
101+
},
102+
{
103+
"begin": "\\S+"
104+
}
105+
],
106+
"contains": [
107+
{
108+
"begin": "\\\\[\\s\\S]",
109+
"relevance": 0
110+
},
111+
{
112+
"className": "template-variable",
113+
"variants": [
114+
{
115+
"begin": "{{",
116+
"end": "}}"
117+
},
118+
{
119+
"begin": "%{",
120+
"end": "}"
121+
}
122+
]
123+
}
124+
]
125+
}
126+
]
127+
}
Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,36 @@
1-
<div translate="no" data-loc="1" class="notranslate codeblock codeblock-length-sm codeblock-yaml">
1+
<div translate="no" data-loc="10" class="notranslate codeblock codeblock-length-md codeblock-yaml">
22
<div class="codeblock-scroll">
3-
<pre class="codeblock-lines">1</pre>
4-
<pre class="codeblock-code"><code><span class="hljs-comment"># some code</span>
5-
</code></pre>
3+
<pre class="codeblock-lines">1
4+
2
5+
3
6+
4
7+
5
8+
6
9+
7
10+
8
11+
9
12+
10</pre>
13+
<pre class="codeblock-code">
14+
<code>
15+
<span class="hljs-comment"># some code</span>
16+
<span class="hljs-attr">parameters:</span>
17+
<span class="hljs-attr">credit_card_number:</span>
18+
<span class="hljs-number">1234_5678_9012_3456</span>
19+
<span class="hljs-attr">long_number:</span>
20+
<span class="hljs-number">10_000_000_000</span>
21+
<span class="hljs-attr">pi:</span>
22+
<span class="hljs-number">3.14159_26535_89793</span>
23+
<span class="hljs-attr">hex_words:</span>
24+
<span class="hljs-number">0x_CAFE_F00D</span>
25+
<span class="hljs-attr">canonical:</span>
26+
<span class="hljs-number">2001-12-15T02:59:43.1Z</span>
27+
<span class="hljs-attr">iso8601:</span>
28+
<span class="hljs-number">2001-12-14t21:59:43.10-05:00</span>
29+
<span class="hljs-attr">spaced:</span>
30+
<span class="hljs-number">2001-12-14 21:59:43.10 -5</span>
31+
<span class="hljs-attr">date:</span>
32+
<span class="hljs-number">2002-12-14</span>
33+
</code>
34+
</pre>
635
</div>
736
</div>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
11

22
.. code-block:: yaml
33
# some code
4+
parameters:
5+
credit_card_number: 1234_5678_9012_3456
6+
long_number: 10_000_000_000
7+
pi: 3.14159_26535_89793
8+
hex_words: 0x_CAFE_F00D
9+
canonical: 2001-12-15T02:59:43.1Z
10+
iso8601: 2001-12-14t21:59:43.10-05:00
11+
spaced: 2001-12-14 21:59:43.10 -5
12+
date: 2002-12-14

0 commit comments

Comments
 (0)