Skip to content

Commit

Permalink
v. 4.0.0 in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikos M committed Apr 9, 2016
1 parent 36b919e commit 3f47701
Show file tree
Hide file tree
Showing 2 changed files with 178 additions and 0 deletions.
102 changes: 102 additions & 0 deletions test/grammar-htmlmixed.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<!doctype html>
<html>
<head>

<meta charset="utf-8">

<link rel="stylesheet" href="codemirror/codemirror.css">
<link rel="stylesheet" href="codemirror/addon/lint/lint.css">
<link rel="stylesheet" href="codemirror/addon/hint/show-hint.css">
<link rel="stylesheet" href="codemirror/addon/fold/foldgutter.css" />
<style type="text/css">
body {
position: relative;
}

.CodeMirror {
margin: 0;
position: relative;
width: 100%;
height: 100%;
min-width:500px;
min-height:500px;
}
</style>

<script src="codemirror/codemirror.js"></script>
<script src="codemirror/addon/comment/comment.js"></script>
<script src="codemirror/addon/lint/lint.js"></script>
<script src="codemirror/addon/hint/show-hint.js"></script>
<script src="codemirror/addon/fold/foldcode.js"></script>
<script src="codemirror/addon/fold/foldgutter.js"></script>
<!--<script src="codemirror/addon/fold/xml-fold.js"></script>
<script src="codemirror/addon/fold/comment-fold.js"></script>-->
<script src="../build/codemirror_grammar.js"></script>
<!-- these grammars are used only for demo, better grammars can be used -->
<script src="grammars/javascript.js"></script>
<script src="grammars/css.js"></script>
<script src="grammars/htmlmixed.js"></script>
<script src="demo.js"></script>

<title>CodeMirror: Dynamic Mixed HTML + JavaScript + CSS Grammar Demo</title>
</head>
<body>

<h3>CodeMirror (v.<span id="editor-version">0</span>) Grammar (v.<span id="grammar-version">0</span>): Dynamic Mixed HTML + JavaScript + CSS Grammar</h3>
<textarea id="code" name="code">&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;!doctype html&gt;
&lt;html id="id1" id="id2" style = "color: green"&gt;
&lt;!-- this is a comment --&gt;
&lt;head id="id1"&gt;
&lt;title&gt;HTML Example&lt;/title&gt;
&lt;style&gt;
/* Some example CSS */
@import url("something.css");
body {
margin: 0;
padding: 3em 6em;
font-family: tahoma, arial, sans-serif;
color: #000;
}
h1:before, h2:before {
content: "::";
}
&lt;/style&gt;
&lt;script&gt;
/* a comment */
var bar = { foo: [1, 2, 3, {"foo3":"foo4"}],
/* a long
comment */
foo2: 123
};
&lt;/script&gt;
&lt;/head&gt;
&lt;body id="id1"&gt;
&lt;!-- this is a
long comment --&gt;
&lt;div class="divclass"&gt;
&lt;![CDATA[
cdata text in here
&lt;a_tag&gt;, etc..
]]&gt;
&lt;/div&gt;
The indentation tries to be &lt;em&gt;somewhat &amp;quot;do what
I mean&amp;quot;&lt;/em&gt;... but might not match your style.
&lt;/bdy&gt;
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;!doctype html&gt;
&lt;/html&gt;
</textarea>
<p></p>

<script>
// <![CDATA[
codemirror_grammar_demo(document.getElementById("code"), [
{language : "htmlmixed", grammar : htmlmixed_grammar}
,{language : "javascript", grammar : js_grammar}
,{language : "css", grammar : css_grammar}
]);
// ]]>
</script>
</body>
</html>
76 changes: 76 additions & 0 deletions test/grammars/htmlmixed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// 1. a partial html grammar with mixed javascript/css sub-grammars in simple JSON format
var htmlmixed_grammar = {

// prefix ID for regular expressions, represented as strings, used in the grammar
"RegExpID" : "RE::",

"Extra" : {

"fold" : "xml"
//"electricChars" : "<"

},

// Style model
"Style" : {

"comment" : "comment"
,"declaration" : "tag"
,"doctype" : "meta"
,"meta" : "meta"
,"cdata" : "atom"
,"tag" : "tag"
,"attribute" : "attribute"
,"string" : "string"
,"atom" : "atom"
,"number" : "number"
,"error" : "error"

},

// Lexical model
"Lex" : {

"comment:comment" : ["<!--", "-->"]
,"declaration:block" : ["<?xml", "?>"]
,"doctype:block" : ["RE::/<!doctype\\b/i", ">"]
,"meta:block" : ["RE::/<\\?[_a-zA-Z][\\w\\._\\-]*/", "?>"]
,"cdata:block" : ["<![CDATA[", "]]>"]
,"open_tag" : "RE::/<([_a-zA-Z][_a-zA-Z0-9\\-]*)/"
,"close_tag" : "RE::/<\\/([_a-zA-Z][_a-zA-Z0-9\\-]*)>/"
,"attribute" : "RE::/[_a-zA-Z][_a-zA-Z0-9\\-]*/"
,"string:line-block" : [["\""], ["'"]]
,"number" : ["RE::/[0-9]\\d*/", "RE::/#[0-9a-fA-F]+/"]
,"atom" : ["RE::/&#x[a-fA-F\\d]+;/", "RE::/&#[\\d]+;/", "RE::/&[a-zA-Z][a-zA-Z0-9]*;/"]
,"text" : "RE::/[^<&]+/"

// actions
,"tag_ctx:action" : {"context":true}
,"\\tag_ctx:action" : {"context":false}
,"unique_id:action" : {"unique":["xml", "$1"],"msg":"Duplicate id value \"$0\""}
,"unique_att:action" : {"unique":["tag", "$0"],"msg":"Duplicate attribute \"$0\"","in-context":true}
,"tag_opened:action" : {"push":"<$1>","ci":true}
,"tag_closed:action" : {"pop":"<$1>","ci":true,"msg":"Tags \"$0\" and \"$1\" do not match"}
,"tag_autoclosed:action" : {"pop":null}
,"out_of_place:error" : "\"$2$3\" can only be at the beginning of XML document"

},

// Syntax model (optional)
"Syntax" : {

"javascript" : {"subgrammar":"javascript"}
,"css" : {"subgrammar":"css"}
,"tag_att" : "'id'.attribute unique_att '=' string unique_id | attribute unique_att '=' (string | number)"
,"style_tag" : "'<style>'.tag css '</style>'.tag"
,"script_tag" : "'<script>'.tag javascript '</script>'.tag"
,"start_tag" : "open_tag.tag tag_ctx tag_opened tag_att* ('>'.tag | '/>'.tag tag_autoclosed) \\tag_ctx"
,"end_tag" : "close_tag.tag tag_closed"
,"htmlmixed" : "(^^1 declaration? doctype?) (declaration.error out_of_place | doctype.error out_of_place | comment | meta | cdata | style_tag | script_tag | start_tag | end_tag | atom | text)*"

},

// what to parse and in what order
"Parser" : [ ["htmlmixed"] ]

};

0 comments on commit 3f47701

Please sign in to comment.