Skip to content

Commit

Permalink
(yaml) improve tag support; add verbatim tags (#2487)
Browse files Browse the repository at this point in the history
* YAML parse non-word characters as part of tags
* adds support for verbatim tags

Co-authored-by: Josh Goebel <me@joshgoebel.com>
  • Loading branch information
pplantinga and joshgoebel authored Apr 27, 2020
1 parent f86d6a9 commit 7502e42
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
9 changes: 9 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## Version 10.1.0 (in progress)

Language improvements:

- fix(yaml) Fix tags to include non-word characters (#2486) [Peter Plantinga][]

[Peter Plantinga]: https://github.com/pplantinga


## Version 10.0.1

Parser Engine Changes:
Expand Down
20 changes: 16 additions & 4 deletions src/languages/yaml.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ Category: common, config
export default function(hljs) {
var LITERALS = 'true false yes no null';

// YAML spec allows non-reserved URI characters in tags.
var URI_CHARACTERS = '[\\w#;/?:@&=+$,.~*\\\'()[\\]]+'

// Define keys as starting with a word character
// ...containing word chars, spaces, colons, forward-slashes, hyphens and periods
// ...and ending with a colon followed immediately by a space, tab or newline.
Expand Down Expand Up @@ -79,13 +82,22 @@ export default function(hljs) {
excludeEnd: true,
relevance: 0
},
{ // local tags
{ // named tags
className: 'type',
begin: '!\\w+!' + URI_CHARACTERS,
},
// https://yaml.org/spec/1.2/spec.html#id2784064
{ // verbatim tags
className: 'type',
begin: '!<' + URI_CHARACTERS + ">",
},
{ // primary tags
className: 'type',
begin: '!' + hljs.UNDERSCORE_IDENT_RE,
begin: '!' + URI_CHARACTERS,
},
{ // data type
{ // secondary tags
className: 'type',
begin: '!!' + hljs.UNDERSCORE_IDENT_RE,
begin: '!!' + URI_CHARACTERS,
},
{ // fragment id &ref
className: 'meta',
Expand Down
8 changes: 8 additions & 0 deletions test/markup/yaml/tag.expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,11 @@
<span class="hljs-attr">key:</span> <span class="hljs-type">!localtagname</span> <span class="hljs-string">test</span>
<span class="hljs-attr">key:</span> <span class="hljs-string">"!notatag"</span>
<span class="hljs-attr">key:</span> <span class="hljs-string">'!!notatageither'</span>
<span class="hljs-attr">key:</span> <span class="hljs-type">!!python/dict</span> <span class="hljs-string">test</span>
<span class="hljs-attr">key:</span> <span class="hljs-type">!!python/name:module.name</span> <span class="hljs-string">test</span>
<span class="hljs-attr">key:</span> <span class="hljs-type">!foo2.bar</span> <span class="hljs-string">test</span>
<span class="hljs-attr">key:</span> <span class="hljs-type">!(foo.bar?):tag</span> <span class="hljs-string">test</span>
<span class="hljs-attr">key:</span> <span class="hljs-type">!named!tag</span> <span class="hljs-string">test</span>

<span class="hljs-string">---</span> <span class="hljs-type">!&lt;tag:clarkevans.com,2002:invoice&gt;</span>
<span class="hljs-attr">invoice:</span> <span class="hljs-number">34843</span>
8 changes: 8 additions & 0 deletions test/markup/yaml/tag.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,11 @@ key: !!builtintagname test
key: !localtagname test
key: "!notatag"
key: '!!notatageither'
key: !!python/dict test
key: !!python/name:module.name test
key: !foo2.bar test
key: !(foo.bar?):tag test
key: !named!tag test

--- !<tag:clarkevans.com,2002:invoice>
invoice: 34843

0 comments on commit 7502e42

Please sign in to comment.