UNPKG

2.69 kBJavaScriptView Raw
1(function (Prism) {
2
3 // https://freemarker.apache.org/docs/dgui_template_exp.html
4
5 // FTL expression with 4 levels of nesting supported
6 var FTL_EXPR = /[^<()"']|\((?:<expr>)*\)|<(?!#--)|<#--(?:[^-]|-(?!->))*-->|"(?:[^\\"]|\\.)*"|'(?:[^\\']|\\.)*'/.source;
7 for (var i = 0; i < 2; i++) {
8 FTL_EXPR = FTL_EXPR.replace(/<expr>/g, function () { return FTL_EXPR; });
9 }
10 FTL_EXPR = FTL_EXPR.replace(/<expr>/g, /[^\s\S]/.source);
11
12 var ftl = {
13 'comment': /<#--[\s\S]*?-->/,
14 'string': [
15 {
16 // raw string
17 pattern: /\br("|')(?:(?!\1)[^\\]|\\.)*\1/,
18 greedy: true
19 },
20 {
21 pattern: RegExp(/("|')(?:(?!\1|\$\{)[^\\]|\\.|\$\{(?:(?!\})(?:<expr>))*\})*\1/.source.replace(/<expr>/g, function () { return FTL_EXPR; })),
22 greedy: true,
23 inside: {
24 'interpolation': {
25 pattern: RegExp(/((?:^|[^\\])(?:\\\\)*)\$\{(?:(?!\})(?:<expr>))*\}/.source.replace(/<expr>/g, function () { return FTL_EXPR; })),
26 lookbehind: true,
27 inside: {
28 'interpolation-punctuation': {
29 pattern: /^\$\{|\}$/,
30 alias: 'punctuation'
31 },
32 rest: null
33 }
34 }
35 }
36 }
37 ],
38 'keyword': /\b(?:as)\b/,
39 'boolean': /\b(?:false|true)\b/,
40 'builtin-function': {
41 pattern: /((?:^|[^?])\?\s*)\w+/,
42 lookbehind: true,
43 alias: 'function'
44 },
45 'function': /\b\w+(?=\s*\()/,
46 'number': /\b\d+(?:\.\d+)?\b/,
47 'operator': /\.\.[<*!]?|->|--|\+\+|&&|\|\||\?{1,2}|[-+*/%!=<>]=?|\b(?:gt|gte|lt|lte)\b/,
48 'punctuation': /[,;.:()[\]{}]/
49 };
50
51 ftl.string[1].inside.interpolation.inside.rest = ftl;
52
53 Prism.languages.ftl = {
54 'ftl-comment': {
55 // the pattern is shortened to be more efficient
56 pattern: /^<#--[\s\S]*/,
57 alias: 'comment'
58 },
59 'ftl-directive': {
60 pattern: /^<[\s\S]+>$/,
61 inside: {
62 'directive': {
63 pattern: /(^<\/?)[#@][a-z]\w*/i,
64 lookbehind: true,
65 alias: 'keyword'
66 },
67 'punctuation': /^<\/?|\/?>$/,
68 'content': {
69 pattern: /\s*\S[\s\S]*/,
70 alias: 'ftl',
71 inside: ftl
72 }
73 }
74 },
75 'ftl-interpolation': {
76 pattern: /^\$\{[\s\S]*\}$/,
77 inside: {
78 'punctuation': /^\$\{|\}$/,
79 'content': {
80 pattern: /\s*\S[\s\S]*/,
81 alias: 'ftl',
82 inside: ftl
83 }
84 }
85 }
86 };
87
88 Prism.hooks.add('before-tokenize', function (env) {
89 // eslint-disable-next-line regexp/no-useless-lazy
90 var pattern = RegExp(/<#--[\s\S]*?-->|<\/?[#@][a-zA-Z](?:<expr>)*?>|\$\{(?:<expr>)*?\}/.source.replace(/<expr>/g, function () { return FTL_EXPR; }), 'gi');
91 Prism.languages['markup-templating'].buildPlaceholders(env, 'ftl', pattern);
92 });
93
94 Prism.hooks.add('after-tokenize', function (env) {
95 Prism.languages['markup-templating'].tokenizePlaceholders(env, 'ftl');
96 });
97
98}(Prism));