1
0

jinja2.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. CodeMirror.defineMode("jinja2", function() {
  2. var keywords = ["block", "endblock", "for", "endfor", "in", "true", "false",
  3. "loop", "none", "self", "super", "if", "as", "not", "and",
  4. "else", "import", "with", "without", "context"];
  5. keywords = new RegExp("^((" + keywords.join(")|(") + "))\\b");
  6. function tokenBase (stream, state) {
  7. var ch = stream.next();
  8. if (ch == "{") {
  9. if (ch = stream.eat(/\{|%|#/)) {
  10. stream.eat("-");
  11. state.tokenize = inTag(ch);
  12. return "tag";
  13. }
  14. }
  15. }
  16. function inTag (close) {
  17. if (close == "{") {
  18. close = "}";
  19. }
  20. return function (stream, state) {
  21. var ch = stream.next();
  22. if ((ch == close || (ch == "-" && stream.eat(close)))
  23. && stream.eat("}")) {
  24. state.tokenize = tokenBase;
  25. return "tag";
  26. }
  27. if (stream.match(keywords)) {
  28. return "keyword";
  29. }
  30. return close == "#" ? "comment" : "string";
  31. };
  32. }
  33. return {
  34. startState: function () {
  35. return {tokenize: tokenBase};
  36. },
  37. token: function (stream, state) {
  38. return state.tokenize(stream, state);
  39. }
  40. };
  41. });