jinja2.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/LICENSE
  3. (function(mod) {
  4. if (typeof exports == "object" && typeof module == "object") // CommonJS
  5. mod(require("../../lib/codemirror"));
  6. else if (typeof define == "function" && define.amd) // AMD
  7. define(["../../lib/codemirror"], mod);
  8. else // Plain browser env
  9. mod(CodeMirror);
  10. })(function(CodeMirror) {
  11. "use strict";
  12. CodeMirror.defineMode("jinja2", function() {
  13. var keywords = ["and", "as", "block", "endblock", "by", "cycle", "debug", "else", "elif",
  14. "extends", "filter", "endfilter", "firstof", "for",
  15. "endfor", "if", "endif", "ifchanged", "endifchanged",
  16. "ifequal", "endifequal", "ifnotequal",
  17. "endifnotequal", "in", "include", "load", "not", "now", "or",
  18. "parsed", "regroup", "reversed", "spaceless",
  19. "endspaceless", "ssi", "templatetag", "openblock",
  20. "closeblock", "openvariable", "closevariable",
  21. "openbrace", "closebrace", "opencomment",
  22. "closecomment", "widthratio", "url", "with", "endwith",
  23. "get_current_language", "trans", "endtrans", "noop", "blocktrans",
  24. "endblocktrans", "get_available_languages",
  25. "get_current_language_bidi", "plural"],
  26. operator = /^[+\-*&%=<>!?|~^]/,
  27. sign = /^[:\[\(\{]/,
  28. atom = ["true", "false"],
  29. number = /^(\d[+\-\*\/])?\d+(\.\d+)?/;
  30. keywords = new RegExp("((" + keywords.join(")|(") + "))\\b");
  31. atom = new RegExp("((" + atom.join(")|(") + "))\\b");
  32. function tokenBase (stream, state) {
  33. var ch = stream.peek();
  34. //Comment
  35. if (state.incomment) {
  36. if(!stream.skipTo("#}")) {
  37. stream.skipToEnd();
  38. } else {
  39. stream.eatWhile(/\#|}/);
  40. state.incomment = false;
  41. }
  42. return "comment";
  43. //Tag
  44. } else if (state.intag) {
  45. //After operator
  46. if(state.operator) {
  47. state.operator = false;
  48. if(stream.match(atom)) {
  49. return "atom";
  50. }
  51. if(stream.match(number)) {
  52. return "number";
  53. }
  54. }
  55. //After sign
  56. if(state.sign) {
  57. state.sign = false;
  58. if(stream.match(atom)) {
  59. return "atom";
  60. }
  61. if(stream.match(number)) {
  62. return "number";
  63. }
  64. }
  65. if(state.instring) {
  66. if(ch == state.instring) {
  67. state.instring = false;
  68. }
  69. stream.next();
  70. return "string";
  71. } else if(ch == "'" || ch == '"') {
  72. state.instring = ch;
  73. stream.next();
  74. return "string";
  75. } else if(stream.match(state.intag + "}") || stream.eat("-") && stream.match(state.intag + "}")) {
  76. state.intag = false;
  77. return "tag";
  78. } else if(stream.match(operator)) {
  79. state.operator = true;
  80. return "operator";
  81. } else if(stream.match(sign)) {
  82. state.sign = true;
  83. } else {
  84. if(stream.eat(" ") || stream.sol()) {
  85. if(stream.match(keywords)) {
  86. return "keyword";
  87. }
  88. if(stream.match(atom)) {
  89. return "atom";
  90. }
  91. if(stream.match(number)) {
  92. return "number";
  93. }
  94. if(stream.sol()) {
  95. stream.next();
  96. }
  97. } else {
  98. stream.next();
  99. }
  100. }
  101. return "variable";
  102. } else if (stream.eat("{")) {
  103. if (stream.eat("#")) {
  104. state.incomment = true;
  105. if(!stream.skipTo("#}")) {
  106. stream.skipToEnd();
  107. } else {
  108. stream.eatWhile(/\#|}/);
  109. state.incomment = false;
  110. }
  111. return "comment";
  112. //Open tag
  113. } else if (ch = stream.eat(/\{|%/)) {
  114. //Cache close tag
  115. state.intag = ch;
  116. if(ch == "{") {
  117. state.intag = "}";
  118. }
  119. stream.eat("-");
  120. return "tag";
  121. }
  122. }
  123. stream.next();
  124. };
  125. return {
  126. startState: function () {
  127. return {tokenize: tokenBase};
  128. },
  129. token: function (stream, state) {
  130. return state.tokenize(stream, state);
  131. },
  132. blockCommentStart: "{#",
  133. blockCommentEnd: "#}"
  134. };
  135. });
  136. CodeMirror.defineMIME("text/jinja2", "jinja2");
  137. });