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