comparison .cms/lib/codemirror/mode/twig/twig.js @ 0:78edf6b517a0 draft

24.10
author Coffee CMS <info@coffee-cms.ru>
date Fri, 11 Oct 2024 22:40:23 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:78edf6b517a0
1 // CodeMirror, copyright (c) by Marijn Haverbeke and others
2 // Distributed under an MIT license: https://codemirror.net/5/LICENSE
3
4 (function(mod) {
5 if (typeof exports == "object" && typeof module == "object") // CommonJS
6 mod(require("../../lib/codemirror"), require("../../addon/mode/multiplex"));
7 else if (typeof define == "function" && define.amd) // AMD
8 define(["../../lib/codemirror", "../../addon/mode/multiplex"], mod);
9 else // Plain browser env
10 mod(CodeMirror);
11 })(function(CodeMirror) {
12 "use strict";
13
14 CodeMirror.defineMode("twig:inner", function() {
15 var keywords = ["and", "as", "autoescape", "endautoescape", "block", "do", "endblock", "else", "elseif", "extends", "for", "endfor", "embed", "endembed", "filter", "endfilter", "flush", "from", "if", "endif", "in", "is", "include", "import", "not", "or", "set", "spaceless", "endspaceless", "with", "endwith", "trans", "endtrans", "blocktrans", "endblocktrans", "macro", "endmacro", "use", "verbatim", "endverbatim"],
16 operator = /^[+\-*&%=<>!?|~^]/,
17 sign = /^[:\[\(\{]/,
18 atom = ["true", "false", "null", "empty", "defined", "divisibleby", "divisible by", "even", "odd", "iterable", "sameas", "same as"],
19 number = /^(\d[+\-\*\/])?\d+(\.\d+)?/;
20
21 keywords = new RegExp("((" + keywords.join(")|(") + "))\\b");
22 atom = new RegExp("((" + atom.join(")|(") + "))\\b");
23
24 function tokenBase (stream, state) {
25 var ch = stream.peek();
26
27 //Comment
28 if (state.incomment) {
29 if (!stream.skipTo("#}")) {
30 stream.skipToEnd();
31 } else {
32 stream.eatWhile(/\#|}/);
33 state.incomment = false;
34 }
35 return "comment";
36 //Tag
37 } else if (state.intag) {
38 //After operator
39 if (state.operator) {
40 state.operator = false;
41 if (stream.match(atom)) {
42 return "atom";
43 }
44 if (stream.match(number)) {
45 return "number";
46 }
47 }
48 //After sign
49 if (state.sign) {
50 state.sign = false;
51 if (stream.match(atom)) {
52 return "atom";
53 }
54 if (stream.match(number)) {
55 return "number";
56 }
57 }
58
59 if (state.instring) {
60 if (ch == state.instring) {
61 state.instring = false;
62 }
63 stream.next();
64 return "string";
65 } else if (ch == "'" || ch == '"') {
66 state.instring = ch;
67 stream.next();
68 return "string";
69 } else if (stream.match(state.intag + "}") || stream.eat("-") && stream.match(state.intag + "}")) {
70 state.intag = false;
71 return "tag";
72 } else if (stream.match(operator)) {
73 state.operator = true;
74 return "operator";
75 } else if (stream.match(sign)) {
76 state.sign = true;
77 } else {
78 if (stream.eat(" ") || stream.sol()) {
79 if (stream.match(keywords)) {
80 return "keyword";
81 }
82 if (stream.match(atom)) {
83 return "atom";
84 }
85 if (stream.match(number)) {
86 return "number";
87 }
88 if (stream.sol()) {
89 stream.next();
90 }
91 } else {
92 stream.next();
93 }
94
95 }
96 return "variable";
97 } else if (stream.eat("{")) {
98 if (stream.eat("#")) {
99 state.incomment = true;
100 if (!stream.skipTo("#}")) {
101 stream.skipToEnd();
102 } else {
103 stream.eatWhile(/\#|}/);
104 state.incomment = false;
105 }
106 return "comment";
107 //Open tag
108 } else if (ch = stream.eat(/\{|%/)) {
109 //Cache close tag
110 state.intag = ch;
111 if (ch == "{") {
112 state.intag = "}";
113 }
114 stream.eat("-");
115 return "tag";
116 }
117 }
118 stream.next();
119 };
120
121 return {
122 startState: function () {
123 return {};
124 },
125 token: function (stream, state) {
126 return tokenBase(stream, state);
127 }
128 };
129 });
130
131 CodeMirror.defineMode("twig", function(config, parserConfig) {
132 var twigInner = CodeMirror.getMode(config, "twig:inner");
133 if (!parserConfig || !parserConfig.base) return twigInner;
134 return CodeMirror.multiplexingMode(
135 CodeMirror.getMode(config, parserConfig.base), {
136 open: /\{[{#%]/, close: /[}#%]\}/, mode: twigInner, parseDelimiters: true
137 }
138 );
139 });
140 CodeMirror.defineMIME("text/x-twig", "twig");
141 });