jade.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. CodeMirror.defineMode("jade", function () {
  2. var symbol_regex1 = /^(?:~|!|%|\^|\*|\+|=|\\|:|;|,|\/|\?|&|<|>|\|)/;
  3. var open_paren_regex = /^(\(|\[)/;
  4. var close_paren_regex = /^(\)|\])/;
  5. var keyword_regex1 = /^(if|else|return|var|function|include|doctype|each)/;
  6. var keyword_regex2 = /^(#|{|}|\.)/;
  7. var keyword_regex3 = /^(in)/;
  8. var html_regex1 = /^(html|head|title|meta|link|script|body|br|div|input|span|a|img)/;
  9. var html_regex2 = /^(h1|h2|h3|h4|h5|p|strong|em)/;
  10. return {
  11. startState: function () {
  12. return {
  13. inString: false,
  14. stringType: "",
  15. beforeTag: true,
  16. justMatchedKeyword: false,
  17. afterParen: false
  18. };
  19. },
  20. token: function (stream, state) {
  21. //check for state changes
  22. if (!state.inString && ((stream.peek() == '"') || (stream.peek() == "'"))) {
  23. state.stringType = stream.peek();
  24. stream.next(); // Skip quote
  25. state.inString = true; // Update state
  26. }
  27. //return state
  28. if (state.inString) {
  29. if (stream.skipTo(state.stringType)) { // Quote found on this line
  30. stream.next(); // Skip quote
  31. state.inString = false; // Clear flag
  32. } else {
  33. stream.skipToEnd(); // Rest of line is string
  34. }
  35. state.justMatchedKeyword = false;
  36. return "string"; // Token style
  37. } else if (stream.sol() && stream.eatSpace()) {
  38. if (stream.match(keyword_regex1)) {
  39. state.justMatchedKeyword = true;
  40. stream.eatSpace();
  41. return "keyword";
  42. }
  43. if (stream.match(html_regex1) || stream.match(html_regex2)) {
  44. state.justMatchedKeyword = true;
  45. return "variable";
  46. }
  47. } else if (stream.sol() && stream.match(keyword_regex1)) {
  48. state.justMatchedKeyword = true;
  49. stream.eatSpace();
  50. return "keyword";
  51. } else if (stream.sol() && (stream.match(html_regex1) || stream.match(html_regex2))) {
  52. state.justMatchedKeyword = true;
  53. return "variable";
  54. } else if (stream.eatSpace()) {
  55. state.justMatchedKeyword = false;
  56. if (stream.match(keyword_regex3) && stream.eatSpace()) {
  57. state.justMatchedKeyword = true;
  58. return "keyword";
  59. }
  60. } else if (stream.match(symbol_regex1)) {
  61. state.justMatchedKeyword = false;
  62. return "atom";
  63. } else if (stream.match(open_paren_regex)) {
  64. state.afterParen = true;
  65. state.justMatchedKeyword = true;
  66. return "def";
  67. } else if (stream.match(close_paren_regex)) {
  68. state.afterParen = false;
  69. state.justMatchedKeyword = true;
  70. return "def";
  71. } else if (stream.match(keyword_regex2)) {
  72. state.justMatchedKeyword = true;
  73. return "keyword";
  74. } else if (stream.eatSpace()) {
  75. state.justMatchedKeyword = false;
  76. } else {
  77. stream.next();
  78. if (state.justMatchedKeyword) {
  79. return "property";
  80. } else if (state.afterParen) {
  81. return "property";
  82. }
  83. }
  84. return null;
  85. }
  86. };
  87. });
  88. CodeMirror.defineMIME('text/x-jade', 'jade');