dtd.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. CodeMirror.defineMode("dtd", function(config) {
  8. var indentUnit = config.indentUnit, type;
  9. function ret(style, tp) {type = tp; return style;}
  10. function tokenBase(stream, state) {
  11. var ch = stream.next();
  12. if (ch == "<" && stream.eat("!") ) {
  13. if (stream.eatWhile(/[\-]/)) {
  14. state.tokenize = tokenSGMLComment;
  15. return tokenSGMLComment(stream, state);
  16. } else if (stream.eatWhile(/[\w]/)) return ret("keyword", "doindent");
  17. } else if (ch == "<" && stream.eat("?")) { //xml declaration
  18. state.tokenize = inBlock("meta", "?>");
  19. return ret("meta", ch);
  20. } else if (ch == "#" && stream.eatWhile(/[\w]/)) return ret("atom", "tag");
  21. else if (ch == "|") return ret("keyword", "seperator");
  22. else if (ch.match(/[\(\)\[\]\-\.,\+\?>]/)) return ret(null, ch);//if(ch === ">") return ret(null, "endtag"); else
  23. else if (ch.match(/[\[\]]/)) return ret("rule", ch);
  24. else if (ch == "\"" || ch == "'") {
  25. state.tokenize = tokenString(ch);
  26. return state.tokenize(stream, state);
  27. } else if (stream.eatWhile(/[a-zA-Z\?\+\d]/)) {
  28. var sc = stream.current();
  29. if( sc.substr(sc.length-1,sc.length).match(/\?|\+/) !== null )stream.backUp(1);
  30. return ret("tag", "tag");
  31. } else if (ch == "%" || ch == "*" ) return ret("number", "number");
  32. else {
  33. stream.eatWhile(/[\w\\\-_%.{,]/);
  34. return ret(null, null);
  35. }
  36. }
  37. function tokenSGMLComment(stream, state) {
  38. var dashes = 0, ch;
  39. while ((ch = stream.next()) != null) {
  40. if (dashes >= 2 && ch == ">") {
  41. state.tokenize = tokenBase;
  42. break;
  43. }
  44. dashes = (ch == "-") ? dashes + 1 : 0;
  45. }
  46. return ret("comment", "comment");
  47. }
  48. function tokenString(quote) {
  49. return function(stream, state) {
  50. var escaped = false, ch;
  51. while ((ch = stream.next()) != null) {
  52. if (ch == quote && !escaped) {
  53. state.tokenize = tokenBase;
  54. break;
  55. }
  56. escaped = !escaped && ch == "\\";
  57. }
  58. return ret("string", "tag");
  59. };
  60. }
  61. function inBlock(style, terminator) {
  62. return function(stream, state) {
  63. while (!stream.eol()) {
  64. if (stream.match(terminator)) {
  65. state.tokenize = tokenBase;
  66. break;
  67. }
  68. stream.next();
  69. }
  70. return style;
  71. };
  72. }
  73. return {
  74. startState: function(base) {
  75. return {tokenize: tokenBase,
  76. baseIndent: base || 0,
  77. stack: []};
  78. },
  79. token: function(stream, state) {
  80. if (stream.eatSpace()) return null;
  81. var style = state.tokenize(stream, state);
  82. var context = state.stack[state.stack.length-1];
  83. if (stream.current() == "[" || type === "doindent" || type == "[") state.stack.push("rule");
  84. else if (type === "endtag") state.stack[state.stack.length-1] = "endtag";
  85. else if (stream.current() == "]" || type == "]" || (type == ">" && context == "rule")) state.stack.pop();
  86. else if (type == "[") state.stack.push("[");
  87. return style;
  88. },
  89. indent: function(state, textAfter) {
  90. var n = state.stack.length;
  91. if( textAfter.match(/\]\s+|\]/) )n=n-1;
  92. else if(textAfter.substr(textAfter.length-1, textAfter.length) === ">"){
  93. if(textAfter.substr(0,1) === "<")n;
  94. else if( type == "doindent" && textAfter.length > 1 )n;
  95. else if( type == "doindent")n--;
  96. else if( type == ">" && textAfter.length > 1)n;
  97. else if( type == "tag" && textAfter !== ">")n;
  98. else if( type == "tag" && state.stack[state.stack.length-1] == "rule")n--;
  99. else if( type == "tag")n++;
  100. else if( textAfter === ">" && state.stack[state.stack.length-1] == "rule" && type === ">")n--;
  101. else if( textAfter === ">" && state.stack[state.stack.length-1] == "rule")n;
  102. else if( textAfter.substr(0,1) !== "<" && textAfter.substr(0,1) === ">" )n=n-1;
  103. else if( textAfter === ">")n;
  104. else n=n-1;
  105. //over rule them all
  106. if(type == null || type == "]")n--;
  107. }
  108. return state.baseIndent + n * indentUnit;
  109. },
  110. electricChars: "]>"
  111. };
  112. });
  113. CodeMirror.defineMIME("application/xml-dtd", "dtd");