Skip to content

Commit

Permalink
bug #179 🐛 Fix yaml number rendering (homersimpsons)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
javiereguiluz committed Sep 6, 2024
2 parents 6702f0d + e7c0a06 commit 4443a68
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/Renderers/CodeNodeRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ private function configureHighlighter()
if (false === self::$isHighlighterConfigured) {
Highlighter::registerLanguage('php', __DIR__.'/../Templates/highlight.php/php.json', true);
Highlighter::registerLanguage('twig', __DIR__.'/../Templates/highlight.php/twig.json', true);
Highlighter::registerLanguage('yaml', __DIR__.'/../Templates/highlight.php/yaml.json', true);
}

self::$isHighlighterConfigured = true;
Expand Down
127 changes: 127 additions & 0 deletions src/Templates/highlight.php/yaml.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
{
"case_insensitive": true,
"aliases": [
"yml",
"YAML",
"yaml"
],
"contains": [
{
"className": "attr",
"variants": [
{
"begin": "\\w[\\w :\\\/.-]*:(?=[ \t]|$)"
},
{
"begin": "\"\\w[\\w :\\\/.-]*\":(?=[ \t]|$)"
},
{
"begin": "'\\w[\\w :\\\/.-]*':(?=[ \t]|$)"
}
]
},
{
"className": "meta",
"begin": "^---s*$",
"relevance": 10
},
{
"className": "string",
"begin": "[\\|>]([0-9]?[+-])?[ ]*\\n( *)[\\S ]+\\n(\\2[\\S ]+\\n?)*"
},
{
"begin": "<%[%=-]?",
"end": "[%-]?%>",
"subLanguage": "ruby",
"excludeBegin": true,
"excludeEnd": true,
"relevance": 0
},
{
"className": "type",
"begin": "![a-zA-Z_]\\w*"
},
{
"className": "type",
"begin": "!![a-zA-Z_]\\w*"
},
{
"className": "meta",
"begin": "&[a-zA-Z_]\\w*$"
},
{
"className": "meta",
"begin": "\\*[a-zA-Z_]\\w*$"
},
{
"className": "bullet",
"begin": "\\-(?=[ ]|$)",
"relevance": 0
},
{
"className": "comment",
"begin": "#",
"end": "$",
"contains": [
{
"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"
},
{
"className": "doctag",
"begin": "(?:TODO|FIXME|NOTE|BUG|XXX):",
"relevance": 0
}
]
},
{
"beginKeywords": "true false yes no null",
"keywords": {
"literal": "true false yes no null"
}
},
{
"className": "number",
"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"
},
{
"className": "number",
"begin": "(-?)(\\b0[xX][a-fA-F0-9_]+|(\\b\\d[\\d_]*(\\.[\\d_]*)?|\\.\\d[\\d_]*)([eE][-+]?\\d[\\d_]*)?)\\b"
},
{
"className": "string",
"relevance": 0,
"variants": [
{
"begin": "'",
"end": "'"
},
{
"begin": "\"",
"end": "\""
},
{
"begin": "\\S+"
}
],
"contains": [
{
"begin": "\\\\[\\s\\S]",
"relevance": 0
},
{
"className": "template-variable",
"variants": [
{
"begin": "{{",
"end": "}}"
},
{
"begin": "%{",
"end": "}"
}
]
}
]
}
]
}
37 changes: 33 additions & 4 deletions tests/fixtures/expected/blocks/code-blocks/yaml.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,36 @@
<div translate="no" data-loc="1" class="notranslate codeblock codeblock-length-sm codeblock-yaml">
<div translate="no" data-loc="10" class="notranslate codeblock codeblock-length-md codeblock-yaml">
<div class="codeblock-scroll">
<pre class="codeblock-lines">1</pre>
<pre class="codeblock-code"><code><span class="hljs-comment"># some code</span>
</code></pre>
<pre class="codeblock-lines">1
2
3
4
5
6
7
8
9
10</pre>
<pre class="codeblock-code">
<code>
<span class="hljs-comment"># some code</span>
<span class="hljs-attr">parameters:</span>
<span class="hljs-attr">credit_card_number:</span>
<span class="hljs-number">1234_5678_9012_3456</span>
<span class="hljs-attr">long_number:</span>
<span class="hljs-number">10_000_000_000</span>
<span class="hljs-attr">pi:</span>
<span class="hljs-number">3.14159_26535_89793</span>
<span class="hljs-attr">hex_words:</span>
<span class="hljs-number">0x_CAFE_F00D</span>
<span class="hljs-attr">canonical:</span>
<span class="hljs-number">2001-12-15T02:59:43.1Z</span>
<span class="hljs-attr">iso8601:</span>
<span class="hljs-number">2001-12-14t21:59:43.10-05:00</span>
<span class="hljs-attr">spaced:</span>
<span class="hljs-number">2001-12-14 21:59:43.10 -5</span>
<span class="hljs-attr">date:</span>
<span class="hljs-number">2002-12-14</span>
</code>
</pre>
</div>
</div>
9 changes: 9 additions & 0 deletions tests/fixtures/source/blocks/code-blocks/yaml.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@

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

0 comments on commit 4443a68

Please sign in to comment.