1
0

haml.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: http://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 embeded 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. // donot 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. state.tokenize = rubyInQuote("}");
  79. return state.tokenize(stream, state);
  80. }
  81. }
  82. return htmlMode.token(stream, state.htmlState);
  83. }
  84. return {
  85. // default to html mode
  86. startState: function() {
  87. var htmlState = htmlMode.startState();
  88. var rubyState = rubyMode.startState();
  89. return {
  90. htmlState: htmlState,
  91. rubyState: rubyState,
  92. indented: 0,
  93. previousToken: { style: null, indented: 0},
  94. tokenize: html
  95. };
  96. },
  97. copyState: function(state) {
  98. return {
  99. htmlState : CodeMirror.copyState(htmlMode, state.htmlState),
  100. rubyState: CodeMirror.copyState(rubyMode, state.rubyState),
  101. indented: state.indented,
  102. previousToken: state.previousToken,
  103. tokenize: state.tokenize
  104. };
  105. },
  106. token: function(stream, state) {
  107. if (stream.sol()) {
  108. state.indented = stream.indentation();
  109. state.startOfLine = true;
  110. }
  111. if (stream.eatSpace()) return null;
  112. var style = state.tokenize(stream, state);
  113. state.startOfLine = false;
  114. // dont record comment line as we only want to measure comment line with
  115. // the opening comment block
  116. if (style && style != "commentLine") {
  117. state.previousToken = { style: style, indented: state.indented };
  118. }
  119. // if current state is ruby and the previous token is not `,` reset the
  120. // tokenize to html
  121. if (stream.eol() && state.tokenize == ruby) {
  122. stream.backUp(1);
  123. var ch = stream.peek();
  124. stream.next();
  125. if (ch && ch != ",") {
  126. state.tokenize = html;
  127. }
  128. }
  129. // reprocess some of the specific style tag when finish setting previousToken
  130. if (style == "hamlTag") {
  131. style = "tag";
  132. } else if (style == "commentLine") {
  133. style = "comment";
  134. } else if (style == "hamlAttribute") {
  135. style = "attribute";
  136. } else if (style == "closeAttributeTag") {
  137. style = null;
  138. }
  139. return style;
  140. }
  141. };
  142. }, "htmlmixed", "ruby");
  143. CodeMirror.defineMIME("text/x-haml", "haml");
  144. });