django.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: http://codemirror.net/LICENSE
  3. (function(mod) {
  4. if (typeof exports == "object" && typeof module == "object") // CommonJS
  5. mod(require("../../lib/codemirror"), require("../htmlmixed/htmlmixed"),
  6. require("../../addon/mode/overlay"));
  7. else if (typeof define == "function" && define.amd) // AMD
  8. define(["../../lib/codemirror", "../htmlmixed/htmlmixed",
  9. "../../addon/mode/overlay"], mod);
  10. else // Plain browser env
  11. mod(CodeMirror);
  12. })(function(CodeMirror) {
  13. "use strict";
  14. CodeMirror.defineMode("django:inner", function() {
  15. var keywords = ["block", "endblock", "for", "endfor", "in", "true", "false",
  16. "loop", "none", "self", "super", "if", "endif", "as", "not", "and",
  17. "else", "import", "with", "endwith", "without", "context", "ifequal", "endifequal",
  18. "ifnotequal", "endifnotequal", "extends", "include", "load", "length", "comment",
  19. "endcomment", "empty"];
  20. keywords = new RegExp("^((" + keywords.join(")|(") + "))\\b");
  21. function tokenBase (stream, state) {
  22. stream.eatWhile(/[^\{]/);
  23. var ch = stream.next();
  24. if (ch == "{") {
  25. if (ch = stream.eat(/\{|%|#/)) {
  26. state.tokenize = inTag(ch);
  27. return "tag";
  28. }
  29. }
  30. }
  31. function inTag (close) {
  32. if (close == "{") {
  33. close = "}";
  34. }
  35. return function (stream, state) {
  36. var ch = stream.next();
  37. if ((ch == close) && stream.eat("}")) {
  38. state.tokenize = tokenBase;
  39. return "tag";
  40. }
  41. if (stream.match(keywords)) {
  42. return "keyword";
  43. }
  44. return close == "#" ? "comment" : "string";
  45. };
  46. }
  47. return {
  48. startState: function () {
  49. return {tokenize: tokenBase};
  50. },
  51. token: function (stream, state) {
  52. return state.tokenize(stream, state);
  53. }
  54. };
  55. });
  56. CodeMirror.defineMode("django", function(config) {
  57. var htmlBase = CodeMirror.getMode(config, "text/html");
  58. var djangoInner = CodeMirror.getMode(config, "django:inner");
  59. return CodeMirror.overlayMode(htmlBase, djangoInner);
  60. });
  61. CodeMirror.defineMIME("text/x-django", "django");
  62. });