Skip to content

Commit 8c02b8c

Browse files
Gergely Hegykozimarijnh
authored andcommitted
[jinja2 mode] Improve
Conflicts: mode/jinja2/jinja2.js
1 parent 35e8137 commit 8c02b8c

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
@@ -1,46 +1,122 @@
11
CodeMirror.defineMode("jinja2", function() {
22
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");
1622

1723
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;
2533
}
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+
}
3046
}
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";
3783
}
38-
if (stream.match(keywords)) {
39-
return "keyword";
84+
if(stream.match(number)) {
85+
return "number";
4086
}
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+
44120
return {
45121
startState: function () {
46122
return {tokenize: tokenBase};

0 commit comments

Comments
 (0)