5
if (typeof exports == "object" && typeof module == "object")
6
mod(require("../../lib/codemirror"), require("../htmlmixed/htmlmixed"),
7
require("../../addon/mode/overlay"));
8
else if (typeof define == "function" && define.amd)
9
define(["../../lib/codemirror", "../htmlmixed/htmlmixed",
10
"../../addon/mode/overlay"], mod);
13
})(function(CodeMirror) {
16
CodeMirror.defineMode("django:inner", function() {
17
var keywords = ["block", "endblock", "for", "endfor", "true", "false", "filter", "endfilter",
18
"loop", "none", "self", "super", "if", "elif", "endif", "as", "else", "import",
19
"with", "endwith", "without", "context", "ifequal", "endifequal", "ifnotequal",
20
"endifnotequal", "extends", "include", "load", "comment", "endcomment",
21
"empty", "url", "static", "trans", "blocktrans", "endblocktrans", "now",
22
"regroup", "lorem", "ifchanged", "endifchanged", "firstof", "debug", "cycle",
23
"csrf_token", "autoescape", "endautoescape", "spaceless", "endspaceless",
24
"ssi", "templatetag", "verbatim", "endverbatim", "widthratio"],
25
filters = ["add", "addslashes", "capfirst", "center", "cut", "date",
26
"default", "default_if_none", "dictsort",
27
"dictsortreversed", "divisibleby", "escape", "escapejs",
28
"filesizeformat", "first", "floatformat", "force_escape",
29
"get_digit", "iriencode", "join", "last", "length",
30
"length_is", "linebreaks", "linebreaksbr", "linenumbers",
31
"ljust", "lower", "make_list", "phone2numeric", "pluralize",
32
"pprint", "random", "removetags", "rjust", "safe",
33
"safeseq", "slice", "slugify", "stringformat", "striptags",
34
"time", "timesince", "timeuntil", "title", "truncatechars",
35
"truncatechars_html", "truncatewords", "truncatewords_html",
36
"unordered_list", "upper", "urlencode", "urlize",
37
"urlizetrunc", "wordcount", "wordwrap", "yesno"],
38
operators = ["==", "!=", "<", ">", "<=", ">="],
39
wordOperators = ["in", "not", "or", "and"];
41
keywords = new RegExp("^\\b(" + keywords.join("|") + ")\\b");
42
filters = new RegExp("^\\b(" + filters.join("|") + ")\\b");
43
operators = new RegExp("^\\b(" + operators.join("|") + ")\\b");
44
wordOperators = new RegExp("^\\b(" + wordOperators.join("|") + ")\\b");
49
function tokenBase (stream, state) {
51
if (stream.match("{{")) {
52
state.tokenize = inVariable;
54
} else if (stream.match("{%")) {
55
state.tokenize = inTag;
57
} else if (stream.match("{#")) {
58
state.tokenize = inComment;
64
while (stream.next() != null && !stream.match(/\{[{%#]/, false)) {}
71
function inString (delimiter, previousTokenizer) {
72
return function (stream, state) {
73
if (!state.escapeNext && stream.eat(delimiter)) {
74
state.tokenize = previousTokenizer;
76
if (state.escapeNext) {
77
state.escapeNext = false;
80
var ch = stream.next();
85
state.escapeNext = true;
94
function inVariable (stream, state) {
97
state.waitDot = false;
99
if (stream.peek() != ".") {
104
if (stream.match(/\.\W+/)) {
106
} else if (stream.eat(".")) {
107
state.waitProperty = true;
110
throw Error ("Unexpected error while waiting for property.");
115
if (state.waitPipe) {
116
state.waitPipe = false;
118
if (stream.peek() != "|") {
123
if (stream.match(/\.\W+/)) {
125
} else if (stream.eat("|")) {
126
state.waitFilter = true;
129
throw Error ("Unexpected error while waiting for filter.");
134
if (state.waitProperty) {
135
state.waitProperty = false;
136
if (stream.match(/\b(\w+)\b/)) {
137
state.waitDot = true;
138
state.waitPipe = true;
144
if (state.waitFilter) {
145
state.waitFilter = false;
146
if (stream.match(filters)) {
152
if (stream.eatSpace()) {
153
state.waitProperty = false;
158
if (stream.match(/\b\d+(\.\d+)?\b/)) {
163
if (stream.match("'")) {
164
state.tokenize = inString("'", state.tokenize);
166
} else if (stream.match('"')) {
167
state.tokenize = inString('"', state.tokenize);
172
if (stream.match(/\b(\w+)\b/) && !state.foundVariable) {
173
state.waitDot = true;
174
state.waitPipe = true;
179
if (stream.match("}}")) {
180
state.waitProperty = null;
181
state.waitFilter = null;
182
state.waitDot = null;
183
state.waitPipe = null;
184
state.tokenize = tokenBase;
193
function inTag (stream, state) {
196
state.waitDot = false;
198
if (stream.peek() != ".") {
203
if (stream.match(/\.\W+/)) {
205
} else if (stream.eat(".")) {
206
state.waitProperty = true;
209
throw Error ("Unexpected error while waiting for property.");
214
if (state.waitPipe) {
215
state.waitPipe = false;
217
if (stream.peek() != "|") {
222
if (stream.match(/\.\W+/)) {
224
} else if (stream.eat("|")) {
225
state.waitFilter = true;
228
throw Error ("Unexpected error while waiting for filter.");
233
if (state.waitProperty) {
234
state.waitProperty = false;
235
if (stream.match(/\b(\w+)\b/)) {
236
state.waitDot = true;
237
state.waitPipe = true;
243
if (state.waitFilter) {
244
state.waitFilter = false;
245
if (stream.match(filters)) {
251
if (stream.eatSpace()) {
252
state.waitProperty = false;
257
if (stream.match(/\b\d+(\.\d+)?\b/)) {
262
if (stream.match("'")) {
263
state.tokenize = inString("'", state.tokenize);
265
} else if (stream.match('"')) {
266
state.tokenize = inString('"', state.tokenize);
271
if (stream.match(operators)) {
276
if (stream.match(wordOperators)) {
281
var keywordMatch = stream.match(keywords);
283
if (keywordMatch[0] == "comment") {
284
state.blockCommentTag = true;
290
if (stream.match(/\b(\w+)\b/)) {
291
state.waitDot = true;
292
state.waitPipe = true;
297
if (stream.match("%}")) {
298
state.waitProperty = null;
299
state.waitFilter = null;
300
state.waitDot = null;
301
state.waitPipe = null;
304
if (state.blockCommentTag) {
305
state.blockCommentTag = false;
306
state.tokenize = inBlockComment;
308
state.tokenize = tokenBase;
319
function inComment (stream, state) {
320
if (stream.match(/^.*?#\}/)) state.tokenize = tokenBase
321
else stream.skipToEnd()
326
function inBlockComment (stream, state) {
327
if (stream.match(/\{%\s*endcomment\s*%\}/, false)) {
328
state.tokenize = inTag;
338
startState: function () {
339
return {tokenize: tokenBase};
341
token: function (stream, state) {
342
return state.tokenize(stream, state);
344
blockCommentStart: "{% comment %}",
345
blockCommentEnd: "{% endcomment %}"
349
CodeMirror.defineMode("django", function(config) {
350
var htmlBase = CodeMirror.getMode(config, "text/html");
351
var djangoInner = CodeMirror.getMode(config, "django:inner");
352
return CodeMirror.overlayMode(htmlBase, djangoInner);
355
CodeMirror.defineMIME("text/x-django", "django");