12
if (typeof exports == "object" && typeof module == "object")
13
mod(require("../../lib/codemirror"));
14
else if (typeof define == "function" && define.amd)
15
define(["../../lib/codemirror"], mod);
18
})(function(CodeMirror) {
21
CodeMirror.defineMode("dtd", function(config) {
22
var indentUnit = config.indentUnit, type;
23
function ret(style, tp) {type = tp; return style;}
25
function tokenBase(stream, state) {
26
var ch = stream.next();
28
if (ch == "<" && stream.eat("!") ) {
29
if (stream.eatWhile(/[\-]/)) {
30
state.tokenize = tokenSGMLComment;
31
return tokenSGMLComment(stream, state);
32
} else if (stream.eatWhile(/[\w]/)) return ret("keyword", "doindent");
33
} else if (ch == "<" && stream.eat("?")) {
34
state.tokenize = inBlock("meta", "?>");
35
return ret("meta", ch);
36
} else if (ch == "#" && stream.eatWhile(/[\w]/)) return ret("atom", "tag");
37
else if (ch == "|") return ret("keyword", "separator");
38
else if (ch.match(/[\(\)\[\]\-\.,\+\?>]/)) return ret(null, ch);
39
else if (ch.match(/[\[\]]/)) return ret("rule", ch);
40
else if (ch == "\"" || ch == "'") {
41
state.tokenize = tokenString(ch);
42
return state.tokenize(stream, state);
43
} else if (stream.eatWhile(/[a-zA-Z\?\+\d]/)) {
44
var sc = stream.current();
45
if( sc.substr(sc.length-1,sc.length).match(/\?|\+/) !== null )stream.backUp(1);
46
return ret("tag", "tag");
47
} else if (ch == "%" || ch == "*" ) return ret("number", "number");
49
stream.eatWhile(/[\w\\\-_%.{,]/);
50
return ret(null, null);
54
function tokenSGMLComment(stream, state) {
56
while ((ch = stream.next()) != null) {
57
if (dashes >= 2 && ch == ">") {
58
state.tokenize = tokenBase;
61
dashes = (ch == "-") ? dashes + 1 : 0;
63
return ret("comment", "comment");
66
function tokenString(quote) {
67
return function(stream, state) {
68
var escaped = false, ch;
69
while ((ch = stream.next()) != null) {
70
if (ch == quote && !escaped) {
71
state.tokenize = tokenBase;
74
escaped = !escaped && ch == "\\";
76
return ret("string", "tag");
80
function inBlock(style, terminator) {
81
return function(stream, state) {
82
while (!stream.eol()) {
83
if (stream.match(terminator)) {
84
state.tokenize = tokenBase;
94
startState: function(base) {
95
return {tokenize: tokenBase,
96
baseIndent: base || 0,
100
token: function(stream, state) {
101
if (stream.eatSpace()) return null;
102
var style = state.tokenize(stream, state);
104
var context = state.stack[state.stack.length-1];
105
if (stream.current() == "[" || type === "doindent" || type == "[") state.stack.push("rule");
106
else if (type === "endtag") state.stack[state.stack.length-1] = "endtag";
107
else if (stream.current() == "]" || type == "]" || (type == ">" && context == "rule")) state.stack.pop();
108
else if (type == "[") state.stack.push("[");
112
indent: function(state, textAfter) {
113
var n = state.stack.length;
115
if( textAfter.charAt(0) === ']' )n--;
116
else if(textAfter.substr(textAfter.length-1, textAfter.length) === ">"){
117
if(textAfter.substr(0,1) === "<") {}
118
else if( type == "doindent" && textAfter.length > 1 ) {}
119
else if( type == "doindent")n--;
120
else if( type == ">" && textAfter.length > 1) {}
121
else if( type == "tag" && textAfter !== ">") {}
122
else if( type == "tag" && state.stack[state.stack.length-1] == "rule")n--;
123
else if( type == "tag")n++;
124
else if( textAfter === ">" && state.stack[state.stack.length-1] == "rule" && type === ">")n--;
125
else if( textAfter === ">" && state.stack[state.stack.length-1] == "rule") {}
126
else if( textAfter.substr(0,1) !== "<" && textAfter.substr(0,1) === ">" )n=n-1;
127
else if( textAfter === ">") {}
130
if(type == null || type == "]")n--;
133
return state.baseIndent + n * indentUnit;
140
CodeMirror.defineMIME("application/xml-dtd", "dtd");