dtd.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. DTD mode
  3. Ported to CodeMirror by Peter Kroon <plakroon@gmail.com>
  4. Report bugs/issues here: https://github.com/marijnh/CodeMirror/issues
  5. GitHub: @peterkroon
  6. */
  7. (function(mod) {
  8. if (typeof exports == "object" && typeof module == "object") // CommonJS
  9. mod(require("../../lib/codemirror"));
  10. else if (typeof define == "function" && define.amd) // AMD
  11. define(["../../lib/codemirror"], mod);
  12. else // Plain browser env
  13. mod(CodeMirror);
  14. })(function(CodeMirror) {
  15. "use strict";
  16. CodeMirror.defineMode("dtd", function(config) {
  17. var indentUnit = config.indentUnit, type;
  18. function ret(style, tp) {type = tp; return style;}
  19. function tokenBase(stream, state) {
  20. var ch = stream.next();
  21. if (ch == "<" && stream.eat("!") ) {
  22. if (stream.eatWhile(/[\-]/)) {
  23. state.tokenize = tokenSGMLComment;
  24. return tokenSGMLComment(stream, state);
  25. } else if (stream.eatWhile(/[\w]/)) return ret("keyword", "doindent");
  26. } else if (ch == "<" && stream.eat("?")) { //xml declaration
  27. state.tokenize = inBlock("meta", "?>");
  28. return ret("meta", ch);
  29. } else if (ch == "#" && stream.eatWhile(/[\w]/)) return ret("atom", "tag");
  30. else if (ch == "|") return ret("keyword", "seperator");
  31. else if (ch.match(/[\(\)\[\]\-\.,\+\?>]/)) return ret(null, ch);//if(ch === ">") return ret(null, "endtag"); else
  32. else if (ch.match(/[\[\]]/)) return ret("rule", ch);
  33. else if (ch == "\"" || ch == "'") {
  34. state.tokenize = tokenString(ch);
  35. return state.tokenize(stream, state);
  36. } else if (stream.eatWhile(/[a-zA-Z\?\+\d]/)) {
  37. var sc = stream.current();
  38. if( sc.substr(sc.length-1,sc.length).match(/\?|\+/) !== null )stream.backUp(1);
  39. return ret("tag", "tag");
  40. } else if (ch == "%" || ch == "*" ) return ret("number", "number");
  41. else {
  42. stream.eatWhile(/[\w\\\-_%.{,]/);
  43. return ret(null, null);
  44. }
  45. }
  46. function tokenSGMLComment(stream, state) {
  47. var dashes = 0, ch;
  48. while ((ch = stream.next()) != null) {
  49. if (dashes >= 2 && ch == ">") {
  50. state.tokenize = tokenBase;
  51. break;
  52. }
  53. dashes = (ch == "-") ? dashes + 1 : 0;
  54. }
  55. return ret("comment", "comment");
  56. }
  57. function tokenString(quote) {
  58. return function(stream, state) {
  59. var escaped = false, ch;
  60. while ((ch = stream.next()) != null) {
  61. if (ch == quote && !escaped) {
  62. state.tokenize = tokenBase;
  63. break;
  64. }
  65. escaped = !escaped && ch == "\\";
  66. }
  67. return ret("string", "tag");
  68. };
  69. }
  70. function inBlock(style, terminator) {
  71. return function(stream, state) {
  72. while (!stream.eol()) {
  73. if (stream.match(terminator)) {
  74. state.tokenize = tokenBase;
  75. break;
  76. }
  77. stream.next();
  78. }
  79. return style;
  80. };
  81. }
  82. return {
  83. startState: function(base) {
  84. return {tokenize: tokenBase,
  85. baseIndent: base || 0,
  86. stack: []};
  87. },
  88. token: function(stream, state) {
  89. if (stream.eatSpace()) return null;
  90. var style = state.tokenize(stream, state);
  91. var context = state.stack[state.stack.length-1];
  92. if (stream.current() == "[" || type === "doindent" || type == "[") state.stack.push("rule");
  93. else if (type === "endtag") state.stack[state.stack.length-1] = "endtag";
  94. else if (stream.current() == "]" || type == "]" || (type == ">" && context == "rule")) state.stack.pop();
  95. else if (type == "[") state.stack.push("[");
  96. return style;
  97. },
  98. indent: function(state, textAfter) {
  99. var n = state.stack.length;
  100. if( textAfter.match(/\]\s+|\]/) )n=n-1;
  101. else if(textAfter.substr(textAfter.length-1, textAfter.length) === ">"){
  102. if(textAfter.substr(0,1) === "<")n;
  103. else if( type == "doindent" && textAfter.length > 1 )n;
  104. else if( type == "doindent")n--;
  105. else if( type == ">" && textAfter.length > 1)n;
  106. else if( type == "tag" && textAfter !== ">")n;
  107. else if( type == "tag" && state.stack[state.stack.length-1] == "rule")n--;
  108. else if( type == "tag")n++;
  109. else if( textAfter === ">" && state.stack[state.stack.length-1] == "rule" && type === ">")n--;
  110. else if( textAfter === ">" && state.stack[state.stack.length-1] == "rule")n;
  111. else if( textAfter.substr(0,1) !== "<" && textAfter.substr(0,1) === ">" )n=n-1;
  112. else if( textAfter === ">")n;
  113. else n=n-1;
  114. //over rule them all
  115. if(type == null || type == "]")n--;
  116. }
  117. return state.baseIndent + n * indentUnit;
  118. },
  119. electricChars: "]>"
  120. };
  121. });
  122. CodeMirror.defineMIME("application/xml-dtd", "dtd");
  123. });