You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

193 lines
5.8 KiB

7 months ago
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/5/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", "do", "for",
  15. "endfor", "if", "endif", "ifchanged", "endifchanged",
  16. "ifequal", "endifequal", "ifnotequal", "set", "raw", "endraw",
  17. "endifnotequal", "in", "include", "load", "not", "now", "or",
  18. "parsed", "regroup", "reversed", "spaceless", "call", "endcall", "macro",
  19. "endmacro", "endspaceless", "ssi", "templatetag", "openblock",
  20. "closeblock", "openvariable", "closevariable", "without", "context",
  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", "pluralize", "autoescape", "endautoescape"],
  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. }
  76. else if (state.inbraces > 0 && ch ==")") {
  77. stream.next()
  78. state.inbraces--;
  79. }
  80. else if (ch == "(") {
  81. stream.next()
  82. state.inbraces++;
  83. }
  84. else if (state.inbrackets > 0 && ch =="]") {
  85. stream.next()
  86. state.inbrackets--;
  87. }
  88. else if (ch == "[") {
  89. stream.next()
  90. state.inbrackets++;
  91. }
  92. else if (!state.lineTag && (stream.match(state.intag + "}") || stream.eat("-") && stream.match(state.intag + "}"))) {
  93. state.intag = false;
  94. return "tag";
  95. } else if(stream.match(operator)) {
  96. state.operator = true;
  97. return "operator";
  98. } else if(stream.match(sign)) {
  99. state.sign = true;
  100. } else {
  101. if (stream.column() == 1 && state.lineTag && stream.match(keywords)) {
  102. //allow nospace after tag before the keyword
  103. return "keyword";
  104. }
  105. if(stream.eat(" ") || stream.sol()) {
  106. if(stream.match(keywords)) {
  107. return "keyword";
  108. }
  109. if(stream.match(atom)) {
  110. return "atom";
  111. }
  112. if(stream.match(number)) {
  113. return "number";
  114. }
  115. if(stream.sol()) {
  116. stream.next();
  117. }
  118. } else {
  119. stream.next();
  120. }
  121. }
  122. return "variable";
  123. } else if (stream.eat("{")) {
  124. if (stream.eat("#")) {
  125. state.incomment = true;
  126. if(!stream.skipTo("#}")) {
  127. stream.skipToEnd();
  128. } else {
  129. stream.eatWhile(/\#|}/);
  130. state.incomment = false;
  131. }
  132. return "comment";
  133. //Open tag
  134. } else if (ch = stream.eat(/\{|%/)) {
  135. //Cache close tag
  136. state.intag = ch;
  137. state.inbraces = 0;
  138. state.inbrackets = 0;
  139. if(ch == "{") {
  140. state.intag = "}";
  141. }
  142. stream.eat("-");
  143. return "tag";
  144. }
  145. //Line statements
  146. } else if (stream.eat('#')) {
  147. if (stream.peek() == '#') {
  148. stream.skipToEnd();
  149. return "comment"
  150. }
  151. else if (!stream.eol()) {
  152. state.intag = true;
  153. state.lineTag = true;
  154. state.inbraces = 0;
  155. state.inbrackets = 0;
  156. return "tag";
  157. }
  158. }
  159. stream.next();
  160. };
  161. return {
  162. startState: function () {
  163. return {
  164. tokenize: tokenBase,
  165. inbrackets:0,
  166. inbraces:0
  167. };
  168. },
  169. token: function(stream, state) {
  170. var style = state.tokenize(stream, state);
  171. if (stream.eol() && state.lineTag && !state.instring && state.inbraces == 0 && state.inbrackets == 0) {
  172. //Close line statement at the EOL
  173. state.intag = false
  174. state.lineTag = false
  175. }
  176. return style;
  177. },
  178. blockCommentStart: "{#",
  179. blockCommentEnd: "#}",
  180. lineComment: "##",
  181. };
  182. });
  183. CodeMirror.defineMIME("text/jinja2", "jinja2");
  184. });