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