Skip to content

Commit ba52f83

Browse files
pariskmarijnh
authored andcommitted
[django mode] Add
Added django.js which implements the following modes - django (text/x-django) - django:inner
1 parent c2b89fd commit ba52f83

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-0
lines changed

doc/compress.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ <h2>Script compression helper</h2>
9292
<option value="http://codemirror.net/mode/css/css.js">css.js</option>
9393
<option value="http://codemirror.net/mode/d/d.js">d.js</option>
9494
<option value="http://codemirror.net/mode/diff/diff.js">diff.js</option>
95+
<option value="http://codemirror.net/mode/django/django.js">django.js</option>
9596
<option value="http://codemirror.net/mode/dtd/dtd.js">dtd.js</option>
9697
<option value="http://codemirror.net/mode/dylan/dylan.js">dylan.js</option>
9798
<option value="http://codemirror.net/mode/ecl/ecl.js">ecl.js</option>

mode/django/django.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
(function(CodeMirror) {
2+
"use strict";
3+
4+
CodeMirror.defineMode("django:inner", function() {
5+
var keywords = ["block", "endblock", "for", "endfor", "in", "true", "false",
6+
"loop", "none", "self", "super", "if", "endif", "as", "not", "and",
7+
"else", "import", "with", "endwith", "without", "context", "ifequal", "endifequal",
8+
"ifnotequal", "endifnotequal", "extends", "include", "load", "length", "comment",
9+
"endcomment", "empty"];
10+
keywords = new RegExp("^((" + keywords.join(")|(") + "))\\b");
11+
12+
function tokenBase (stream, state) {
13+
stream.eatWhile(/[^\{]/);
14+
var ch = stream.next();
15+
if (ch == "{") {
16+
if (ch = stream.eat(/\{|%|#/)) {
17+
state.tokenize = inTag(ch);
18+
return "tag";
19+
}
20+
}
21+
}
22+
function inTag (close) {
23+
if (close == "{") {
24+
close = "}";
25+
}
26+
return function (stream, state) {
27+
var ch = stream.next();
28+
if ((ch == close) && stream.eat("}")) {
29+
state.tokenize = tokenBase;
30+
return "tag";
31+
}
32+
if (stream.match(keywords)) {
33+
return "keyword";
34+
}
35+
return close == "#" ? "comment" : "string";
36+
};
37+
}
38+
return {
39+
startState: function () {
40+
return {tokenize: tokenBase};
41+
},
42+
token: function (stream, state) {
43+
return state.tokenize(stream, state);
44+
}
45+
};
46+
});
47+
48+
CodeMirror.defineMode("django", function(config) {
49+
var htmlBase = CodeMirror.getMode(config, "text/html");
50+
var djangoInner = CodeMirror.getMode(config, "django:inner");
51+
return CodeMirror.overlayMode(htmlBase, djangoInner);
52+
});
53+
54+
CodeMirror.defineMIME("text/x-django", "django");
55+
})(CodeMirror);

mode/django/index.html

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<!doctype html>
2+
3+
<title>CodeMirror: Django template mode</title>
4+
<meta charset="utf-8"/>
5+
<link rel=stylesheet href="../../doc/docs.css">
6+
7+
<link rel="stylesheet" href="../../lib/codemirror.css">
8+
<script src="../../lib/codemirror.js"></script>
9+
<script src="../../addon/mode/overlay.js"></script>
10+
<script src="../xml/xml.js"></script>
11+
<script src="../htmlmixed/htmlmixed.js"></script>
12+
<script src="django.js"></script>
13+
<style type="text/css">.CodeMirror {border-top: 1px solid black; border-bottom: 1px solid black;}</style>
14+
<div id=nav>
15+
<a href="http://codemirror.net"><img id=logo src="../../doc/logo.png"></a>
16+
17+
<ul>
18+
<li><a href="../../index.html">Home</a>
19+
<li><a href="../../doc/manual.html">Manual</a>
20+
<li><a href="https://github.com/marijnh/codemirror">Code</a>
21+
</ul>
22+
<ul>
23+
<li><a href="../index.html">Language modes</a>
24+
<li><a class=active href="#">Django</a>
25+
</ul>
26+
</div>
27+
28+
<article>
29+
<h2>Django template mode</h2>
30+
<form><textarea id="code" name="code">
31+
<!doctype html>
32+
<html>
33+
<head>
34+
<title>My Django web application</title>
35+
</head>
36+
<body>
37+
<h1>
38+
{{ page.title }}
39+
</h1>
40+
<ul class="my-list">
41+
{% for item in items %}
42+
<li>{% item.name %}</li>
43+
{% empty %}
44+
<li>You have no items in your list.</li>
45+
{% endfor %}
46+
</ul>
47+
</body>
48+
</html>
49+
</textarea></form>
50+
51+
<script>
52+
var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
53+
lineNumbers: true,
54+
mode: "django",
55+
indentUnit: 4,
56+
indentWithTabs: true
57+
});
58+
</script>
59+
60+
<p>Mode for HTML with embedded Django template markup.</p>
61+
62+
<p><strong>MIME types defined:</strong> <code>text/x-django</code></p>
63+
</article>

mode/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ <h2>Language modes</h2>
4040
<li><a href="css/index.html">CSS</a></li>
4141
<li><a href="python/index.html">Cython</a></li>
4242
<li><a href="d/index.html">D</a></li>
43+
<li><a href="django/index.html">Django</a> (templating language)</li>
4344
<li><a href="diff/index.html">diff</a></li>
4445
<li><a href="dtd/index.html">DTD</a></li>
4546
<li><a href="dylan/index.html">Dylan</a></li>

0 commit comments

Comments
 (0)