Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[dtd mode] Add

  • Loading branch information...
commit 007434607dbd4faf5c7d4c99a836e989fe8b20f5 1 parent 7ea6f43
@peterkroon peterkroon authored marijnh committed
View
1  doc/compress.html
@@ -86,6 +86,7 @@
<option value="http://codemirror.net/mode/css/css.js">css.js</option>
<option value="http://codemirror.net/mode/d/d.js">d.js</option>
<option value="http://codemirror.net/mode/diff/diff.js">diff.js</option>
+ <option value="http://codemirror.net/mode/dtd/dtd.js">dtd.js</option>
<option value="http://codemirror.net/mode/ecl/ecl.js">ecl.js</option>
<option value="http://codemirror.net/mode/erlang/erlang.js">erlang.js</option>
<option value="http://codemirror.net/mode/gfm/gfm.js">gfm.js</option>
View
127 mode/dtd/dtd.js
@@ -0,0 +1,127 @@
+/*
+ DTD mode
+ Ported to CodeMirror by Peter Kroon <plakroon@gmail.com>
+ Report bugs/issues here: https://github.com/marijnh/CodeMirror/issues
+ GitHub: @peterkroon
+*/
+
+CodeMirror.defineMode("dtd", function(config) {
+ var indentUnit = config.indentUnit, type;
+ function ret(style, tp) {type = tp; return style;}
+
+ function tokenBase(stream, state) {
+ var ch = stream.next();
+
+ if (ch == "<" && stream.eat("!") ) {
+ if (stream.eatWhile(/[\-]/)) {
+ state.tokenize = tokenSGMLComment;
+ return tokenSGMLComment(stream, state);
+ } else if (stream.eatWhile(/[\w]/)) return ret("keyword", "doindent");
+ } else if (ch == "<" && stream.eat("?")) { //xml declaration
+ state.tokenize = inBlock("meta", "?>");
+ return ret("meta", ch);
+ } else if (ch == "#" && stream.eatWhile(/[\w]/)) return ret("atom", "tag");
+ else if (ch == "|") return ret("keyword", "seperator");
+ else if (ch.match(/[\(\)\[\]\-\.,\+\?>]/)) return ret(null, ch);//if(ch === ">") return ret(null, "endtag"); else
+ else if (ch.match(/[\[\]]/)) return ret("rule", ch);
+ else if (ch == "\"" || ch == "'") {
+ state.tokenize = tokenString(ch);
+ return state.tokenize(stream, state);
+ } else if (stream.eatWhile(/[a-zA-Z\?\+\d]/)) {
+ var sc = stream.current();
+ if( sc.substr(sc.length-1,sc.length).match(/\?|\+/) !== null )stream.backUp(1);
+ return ret("tag", "tag");
+ } else if (ch == "%" || ch == "*" ) return ret("number", "number");
+ else {
+ stream.eatWhile(/[\w\\\-_%.{,]/);
+ return ret(null, null);
+ }
+ }
+
+ function tokenSGMLComment(stream, state) {
+ var dashes = 0, ch;
+ while ((ch = stream.next()) != null) {
+ if (dashes >= 2 && ch == ">") {
+ state.tokenize = tokenBase;
+ break;
+ }
+ dashes = (ch == "-") ? dashes + 1 : 0;
+ }
+ return ret("comment", "comment");
+ }
+
+ function tokenString(quote) {
+ return function(stream, state) {
+ var escaped = false, ch;
+ while ((ch = stream.next()) != null) {
+ if (ch == quote && !escaped) {
+ state.tokenize = tokenBase;
+ break;
+ }
+ escaped = !escaped && ch == "\\";
+ }
+ return ret("string", "tag");
+ };
+ }
+
+ function inBlock(style, terminator) {
+ return function(stream, state) {
+ while (!stream.eol()) {
+ if (stream.match(terminator)) {
+ state.tokenize = tokenBase;
+ break;
+ }
+ stream.next();
+ }
+ return style;
+ };
+ }
+
+ return {
+ startState: function(base) {
+ return {tokenize: tokenBase,
+ baseIndent: base || 0,
+ stack: []};
+ },
+
+ token: function(stream, state) {
+ if (stream.eatSpace()) return null;
+ var style = state.tokenize(stream, state);
+
+ var context = state.stack[state.stack.length-1];
+ if (stream.current() == "[" || type === "doindent" || type == "[") state.stack.push("rule");
+ else if (type === "endtag") state.stack[state.stack.length-1] = "endtag";
+ else if (stream.current() == "]" || type == "]" || (type == ">" && context == "rule")) state.stack.pop();
+ else if (type == "[") state.stack.push("[");
+ return style;
+ },
+
+ indent: function(state, textAfter) {
+ var n = state.stack.length;
+
+ if( textAfter.match(/\]\s+|\]/) )n=n-1;
+ else if(textAfter.substr(textAfter.length-1, textAfter.length) === ">"){
+ if(textAfter.substr(0,1) === "<")n;
+ else if( type == "doindent" && textAfter.length > 1 )n;
+ else if( type == "doindent")n--;
+ else if( type == ">" && textAfter.length > 1)n;
+ else if( type == "tag" && textAfter !== ">")n;
+ else if( type == "tag" && state.stack[state.stack.length-1] == "rule")n--;
+ else if( type == "tag")n++;
+ else if( textAfter === ">" && state.stack[state.stack.length-1] == "rule" && type === ">")n--;
+ else if( textAfter === ">" && state.stack[state.stack.length-1] == "rule")n;
+ else if( textAfter.substr(0,1) !== "<" && textAfter.substr(0,1) === ">" )n=n-1;
+ else if( textAfter === ">")n;
+ else n=n-1;
+ //over rule them all
+ if(type == null || type == "]")n--;
+ }
+
+ return state.baseIndent + n * indentUnit;
+ },
+
+ electricChars: "]>"
+ };
+});
+
+CodeMirror.defineMIME("application/xml-dtd", "dtd");
View
89 mode/dtd/index.html
@@ -0,0 +1,89 @@
+<!doctype html>
+
+<title>CodeMirror: DTD mode</title>
+<meta charset="utf-8"/>
+<link rel=stylesheet href="../../doc/docs.css">
+
+<link rel="stylesheet" href="../../lib/codemirror.css">
+<script src="../../lib/codemirror.js"></script>
+<script src="dtd.js"></script>
+<style type="text/css">.CodeMirror {border-top: 1px solid black; border-bottom: 1px solid black;}</style>
+<div id=nav>
+ <a href="http://codemirror.net"><img id=logo src="../../doc/logo.png"></a>
+
+ <ul>
+ <li><a href="../../index.html">Home</a>
+ <li><a href="../../doc/manual.html">Manual</a>
+ <li><a href="https://github.com/marijnh/codemirror">Code</a>
+ </ul>
+ <ul>
+ <li><a href="../index.html">Language modes</a>
+ <li><a class=active href="#">DTD</a>
+ </ul>
+</div>
+
+<article>
+<h2>DTD mode</h2>
+<form><textarea id="code" name="code"><?xml version="1.0" encoding="UTF-8"?>
+
+<!ATTLIST title
+ xmlns CDATA #FIXED "http://docbook.org/ns/docbook"
+ role CDATA #IMPLIED
+ %db.common.attributes;
+ %db.common.linking.attributes;
+>
+
+<!--
+ Try: http://docbook.org/xml/5.0/dtd/docbook.dtd
+-->
+
+<!DOCTYPE xsl:stylesheet
+ [
+ <!ENTITY nbsp "&amp;#160;">
+ <!ENTITY copy "&amp;#169;">
+ <!ENTITY reg "&amp;#174;">
+ <!ENTITY trade "&amp;#8482;">
+ <!ENTITY mdash "&amp;#8212;">
+ <!ENTITY ldquo "&amp;#8220;">
+ <!ENTITY rdquo "&amp;#8221;">
+ <!ENTITY pound "&amp;#163;">
+ <!ENTITY yen "&amp;#165;">
+ <!ENTITY euro "&amp;#8364;">
+ <!ENTITY mathml "http://www.w3.org/1998/Math/MathML">
+ ]
+>
+
+<!ELEMENT title (#PCDATA|inlinemediaobject|remark|superscript|subscript|xref|link|olink|anchor|biblioref|alt|annotation|indexterm|abbrev|acronym|date|emphasis|footnote|footnoteref|foreignphrase|phrase|quote|wordasword|firstterm|glossterm|coref|trademark|productnumber|productname|database|application|hardware|citation|citerefentry|citetitle|citebiblioid|author|person|personname|org|orgname|editor|jobtitle|replaceable|package|parameter|termdef|nonterminal|systemitem|option|optional|property|inlineequation|tag|markup|token|symbol|literal|code|constant|email|uri|guiicon|guibutton|guimenuitem|guimenu|guisubmenu|guilabel|menuchoice|mousebutton|keycombo|keycap|keycode|keysym|shortcut|accel|prompt|envar|filename|command|computeroutput|userinput|function|varname|returnvalue|type|classname|exceptionname|interfacename|methodname|modifier|initializer|ooclass|ooexception|oointerface|errorcode|errortext|errorname|errortype)*>
+
+<!ENTITY % db.common.attributes "
+ xml:id ID #IMPLIED
+ version CDATA #IMPLIED
+ xml:lang CDATA #IMPLIED
+ xml:base CDATA #IMPLIED
+ remap CDATA #IMPLIED
+ xreflabel CDATA #IMPLIED
+ revisionflag (changed|added|deleted|off) #IMPLIED
+ dir (ltr|rtl|lro|rlo) #IMPLIED
+ arch CDATA #IMPLIED
+ audience CDATA #IMPLIED
+ condition CDATA #IMPLIED
+ conformance CDATA #IMPLIED
+ os CDATA #IMPLIED
+ revision CDATA #IMPLIED
+ security CDATA #IMPLIED
+ userlevel CDATA #IMPLIED
+ vendor CDATA #IMPLIED
+ wordsize CDATA #IMPLIED
+ annotations CDATA #IMPLIED
+
+"></textarea></form>
+ <script>
+ var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
+ mode: {name: "dtd", alignCDATA: true},
+ lineNumbers: true,
+ lineWrapping: true
+ });
+ </script>
+
+ <p><strong>MIME types defined:</strong> <code>application/xml-dtd</code>.</p>
+ </article>
View
1  mode/index.html
@@ -41,6 +41,7 @@
<li><a href="python/index.html">Cython</a></li>
<li><a href="d/index.html">D</a></li>
<li><a href="diff/index.html">diff</a></li>
+ <li><a href="dtd/index.html">DTD</a></li>
<li><a href="ecl/index.html">ECL</a></li>
<li><a href="erlang/index.html">Erlang</a></li>
<li><a href="gas/index.html">Gas</a> (AT&amp;T-style assembly)</li>
Please sign in to comment.
Something went wrong with that request. Please try again.