]> luflow.net public git repositories - flow-web.git/blob - static/highlight/languages/twig.js
Initial commit.
[flow-web.git] / static / highlight / languages / twig.js
1 /*! `twig` grammar compiled for Highlight.js 11.11.1 */
2 (function(){
3 var hljsGrammar = (function () {
4 'use strict';
5
6 /*
7 Language: Twig
8 Requires: xml.js
9 Author: Luke Holder <lukemh@gmail.com>
10 Description: Twig is a templating language for PHP
11 Website: https://twig.symfony.com
12 Category: template
13 */
14
15 function twig(hljs) {
16 const regex = hljs.regex;
17 const FUNCTION_NAMES = [
18 "absolute_url",
19 "asset|0",
20 "asset_version",
21 "attribute",
22 "block",
23 "constant",
24 "controller|0",
25 "country_timezones",
26 "csrf_token",
27 "cycle",
28 "date",
29 "dump",
30 "expression",
31 "form|0",
32 "form_end",
33 "form_errors",
34 "form_help",
35 "form_label",
36 "form_rest",
37 "form_row",
38 "form_start",
39 "form_widget",
40 "html_classes",
41 "include",
42 "is_granted",
43 "logout_path",
44 "logout_url",
45 "max",
46 "min",
47 "parent",
48 "path|0",
49 "random",
50 "range",
51 "relative_path",
52 "render",
53 "render_esi",
54 "source",
55 "template_from_string",
56 "url|0"
57 ];
58
59 const FILTERS = [
60 "abs",
61 "abbr_class",
62 "abbr_method",
63 "batch",
64 "capitalize",
65 "column",
66 "convert_encoding",
67 "country_name",
68 "currency_name",
69 "currency_symbol",
70 "data_uri",
71 "date",
72 "date_modify",
73 "default",
74 "escape",
75 "file_excerpt",
76 "file_link",
77 "file_relative",
78 "filter",
79 "first",
80 "format",
81 "format_args",
82 "format_args_as_text",
83 "format_currency",
84 "format_date",
85 "format_datetime",
86 "format_file",
87 "format_file_from_text",
88 "format_number",
89 "format_time",
90 "html_to_markdown",
91 "humanize",
92 "inky_to_html",
93 "inline_css",
94 "join",
95 "json_encode",
96 "keys",
97 "language_name",
98 "last",
99 "length",
100 "locale_name",
101 "lower",
102 "map",
103 "markdown",
104 "markdown_to_html",
105 "merge",
106 "nl2br",
107 "number_format",
108 "raw",
109 "reduce",
110 "replace",
111 "reverse",
112 "round",
113 "slice",
114 "slug",
115 "sort",
116 "spaceless",
117 "split",
118 "striptags",
119 "timezone_name",
120 "title",
121 "trans",
122 "transchoice",
123 "trim",
124 "u|0",
125 "upper",
126 "url_encode",
127 "yaml_dump",
128 "yaml_encode"
129 ];
130
131 let TAG_NAMES = [
132 "apply",
133 "autoescape",
134 "block",
135 "cache",
136 "deprecated",
137 "do",
138 "embed",
139 "extends",
140 "filter",
141 "flush",
142 "for",
143 "form_theme",
144 "from",
145 "if",
146 "import",
147 "include",
148 "macro",
149 "sandbox",
150 "set",
151 "stopwatch",
152 "trans",
153 "trans_default_domain",
154 "transchoice",
155 "use",
156 "verbatim",
157 "with"
158 ];
159
160 TAG_NAMES = TAG_NAMES.concat(TAG_NAMES.map(t => `end${t}`));
161
162 const STRING = {
163 scope: 'string',
164 variants: [
165 {
166 begin: /'/,
167 end: /'/
168 },
169 {
170 begin: /"/,
171 end: /"/
172 },
173 ]
174 };
175
176 const NUMBER = {
177 scope: "number",
178 match: /\d+/
179 };
180
181 const PARAMS = {
182 begin: /\(/,
183 end: /\)/,
184 excludeBegin: true,
185 excludeEnd: true,
186 contains: [
187 STRING,
188 NUMBER
189 ]
190 };
191
192
193 const FUNCTIONS = {
194 beginKeywords: FUNCTION_NAMES.join(" "),
195 keywords: { name: FUNCTION_NAMES },
196 relevance: 0,
197 contains: [ PARAMS ]
198 };
199
200 const FILTER = {
201 match: /\|(?=[A-Za-z_]+:?)/,
202 beginScope: "punctuation",
203 relevance: 0,
204 contains: [
205 {
206 match: /[A-Za-z_]+:?/,
207 keywords: FILTERS
208 },
209 ]
210 };
211
212 const tagNamed = (tagnames, { relevance }) => {
213 return {
214 beginScope: {
215 1: 'template-tag',
216 3: 'name'
217 },
218 relevance: relevance || 2,
219 endScope: 'template-tag',
220 begin: [
221 /\{%/,
222 /\s*/,
223 regex.either(...tagnames)
224 ],
225 end: /%\}/,
226 keywords: "in",
227 contains: [
228 FILTER,
229 FUNCTIONS,
230 STRING,
231 NUMBER
232 ]
233 };
234 };
235
236 const CUSTOM_TAG_RE = /[a-z_]+/;
237 const TAG = tagNamed(TAG_NAMES, { relevance: 2 });
238 const CUSTOM_TAG = tagNamed([ CUSTOM_TAG_RE ], { relevance: 1 });
239
240 return {
241 name: 'Twig',
242 aliases: [ 'craftcms' ],
243 case_insensitive: true,
244 subLanguage: 'xml',
245 contains: [
246 hljs.COMMENT(/\{#/, /#\}/),
247 TAG,
248 CUSTOM_TAG,
249 {
250 className: 'template-variable',
251 begin: /\{\{/,
252 end: /\}\}/,
253 contains: [
254 'self',
255 FILTER,
256 FUNCTIONS,
257 STRING,
258 NUMBER
259 ]
260 }
261 ]
262 };
263 }
264
265 return twig;
266
267 })();
268
269 hljs.registerLanguage('twig', hljsGrammar);
270 })();