-
-
Notifications
You must be signed in to change notification settings - Fork 5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement mode for Django templating language #2402
Closed
Closed
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e298eeb
Added django.js
parisk 777e222
Fixed indentation and added CommonJS/AMD support
parisk 9129b7d
Added index.html
parisk 4ca408b
Added comment tags in Django mode
parisk 1dd9953
Fixed indentation for textarea contents
parisk d3ad51f
Added empty keyword
parisk 27a1e1e
Listed mode in mode/index.html
parisk da50b3d
Removed parenthesis content from link
parisk 72e3c92
Added character eating while not curly brance
parisk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
(function(mod) { | ||
if (typeof exports == "object" && typeof module == "object") // CommonJS | ||
mod(require("../../lib/codemirror"), require("../htmlmixed/htmlmixed"), | ||
require("../../addon/mode/overlay")); | ||
else if (typeof define == "function" && define.amd) // AMD | ||
define(["../../lib/codemirror", "../htmlmixed/htmlmixed", | ||
"../../addon/mode/overlay"], mod); | ||
else // Plain browser env | ||
mod(CodeMirror); | ||
})(function(CodeMirror) { | ||
"use strict"; | ||
|
||
CodeMirror.defineMode("django:inner", function() { | ||
var keywords = ["block", "endblock", "for", "endfor", "in", "true", "false", | ||
"loop", "none", "self", "super", "if", "endif", "as", "not", "and", | ||
"else", "import", "with", "endwith", "without", "context", "ifequal", "endifequal", | ||
"ifnotequal", "endifnotequal", "extends", "include", "load", "length", "comment", | ||
"endcomment", "empty"]; | ||
keywords = new RegExp("^((" + keywords.join(")|(") + "))\\b"); | ||
|
||
function tokenBase (stream, state) { | ||
var ch = stream.next(); | ||
if (ch == "{") { | ||
if (ch = stream.eat(/\{|%|#/)) { | ||
state.tokenize = inTag(ch); | ||
return "tag"; | ||
} | ||
} | ||
} | ||
function inTag (close) { | ||
if (close == "{") { | ||
close = "}"; | ||
} | ||
return function (stream, state) { | ||
var ch = stream.next(); | ||
if ((ch == close) && stream.eat("}")) { | ||
state.tokenize = tokenBase; | ||
return "tag"; | ||
} | ||
if (stream.match(keywords)) { | ||
return "keyword"; | ||
} | ||
return close == "#" ? "comment" : "string"; | ||
}; | ||
} | ||
return { | ||
startState: function () { | ||
return {tokenize: tokenBase}; | ||
}, | ||
token: function (stream, state) { | ||
return state.tokenize(stream, state); | ||
} | ||
}; | ||
}); | ||
|
||
CodeMirror.defineMode("django", function(config) { | ||
var htmlBase = CodeMirror.getMode(config, "text/html"); | ||
var djangoInner = CodeMirror.getMode(config, "django:inner"); | ||
return CodeMirror.overlayMode(htmlBase, djangoInner); | ||
}); | ||
|
||
CodeMirror.defineMIME("text/x-django", "django"); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<!doctype html> | ||
|
||
<title>CodeMirror: Django template mode</title> | ||
<meta charset="utf-8"/> | ||
<link rel=stylesheet href="../../doc/docs.css"> | ||
|
||
<link rel="stylesheet" href="../../lib/codemirror.css"> | ||
<script src="../../lib/codemirror.js"></script> | ||
<script src="../../addon/mode/overlay.js"></script> | ||
<script src="../xml/xml.js"></script> | ||
<script src="../htmlmixed/htmlmixed.js"></script> | ||
<script src="django.js"></script> | ||
<style type="text/css">.CodeMirror {border-top: 1px solid black; border-bottom: 1px solid black;}</style> | ||
<div id=nav> | ||
<a href="http://codemirror.net"><img id=logo src="../../doc/logo.png"></a> | ||
|
||
<ul> | ||
<li><a href="../../index.html">Home</a> | ||
<li><a href="../../doc/manual.html">Manual</a> | ||
<li><a href="https://github.com/marijnh/codemirror">Code</a> | ||
</ul> | ||
<ul> | ||
<li><a href="../index.html">Language modes</a> | ||
<li><a class=active href="#">Html Embedded Scripts</a> | ||
</ul> | ||
</div> | ||
|
||
<article> | ||
<h2>Django template mode</h2> | ||
<form><textarea id="code" name="code"> | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<title>My Django web application</title> | ||
</head> | ||
<body> | ||
<h1> | ||
{{ page.title }} | ||
</h1> | ||
<ul class="my-list"> | ||
{% for item in items %} | ||
<li>{% item.name %}</li> | ||
{% empty %} | ||
<li>You have no items in your list.</li> | ||
{% endfor %} | ||
</ul> | ||
</body> | ||
</html> | ||
</textarea></form> | ||
|
||
<script> | ||
var editor = CodeMirror.fromTextArea(document.getElementById("code"), { | ||
lineNumbers: true, | ||
mode: "django", | ||
indentUnit: 4, | ||
indentWithTabs: true | ||
}); | ||
</script> | ||
|
||
<p>Mode for HTML with embedded Django template markup.</p> | ||
|
||
<p><strong>MIME types defined:</strong> <code>text/x-django</code></p> | ||
</article> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Try to make this consume stretches of non-{ chars in a single call, so that not every character becomes its own token.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool, could you please check if sourcelair@72e3c92#diff-4b334edf14b1bb6080b9c2b2f1cf600aR22 works for you?