|
1 | 1 | CodeMirror.defineMode("jinja2", function() { |
2 | 2 | var keywords = ["and", "as", "block", "endblock", "by", "cycle", "debug", "else", "elif", |
3 | | - "extends", "filter", "endfilter", "firstof", "for", |
4 | | - "endfor", "if", "endif", "ifchanged", "endifchanged", |
5 | | - "ifequal", "endifequal", "ifnotequal", |
6 | | - "endifnotequal", "in", "include", "load", "not", "now", "or", |
7 | | - "parsed", "regroup", "reversed", "spaceless", |
8 | | - "endspaceless", "ssi", "templatetag", "openblock", |
9 | | - "closeblock", "openvariable", "closevariable", |
10 | | - "openbrace", "closebrace", "opencomment", |
11 | | - "closecomment", "widthratio", "url", "with", "endwith", |
12 | | - "get_current_language", "trans", "noop", "blocktrans", |
13 | | - "endblocktrans", "get_available_languages", |
14 | | - "get_current_language_bidi", "plural"]; |
15 | | - keywords = new RegExp("^((" + keywords.join(")|(") + "))\\b"); |
| 3 | + "extends", "filter", "endfilter", "firstof", "for", |
| 4 | + "endfor", "if", "endif", "ifchanged", "endifchanged", |
| 5 | + "ifequal", "endifequal", "ifnotequal", |
| 6 | + "endifnotequal", "in", "include", "load", "not", "now", "or", |
| 7 | + "parsed", "regroup", "reversed", "spaceless", |
| 8 | + "endspaceless", "ssi", "templatetag", "openblock", |
| 9 | + "closeblock", "openvariable", "closevariable", |
| 10 | + "openbrace", "closebrace", "opencomment", |
| 11 | + "closecomment", "widthratio", "url", "with", "endwith", |
| 12 | + "get_current_language", "trans", "endtrans", "noop", "blocktrans", |
| 13 | + "endblocktrans", "get_available_languages", |
| 14 | + "get_current_language_bidi", "plural"], |
| 15 | + operator = /^[+\-*&%=<>!?|~^]/, |
| 16 | + sign = /^[:\[\(\{]/, |
| 17 | + atom = ["true", "false"], |
| 18 | + number = /^(\d[+\-\*\/])?\d+(\.\d+)?/; |
| 19 | + |
| 20 | + keywords = new RegExp("((" + keywords.join(")|(") + "))\\b"); |
| 21 | + atom = new RegExp("((" + atom.join(")|(") + "))\\b"); |
16 | 22 |
|
17 | 23 | function tokenBase (stream, state) { |
18 | | - var ch = stream.next(); |
19 | | - if (ch == "{") { |
20 | | - if (ch = stream.eat(/\{|%|#/)) { |
21 | | - stream.eat("-"); |
22 | | - state.tokenize = inTag(ch); |
23 | | - return "tag"; |
24 | | - } |
| 24 | + var ch = stream.peek(); |
| 25 | + |
| 26 | + //Comment |
| 27 | + if (state.incomment) { |
| 28 | + if(!stream.skipTo("#}")) { |
| 29 | + stream.skipToEnd(); |
| 30 | + } else { |
| 31 | + stream.eatWhile(/\#|}/); |
| 32 | + state.incomment = false; |
25 | 33 | } |
26 | | - } |
27 | | - function inTag (close) { |
28 | | - if (close == "{") { |
29 | | - close = "}"; |
| 34 | + return "comment"; |
| 35 | + //Tag |
| 36 | + } else if (state.intag) { |
| 37 | + //After operator |
| 38 | + if(state.operator) { |
| 39 | + state.operator = false; |
| 40 | + if(stream.match(atom)) { |
| 41 | + return "atom"; |
| 42 | + } |
| 43 | + if(stream.match(number)) { |
| 44 | + return "number"; |
| 45 | + } |
30 | 46 | } |
31 | | - return function (stream, state) { |
32 | | - var ch = stream.next(); |
33 | | - if ((ch == close || (ch == "-" && stream.eat(close))) |
34 | | - && stream.eat("}")) { |
35 | | - state.tokenize = tokenBase; |
36 | | - return "tag"; |
| 47 | + //After sign |
| 48 | + if(state.sign) { |
| 49 | + state.sign = false; |
| 50 | + if(stream.match(atom)) { |
| 51 | + return "atom"; |
| 52 | + } |
| 53 | + if(stream.match(number)) { |
| 54 | + return "number"; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + if(state.instring) { |
| 59 | + if(ch == state.instring) { |
| 60 | + state.instring = false; |
| 61 | + } |
| 62 | + stream.next(); |
| 63 | + return "string"; |
| 64 | + } else if(ch == "'" || ch == '"') { |
| 65 | + state.instring = ch; |
| 66 | + stream.next(); |
| 67 | + return "string"; |
| 68 | + } else if(stream.match(state.intag + "}") || stream.eat("-") && stream.match(state.intag + "}")) { |
| 69 | + state.intag = false; |
| 70 | + return "tag"; |
| 71 | + } else if(stream.match(operator)) { |
| 72 | + state.operator = true; |
| 73 | + return "operator"; |
| 74 | + } else if(stream.match(sign)) { |
| 75 | + state.sign = true; |
| 76 | + } else { |
| 77 | + if(stream.eat(" ") || stream.sol()) { |
| 78 | + if(stream.match(keywords)) { |
| 79 | + return "keyword"; |
| 80 | + } |
| 81 | + if(stream.match(atom)) { |
| 82 | + return "atom"; |
37 | 83 | } |
38 | | - if (stream.match(keywords)) { |
39 | | - return "keyword"; |
| 84 | + if(stream.match(number)) { |
| 85 | + return "number"; |
40 | 86 | } |
41 | | - return close == "#" ? "comment" : "string"; |
42 | | - }; |
43 | | - } |
| 87 | + if(stream.sol()) { |
| 88 | + stream.next(); |
| 89 | + } |
| 90 | + } else { |
| 91 | + stream.next(); |
| 92 | + } |
| 93 | + |
| 94 | + } |
| 95 | + return "variable"; |
| 96 | + } else if (stream.eat("{")) { |
| 97 | + if (ch = stream.eat("#")) { |
| 98 | + state.incomment = true; |
| 99 | + if(!stream.skipTo("#}")) { |
| 100 | + stream.skipToEnd(); |
| 101 | + } else { |
| 102 | + stream.eatWhile(/\#|}/); |
| 103 | + state.incomment = false; |
| 104 | + } |
| 105 | + return "comment"; |
| 106 | + //Open tag |
| 107 | + } else if (ch = stream.eat(/\{|%/)) { |
| 108 | + //Cache close tag |
| 109 | + state.intag = ch; |
| 110 | + if(ch == "{") { |
| 111 | + state.intag = "}"; |
| 112 | + } |
| 113 | + stream.eat("-"); |
| 114 | + return "tag"; |
| 115 | + } |
| 116 | + } |
| 117 | + stream.next(); |
| 118 | + }; |
| 119 | + |
44 | 120 | return { |
45 | 121 | startState: function () { |
46 | 122 | return {tokenize: tokenBase}; |
|
0 commit comments