jinja2.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. (function(mod) {
  2. if (typeof exports == "object" && typeof module == "object") // CommonJS
  3. mod(require("../../lib/codemirror"));
  4. else if (typeof define == "function" && define.amd) // AMD
  5. define(["../../lib/codemirror"], mod);
  6. else // Plain browser env
  7. mod(CodeMirror);
  8. })(function(CodeMirror) {
  9. "use strict";
  10. CodeMirror.defineMode("jinja2", function() {
  11. var keywords = ["and", "as", "block", "endblock", "by", "cycle", "debug", "else", "elif",
  12. "extends", "filter", "endfilter", "firstof", "for",
  13. "endfor", "if", "endif", "ifchanged", "endifchanged",
  14. "ifequal", "endifequal", "ifnotequal",
  15. "endifnotequal", "in", "include", "load", "not", "now", "or",
  16. "parsed", "regroup", "reversed", "spaceless",
  17. "endspaceless", "ssi", "templatetag", "openblock",
  18. "closeblock", "openvariable", "closevariable",
  19. "openbrace", "closebrace", "opencomment",
  20. "closecomment", "widthratio", "url", "with", "endwith",
  21. "get_current_language", "trans", "noop", "blocktrans",
  22. "endblocktrans", "get_available_languages",
  23. "get_current_language_bidi", "plural"];
  24. keywords = new RegExp("^((" + keywords.join(")|(") + "))\\b");
  25. function tokenBase (stream, state) {
  26. var ch = stream.next();
  27. if (ch == "{") {
  28. if (ch = stream.eat(/\{|%|#/)) {
  29. stream.eat("-");
  30. state.tokenize = inTag(ch);
  31. return "tag";
  32. }
  33. }
  34. }
  35. function inTag (close) {
  36. if (close == "{") {
  37. close = "}";
  38. }
  39. return function (stream, state) {
  40. var ch = stream.next();
  41. if ((ch == close || (ch == "-" && stream.eat(close)))
  42. && stream.eat("}")) {
  43. state.tokenize = tokenBase;
  44. return "tag";
  45. }
  46. if (stream.match(keywords)) {
  47. return "keyword";
  48. }
  49. return close == "#" ? "comment" : "string";
  50. };
  51. }
  52. return {
  53. startState: function () {
  54. return {tokenize: tokenBase};
  55. },
  56. token: function (stream, state) {
  57. return state.tokenize(stream, state);
  58. }
  59. };
  60. });
  61. });