| 1 |
var keywords = ["and", "as", "block", "endblock", "by", "cycle", "debug", "else", "elif", |
| 2 |
"extends", "filter", "endfilter", "firstof", "do", "for", |
| 3 |
"endfor", "if", "endif", "ifchanged", "endifchanged", |
| 4 |
"ifequal", "endifequal", "ifnotequal", "set", "raw", "endraw", |
| 5 |
"endifnotequal", "in", "include", "load", "not", "now", "or", |
| 6 |
"parsed", "regroup", "reversed", "spaceless", "call", "endcall", "macro", |
| 7 |
"endmacro", "endspaceless", "ssi", "templatetag", "openblock", |
| 8 |
"closeblock", "openvariable", "closevariable", "without", "context", |
| 9 |
"openbrace", "closebrace", "opencomment", |
| 10 |
"closecomment", "widthratio", "url", "with", "endwith", |
| 11 |
"get_current_language", "trans", "endtrans", "noop", "blocktrans", |
| 12 |
"endblocktrans", "get_available_languages", |
| 13 |
"get_current_language_bidi", "pluralize", "autoescape", "endautoescape"], |
| 14 |
operator = /^[+\-*&%=<>!?|~^]/, |
| 15 |
sign = /^[:\[\(\{]/, |
| 16 |
atom = ["true", "false"], |
| 17 |
number = /^(\d[+\-\*\/])?\d+(\.\d+)?/; |
| 18 |
|
| 19 |
keywords = new RegExp("((" + keywords.join(")|(") + "))\\b"); |
| 20 |
atom = new RegExp("((" + atom.join(")|(") + "))\\b"); |
| 21 |
|
| 22 |
function tokenBase (stream, state) { |
| 23 |
var ch = stream.peek(); |
| 24 |
|
| 25 |
//Comment |
| 26 |
if (state.incomment) { |
| 27 |
if(!stream.skipTo("#}")) { |
| 28 |
stream.skipToEnd(); |
| 29 |
} else { |
| 30 |
stream.eatWhile(/\#|}/); |
| 31 |
state.incomment = false; |
| 32 |
} |
| 33 |
return "comment"; |
| 34 |
//Tag |
| 35 |
} else if (state.intag) { |
| 36 |
//After operator |
| 37 |
if(state.operator) { |
| 38 |
state.operator = false; |
| 39 |
if(stream.match(atom)) { |
| 40 |
return "atom"; |
| 41 |
} |
| 42 |
if(stream.match(number)) { |
| 43 |
return "number"; |
| 44 |
} |
| 45 |
} |
| 46 |
//After sign |
| 47 |
if(state.sign) { |
| 48 |
state.sign = false; |
| 49 |
if(stream.match(atom)) { |
| 50 |
return "atom"; |
| 51 |
} |
| 52 |
if(stream.match(number)) { |
| 53 |
return "number"; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
if(state.instring) { |
| 58 |
if(ch == state.instring) { |
| 59 |
state.instring = false; |
| 60 |
} |
| 61 |
stream.next(); |
| 62 |
return "string"; |
| 63 |
} else if(ch == "'" || ch == '"') { |
| 64 |
state.instring = ch; |
| 65 |
stream.next(); |
| 66 |
return "string"; |
| 67 |
} else if (state.inbraces > 0 && ch ==")") { |
| 68 |
stream.next() |
| 69 |
state.inbraces--; |
| 70 |
} |
| 71 |
else if (ch == "(") { |
| 72 |
stream.next() |
| 73 |
state.inbraces++; |
| 74 |
} |
| 75 |
else if (state.inbrackets > 0 && ch =="]") { |
| 76 |
stream.next() |
| 77 |
state.inbrackets--; |
| 78 |
} |
| 79 |
else if (ch == "[") { |
| 80 |
stream.next() |
| 81 |
state.inbrackets++; |
| 82 |
} else if (!state.lineTag && (stream.match(state.intag + "}") || stream.eat("-") && stream.match(state.intag + "}"))) { |
| 83 |
state.intag = false; |
| 84 |
return "tag"; |
| 85 |
} else if(stream.match(operator)) { |
| 86 |
state.operator = true; |
| 87 |
return "operator"; |
| 88 |
} else if(stream.match(sign)) { |
| 89 |
state.sign = true; |
| 90 |
} else { |
| 91 |
if (stream.column() == 1 && state.lineTag && stream.match(keywords)) { |
| 92 |
//allow nospace after tag before the keyword |
| 93 |
return "keyword"; |
| 94 |
} |
| 95 |
if(stream.eat(" ") || stream.sol()) { |
| 96 |
if(stream.match(keywords)) { |
| 97 |
return "keyword"; |
| 98 |
} |
| 99 |
if(stream.match(atom)) { |
| 100 |
return "atom"; |
| 101 |
} |
| 102 |
if(stream.match(number)) { |
| 103 |
return "number"; |
| 104 |
} |
| 105 |
if(stream.sol()) { |
| 106 |
stream.next(); |
| 107 |
} |
| 108 |
} else { |
| 109 |
stream.next(); |
| 110 |
} |
| 111 |
|
| 112 |
} |
| 113 |
return "variable"; |
| 114 |
} else if (stream.eat("{")) { |
| 115 |
if (stream.eat("#")) { |
| 116 |
state.incomment = true; |
| 117 |
if(!stream.skipTo("#}")) { |
| 118 |
stream.skipToEnd(); |
| 119 |
} else { |
| 120 |
stream.eatWhile(/\#|}/); |
| 121 |
state.incomment = false; |
| 122 |
} |
| 123 |
return "comment"; |
| 124 |
//Open tag |
| 125 |
} else if (ch = stream.eat(/\{|%/)) { |
| 126 |
//Cache close tag |
| 127 |
state.intag = ch; |
| 128 |
state.inbraces = 0; |
| 129 |
state.inbrackets = 0; |
| 130 |
if(ch == "{") { |
| 131 |
state.intag = "}"; |
| 132 |
} |
| 133 |
stream.eat("-"); |
| 134 |
return "tag"; |
| 135 |
} |
| 136 |
//Line statements |
| 137 |
} else if (stream.eat('#')) { |
| 138 |
if (stream.peek() == '#') { |
| 139 |
stream.skipToEnd(); |
| 140 |
return "comment" |
| 141 |
} |
| 142 |
else if (!stream.eol()) { |
| 143 |
state.intag = true; |
| 144 |
state.lineTag = true; |
| 145 |
state.inbraces = 0; |
| 146 |
state.inbrackets = 0; |
| 147 |
return "tag"; |
| 148 |
} |
| 149 |
} |
| 150 |
stream.next(); |
| 151 |
}; |
| 152 |
|
| 153 |
export const jinja2 = { |
| 154 |
name: "jinja2", |
| 155 |
startState: function () { |
| 156 |
return {tokenize: tokenBase, inbrackets: 0, inbraces: 0}; |
| 157 |
}, |
| 158 |
token: function(stream, state) { |
| 159 |
var style = state.tokenize(stream, state); |
| 160 |
if (stream.eol() && state.lineTag && !state.instring && state.inbraces == 0 && state.inbrackets == 0) { |
| 161 |
//Close line statement at the EOL |
| 162 |
state.intag = false |
| 163 |
state.lineTag = false |
| 164 |
} |
| 165 |
return style; |
| 166 |
}, |
| 167 |
languageData: { |
| 168 |
commentTokens: {block: {open: "{#", close: "#}", line: "##"}} |
| 169 |
} |
| 170 |
}; |