[d24f17c] | 1 | /*
|
---|
| 2 | Language: YAML
|
---|
| 3 | Description: Yet Another Markdown Language
|
---|
| 4 | Author: Stefan Wienert <stwienert@gmail.com>
|
---|
| 5 | Contributors: Carl Baxter <carl@cbax.tech>
|
---|
| 6 | Requires: ruby.js
|
---|
| 7 | Website: https://yaml.org
|
---|
| 8 | Category: common, config
|
---|
| 9 | */
|
---|
| 10 | function yaml(hljs) {
|
---|
| 11 | var LITERALS = 'true false yes no null';
|
---|
| 12 |
|
---|
| 13 | // YAML spec allows non-reserved URI characters in tags.
|
---|
| 14 | var URI_CHARACTERS = '[\\w#;/?:@&=+$,.~*\'()[\\]]+';
|
---|
| 15 |
|
---|
| 16 | // Define keys as starting with a word character
|
---|
| 17 | // ...containing word chars, spaces, colons, forward-slashes, hyphens and periods
|
---|
| 18 | // ...and ending with a colon followed immediately by a space, tab or newline.
|
---|
| 19 | // The YAML spec allows for much more than this, but this covers most use-cases.
|
---|
| 20 | var KEY = {
|
---|
| 21 | className: 'attr',
|
---|
| 22 | variants: [
|
---|
| 23 | { begin: '\\w[\\w :\\/.-]*:(?=[ \t]|$)' },
|
---|
| 24 | { begin: '"\\w[\\w :\\/.-]*":(?=[ \t]|$)' }, // double quoted keys
|
---|
| 25 | { begin: '\'\\w[\\w :\\/.-]*\':(?=[ \t]|$)' } // single quoted keys
|
---|
| 26 | ]
|
---|
| 27 | };
|
---|
| 28 |
|
---|
| 29 | var TEMPLATE_VARIABLES = {
|
---|
| 30 | className: 'template-variable',
|
---|
| 31 | variants: [
|
---|
| 32 | { begin: /\{\{/, end: /\}\}/ }, // jinja templates Ansible
|
---|
| 33 | { begin: /%\{/, end: /\}/ } // Ruby i18n
|
---|
| 34 | ]
|
---|
| 35 | };
|
---|
| 36 | var STRING = {
|
---|
| 37 | className: 'string',
|
---|
| 38 | relevance: 0,
|
---|
| 39 | variants: [
|
---|
| 40 | { begin: /'/, end: /'/ },
|
---|
| 41 | { begin: /"/, end: /"/ },
|
---|
| 42 | { begin: /\S+/ }
|
---|
| 43 | ],
|
---|
| 44 | contains: [
|
---|
| 45 | hljs.BACKSLASH_ESCAPE,
|
---|
| 46 | TEMPLATE_VARIABLES
|
---|
| 47 | ]
|
---|
| 48 | };
|
---|
| 49 |
|
---|
| 50 | // Strings inside of value containers (objects) can't contain braces,
|
---|
| 51 | // brackets, or commas
|
---|
| 52 | var CONTAINER_STRING = hljs.inherit(STRING, {
|
---|
| 53 | variants: [
|
---|
| 54 | { begin: /'/, end: /'/ },
|
---|
| 55 | { begin: /"/, end: /"/ },
|
---|
| 56 | { begin: /[^\s,{}[\]]+/ }
|
---|
| 57 | ]
|
---|
| 58 | });
|
---|
| 59 |
|
---|
| 60 | var DATE_RE = '[0-9]{4}(-[0-9][0-9]){0,2}';
|
---|
| 61 | var TIME_RE = '([Tt \\t][0-9][0-9]?(:[0-9][0-9]){2})?';
|
---|
| 62 | var FRACTION_RE = '(\\.[0-9]*)?';
|
---|
| 63 | var ZONE_RE = '([ \\t])*(Z|[-+][0-9][0-9]?(:[0-9][0-9])?)?';
|
---|
| 64 | var TIMESTAMP = {
|
---|
| 65 | className: 'number',
|
---|
| 66 | begin: '\\b' + DATE_RE + TIME_RE + FRACTION_RE + ZONE_RE + '\\b'
|
---|
| 67 | };
|
---|
| 68 |
|
---|
| 69 | var VALUE_CONTAINER = {
|
---|
| 70 | end: ',',
|
---|
| 71 | endsWithParent: true,
|
---|
| 72 | excludeEnd: true,
|
---|
| 73 | keywords: LITERALS,
|
---|
| 74 | relevance: 0
|
---|
| 75 | };
|
---|
| 76 | var OBJECT = {
|
---|
| 77 | begin: /\{/,
|
---|
| 78 | end: /\}/,
|
---|
| 79 | contains: [VALUE_CONTAINER],
|
---|
| 80 | illegal: '\\n',
|
---|
| 81 | relevance: 0
|
---|
| 82 | };
|
---|
| 83 | var ARRAY = {
|
---|
| 84 | begin: '\\[',
|
---|
| 85 | end: '\\]',
|
---|
| 86 | contains: [VALUE_CONTAINER],
|
---|
| 87 | illegal: '\\n',
|
---|
| 88 | relevance: 0
|
---|
| 89 | };
|
---|
| 90 |
|
---|
| 91 | var MODES = [
|
---|
| 92 | KEY,
|
---|
| 93 | {
|
---|
| 94 | className: 'meta',
|
---|
| 95 | begin: '^---\\s*$',
|
---|
| 96 | relevance: 10
|
---|
| 97 | },
|
---|
| 98 | { // multi line string
|
---|
| 99 | // Blocks start with a | or > followed by a newline
|
---|
| 100 | //
|
---|
| 101 | // Indentation of subsequent lines must be the same to
|
---|
| 102 | // be considered part of the block
|
---|
| 103 | className: 'string',
|
---|
| 104 | begin: '[\\|>]([1-9]?[+-])?[ ]*\\n( +)[^ ][^\\n]*\\n(\\2[^\\n]+\\n?)*'
|
---|
| 105 | },
|
---|
| 106 | { // Ruby/Rails erb
|
---|
| 107 | begin: '<%[%=-]?',
|
---|
| 108 | end: '[%-]?%>',
|
---|
| 109 | subLanguage: 'ruby',
|
---|
| 110 | excludeBegin: true,
|
---|
| 111 | excludeEnd: true,
|
---|
| 112 | relevance: 0
|
---|
| 113 | },
|
---|
| 114 | { // named tags
|
---|
| 115 | className: 'type',
|
---|
| 116 | begin: '!\\w+!' + URI_CHARACTERS
|
---|
| 117 | },
|
---|
| 118 | // https://yaml.org/spec/1.2/spec.html#id2784064
|
---|
| 119 | { // verbatim tags
|
---|
| 120 | className: 'type',
|
---|
| 121 | begin: '!<' + URI_CHARACTERS + ">"
|
---|
| 122 | },
|
---|
| 123 | { // primary tags
|
---|
| 124 | className: 'type',
|
---|
| 125 | begin: '!' + URI_CHARACTERS
|
---|
| 126 | },
|
---|
| 127 | { // secondary tags
|
---|
| 128 | className: 'type',
|
---|
| 129 | begin: '!!' + URI_CHARACTERS
|
---|
| 130 | },
|
---|
| 131 | { // fragment id &ref
|
---|
| 132 | className: 'meta',
|
---|
| 133 | begin: '&' + hljs.UNDERSCORE_IDENT_RE + '$'
|
---|
| 134 | },
|
---|
| 135 | { // fragment reference *ref
|
---|
| 136 | className: 'meta',
|
---|
| 137 | begin: '\\*' + hljs.UNDERSCORE_IDENT_RE + '$'
|
---|
| 138 | },
|
---|
| 139 | { // array listing
|
---|
| 140 | className: 'bullet',
|
---|
| 141 | // TODO: remove |$ hack when we have proper look-ahead support
|
---|
| 142 | begin: '-(?=[ ]|$)',
|
---|
| 143 | relevance: 0
|
---|
| 144 | },
|
---|
| 145 | hljs.HASH_COMMENT_MODE,
|
---|
| 146 | {
|
---|
| 147 | beginKeywords: LITERALS,
|
---|
| 148 | keywords: { literal: LITERALS }
|
---|
| 149 | },
|
---|
| 150 | TIMESTAMP,
|
---|
| 151 | // numbers are any valid C-style number that
|
---|
| 152 | // sit isolated from other words
|
---|
| 153 | {
|
---|
| 154 | className: 'number',
|
---|
| 155 | begin: hljs.C_NUMBER_RE + '\\b',
|
---|
| 156 | relevance: 0
|
---|
| 157 | },
|
---|
| 158 | OBJECT,
|
---|
| 159 | ARRAY,
|
---|
| 160 | STRING
|
---|
| 161 | ];
|
---|
| 162 |
|
---|
| 163 | var VALUE_MODES = [...MODES];
|
---|
| 164 | VALUE_MODES.pop();
|
---|
| 165 | VALUE_MODES.push(CONTAINER_STRING);
|
---|
| 166 | VALUE_CONTAINER.contains = VALUE_MODES;
|
---|
| 167 |
|
---|
| 168 | return {
|
---|
| 169 | name: 'YAML',
|
---|
| 170 | case_insensitive: true,
|
---|
| 171 | aliases: [ 'yml' ],
|
---|
| 172 | contains: MODES
|
---|
| 173 | };
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | module.exports = yaml;
|
---|