Skip to content

Commit 0ffe3e9

Browse files
Gergely Hegykozimarijnh
authored andcommitted
[jinja2 mode] Improve
1 parent d3ba3a2 commit 0ffe3e9

File tree

2 files changed

+131
-51
lines changed

2 files changed

+131
-51
lines changed

mode/jinja2/index.html

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,26 @@
2525
<article>
2626
<h2>Jinja2 mode</h2>
2727
<form><textarea id="code" name="code">
28-
&lt;html style="color: green"&gt;
29-
&lt;!-- this is a comment --&gt;
30-
&lt;head&gt;
31-
&lt;title&gt;Jinja2 Example&lt;/title&gt;
32-
&lt;/head&gt;
33-
&lt;body&gt;
34-
&lt;ul&gt;
35-
{# this is a comment #}
36-
{%- for item in li -%}
37-
&lt;li&gt;
38-
{{ item.label }}
39-
&lt;/li&gt;
40-
{% endfor -%}
41-
&lt;/ul&gt;
42-
&lt;/body&gt;
43-
&lt;/html&gt;
28+
{# this is a comment #}
29+
{%- for item in li -%}
30+
&lt;li&gt;{{ item.label }}&lt;/li&gt;
31+
{% endfor -%}
32+
{{ item.sand == true and item.keyword == false ? 1 : 0 }}
33+
{{ app.get(55, 1.2, true) }}
34+
{% if app.get(&#39;_route&#39;) == (&#39;_home&#39;) %}home{% endif %}
35+
{% if app.session.flashbag.has(&#39;message&#39;) %}
36+
{% for message in app.session.flashbag.get(&#39;message&#39;) %}
37+
{{ message.content }}
38+
{% endfor %}
39+
{% endif %}
40+
{{ path(&#39;_home&#39;, {&#39;section&#39;: app.request.get(&#39;section&#39;)}) }}
41+
{{ path(&#39;_home&#39;, {
42+
&#39;section&#39;: app.request.get(&#39;section&#39;),
43+
&#39;boolean&#39;: true,
44+
&#39;number&#39;: 55.33
45+
})
46+
}}
47+
{% include (&#39;test.incl.html.twig&#39;) %}
4448
</textarea></form>
4549
<script>
4650
var editor =

mode/jinja2/jinja2.js

Lines changed: 111 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,47 +10,123 @@
1010

1111
CodeMirror.defineMode("jinja2", function() {
1212
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");
2632

2733
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;
3443
}
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;
4680
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+
47104
}
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";
50125
}
51-
return close == "#" ? "comment" : "string";
52-
};
53-
}
126+
}
127+
stream.next();
128+
};
129+
54130
return {
55131
startState: function () {
56132
return {tokenize: tokenBase};

0 commit comments

Comments
 (0)