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.

haml.js 5.2KB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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("../htmlmixed/htmlmixed"), require("../ruby/ruby"));
  6. else if (typeof define == "function" && define.amd) // AMD
  7. define(["../../lib/codemirror", "../htmlmixed/htmlmixed", "../ruby/ruby"], mod);
  8. else // Plain browser env
  9. mod(CodeMirror);
  10. })(function(CodeMirror) {
  11. "use strict";
  12. // full haml mode. This handled embedded ruby and html fragments too
  13. CodeMirror.defineMode("haml", function(config) {
  14. var htmlMode = CodeMirror.getMode(config, {name: "htmlmixed"});
  15. var rubyMode = CodeMirror.getMode(config, "ruby");
  16. function rubyInQuote(endQuote) {
  17. return function(stream, state) {
  18. var ch = stream.peek();
  19. if (ch == endQuote && state.rubyState.tokenize.length == 1) {
  20. // step out of ruby context as it seems to complete processing all the braces
  21. stream.next();
  22. state.tokenize = html;
  23. return "closeAttributeTag";
  24. } else {
  25. return ruby(stream, state);
  26. }
  27. };
  28. }
  29. function ruby(stream, state) {
  30. if (stream.match("-#")) {
  31. stream.skipToEnd();
  32. return "comment";
  33. }
  34. return rubyMode.token(stream, state.rubyState);
  35. }
  36. function html(stream, state) {
  37. var ch = stream.peek();
  38. // handle haml declarations. All declarations that cant be handled here
  39. // will be passed to html mode
  40. if (state.previousToken.style == "comment" ) {
  41. if (state.indented > state.previousToken.indented) {
  42. stream.skipToEnd();
  43. return "commentLine";
  44. }
  45. }
  46. if (state.startOfLine) {
  47. if (ch == "!" && stream.match("!!")) {
  48. stream.skipToEnd();
  49. return "tag";
  50. } else if (stream.match(/^%[\w:#\.]+=/)) {
  51. state.tokenize = ruby;
  52. return "hamlTag";
  53. } else if (stream.match(/^%[\w:]+/)) {
  54. return "hamlTag";
  55. } else if (ch == "/" ) {
  56. stream.skipToEnd();
  57. return "comment";
  58. }
  59. }
  60. if (state.startOfLine || state.previousToken.style == "hamlTag") {
  61. if ( ch == "#" || ch == ".") {
  62. stream.match(/[\w-#\.]*/);
  63. return "hamlAttribute";
  64. }
  65. }
  66. // do not handle --> as valid ruby, make it HTML close comment instead
  67. if (state.startOfLine && !stream.match("-->", false) && (ch == "=" || ch == "-" )) {
  68. state.tokenize = ruby;
  69. return state.tokenize(stream, state);
  70. }
  71. if (state.previousToken.style == "hamlTag" ||
  72. state.previousToken.style == "closeAttributeTag" ||
  73. state.previousToken.style == "hamlAttribute") {
  74. if (ch == "(") {
  75. state.tokenize = rubyInQuote(")");
  76. return state.tokenize(stream, state);
  77. } else if (ch == "{") {
  78. if (!stream.match(/^\{%.*/)) {
  79. state.tokenize = rubyInQuote("}");
  80. return state.tokenize(stream, state);
  81. }
  82. }
  83. }
  84. return htmlMode.token(stream, state.htmlState);
  85. }
  86. return {
  87. // default to html mode
  88. startState: function() {
  89. var htmlState = CodeMirror.startState(htmlMode);
  90. var rubyState = CodeMirror.startState(rubyMode);
  91. return {
  92. htmlState: htmlState,
  93. rubyState: rubyState,
  94. indented: 0,
  95. previousToken: { style: null, indented: 0},
  96. tokenize: html
  97. };
  98. },
  99. copyState: function(state) {
  100. return {
  101. htmlState : CodeMirror.copyState(htmlMode, state.htmlState),
  102. rubyState: CodeMirror.copyState(rubyMode, state.rubyState),
  103. indented: state.indented,
  104. previousToken: state.previousToken,
  105. tokenize: state.tokenize
  106. };
  107. },
  108. token: function(stream, state) {
  109. if (stream.sol()) {
  110. state.indented = stream.indentation();
  111. state.startOfLine = true;
  112. }
  113. if (stream.eatSpace()) return null;
  114. var style = state.tokenize(stream, state);
  115. state.startOfLine = false;
  116. // dont record comment line as we only want to measure comment line with
  117. // the opening comment block
  118. if (style && style != "commentLine") {
  119. state.previousToken = { style: style, indented: state.indented };
  120. }
  121. // if current state is ruby and the previous token is not `,` reset the
  122. // tokenize to html
  123. if (stream.eol() && state.tokenize == ruby) {
  124. stream.backUp(1);
  125. var ch = stream.peek();
  126. stream.next();
  127. if (ch && ch != ",") {
  128. state.tokenize = html;
  129. }
  130. }
  131. // reprocess some of the specific style tag when finish setting previousToken
  132. if (style == "hamlTag") {
  133. style = "tag";
  134. } else if (style == "commentLine") {
  135. style = "comment";
  136. } else if (style == "hamlAttribute") {
  137. style = "attribute";
  138. } else if (style == "closeAttributeTag") {
  139. style = null;
  140. }
  141. return style;
  142. }
  143. };
  144. }, "htmlmixed", "ruby");
  145. CodeMirror.defineMIME("text/x-haml", "haml");
  146. });