本站源代码
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.

142 lines
4.5KB

  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"), require("../../addon/mode/multiplex"));
  6. else if (typeof define == "function" && define.amd) // AMD
  7. define(["../../lib/codemirror", "../../addon/mode/multiplex"], mod);
  8. else // Plain browser env
  9. mod(CodeMirror);
  10. })(function(CodeMirror) {
  11. "use strict";
  12. CodeMirror.defineMode("twig:inner", function() {
  13. var keywords = ["and", "as", "autoescape", "endautoescape", "block", "do", "endblock", "else", "elseif", "extends", "for", "endfor", "embed", "endembed", "filter", "endfilter", "flush", "from", "if", "endif", "in", "is", "include", "import", "not", "or", "set", "spaceless", "endspaceless", "with", "endwith", "trans", "endtrans", "blocktrans", "endblocktrans", "macro", "endmacro", "use", "verbatim", "endverbatim"],
  14. operator = /^[+\-*&%=<>!?|~^]/,
  15. sign = /^[:\[\(\{]/,
  16. atom = ["true", "false", "null", "empty", "defined", "divisibleby", "divisible by", "even", "odd", "iterable", "sameas", "same as"],
  17. number = /^(\d[+\-\*\/])?\d+(\.\d+)?/;
  18. keywords = new RegExp("((" + keywords.join(")|(") + "))\\b");
  19. atom = new RegExp("((" + atom.join(")|(") + "))\\b");
  20. function tokenBase (stream, state) {
  21. var ch = stream.peek();
  22. //Comment
  23. if (state.incomment) {
  24. if (!stream.skipTo("#}")) {
  25. stream.skipToEnd();
  26. } else {
  27. stream.eatWhile(/\#|}/);
  28. state.incomment = false;
  29. }
  30. return "comment";
  31. //Tag
  32. } else if (state.intag) {
  33. //After operator
  34. if (state.operator) {
  35. state.operator = false;
  36. if (stream.match(atom)) {
  37. return "atom";
  38. }
  39. if (stream.match(number)) {
  40. return "number";
  41. }
  42. }
  43. //After sign
  44. if (state.sign) {
  45. state.sign = false;
  46. if (stream.match(atom)) {
  47. return "atom";
  48. }
  49. if (stream.match(number)) {
  50. return "number";
  51. }
  52. }
  53. if (state.instring) {
  54. if (ch == state.instring) {
  55. state.instring = false;
  56. }
  57. stream.next();
  58. return "string";
  59. } else if (ch == "'" || ch == '"') {
  60. state.instring = ch;
  61. stream.next();
  62. return "string";
  63. } else if (stream.match(state.intag + "}") || stream.eat("-") && stream.match(state.intag + "}")) {
  64. state.intag = false;
  65. return "tag";
  66. } else if (stream.match(operator)) {
  67. state.operator = true;
  68. return "operator";
  69. } else if (stream.match(sign)) {
  70. state.sign = true;
  71. } else {
  72. if (stream.eat(" ") || stream.sol()) {
  73. if (stream.match(keywords)) {
  74. return "keyword";
  75. }
  76. if (stream.match(atom)) {
  77. return "atom";
  78. }
  79. if (stream.match(number)) {
  80. return "number";
  81. }
  82. if (stream.sol()) {
  83. stream.next();
  84. }
  85. } else {
  86. stream.next();
  87. }
  88. }
  89. return "variable";
  90. } else if (stream.eat("{")) {
  91. if (stream.eat("#")) {
  92. state.incomment = true;
  93. if (!stream.skipTo("#}")) {
  94. stream.skipToEnd();
  95. } else {
  96. stream.eatWhile(/\#|}/);
  97. state.incomment = false;
  98. }
  99. return "comment";
  100. //Open tag
  101. } else if (ch = stream.eat(/\{|%/)) {
  102. //Cache close tag
  103. state.intag = ch;
  104. if (ch == "{") {
  105. state.intag = "}";
  106. }
  107. stream.eat("-");
  108. return "tag";
  109. }
  110. }
  111. stream.next();
  112. };
  113. return {
  114. startState: function () {
  115. return {};
  116. },
  117. token: function (stream, state) {
  118. return tokenBase(stream, state);
  119. }
  120. };
  121. });
  122. CodeMirror.defineMode("twig", function(config, parserConfig) {
  123. var twigInner = CodeMirror.getMode(config, "twig:inner");
  124. if (!parserConfig || !parserConfig.base) return twigInner;
  125. return CodeMirror.multiplexingMode(
  126. CodeMirror.getMode(config, parserConfig.base), {
  127. open: /\{[{#%]/, close: /[}#%]\}/, mode: twigInner, parseDelimiters: true
  128. }
  129. );
  130. });
  131. CodeMirror.defineMIME("text/x-twig", "twig");
  132. });
上海开阖软件有限公司 沪ICP备12045867号-1