Skip to content

Commit fb72b7a

Browse files
whalefoodmarijnh
authored andcommitted
Modified and renamed ejs mode to be generic so could include modes for asp, jsp and possibly php.
1 parent 42bcd3f commit fb72b7a

File tree

3 files changed

+82
-65
lines changed

3 files changed

+82
-65
lines changed

mode/ejs/ejs.js

Lines changed: 0 additions & 57 deletions
This file was deleted.

mode/htmlembedded/htmlembedded.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
CodeMirror.defineMode("htmlembedded", function(config, parserConfig) {
2+
3+
//config settings
4+
var scriptStartRegex = parserConfig.scriptStartRegex || /^<%/i,
5+
scriptEndRegex = parserConfig.scriptEndRegex || /^%>/i,
6+
scriptingMode;
7+
8+
var htmlMixedMode = CodeMirror.getMode(config, "htmlmixed");
9+
10+
//tokenizer when in html mode
11+
function htmlDispatch(stream, state) {
12+
if (stream.match(scriptStartRegex, false)) {
13+
state.token=scriptingDispatch;
14+
return scriptingMode.token(stream, state.scriptState);
15+
}
16+
else
17+
return htmlMixedMode.token(stream, state.htmlState);
18+
}
19+
20+
//tokenizer when in scripting mode
21+
function scriptingDispatch(stream, state) {
22+
if (stream.match(scriptEndRegex, false)) {
23+
state.token=htmlDispatch;
24+
return htmlMixedMode.token(stream, state.htmlState);
25+
}
26+
else
27+
return scriptingMode.token(stream, state.scriptState);
28+
}
29+
30+
31+
return {
32+
startState: function() {
33+
scriptingMode = scriptingMode || CodeMirror.getMode(config, parserConfig.scriptingModeSpec)
34+
return {
35+
token : parserConfig.startOpen ? scriptingDispatch : htmlDispatch,
36+
htmlState : htmlMixedMode.startState(),
37+
scriptState : scriptingMode.startState()
38+
}
39+
},
40+
41+
token: function(stream, state) {
42+
return state.token(stream, state);
43+
},
44+
45+
indent: function(state, textAfter) {
46+
if (state.token == htmlDispatch)
47+
return htmlMixedMode.indent(state.htmlState, textAfter);
48+
else
49+
return scriptingMode.indent(state.scriptState, textAfter);
50+
},
51+
52+
copyState: function(state) {
53+
return {
54+
token : state.token,
55+
htmlState : CodeMirror.copyState(htmlMixedMode, state.htmlState),
56+
scriptState : CodeMirror.copyState(scriptingMode, state.scriptState)
57+
}
58+
},
59+
60+
61+
electricChars: "/{}:"
62+
}
63+
});
64+
65+
CodeMirror.defineMIME("application/x-ejs", { name: "htmlembedded", scriptingModeSpec:"javascript"});
66+
CodeMirror.defineMIME("application/x-aspx", { name: "htmlembedded", scriptingModeSpec:"text/x-csharp"});
67+
CodeMirror.defineMIME("application/x-jsp", { name: "htmlembedded", scriptingModeSpec:"text/x-java"});
68+
/*CodeMirror.defineMIME("application/x-httpd-php", {
69+
name: "htmlembedded",
70+
scriptingModeSpec:"text/x-php",
71+
scriptStartRegex: /^<\?/,
72+
scriptEndRegex: /^\?>/});*/
Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
11
<!doctype html>
22
<html>
33
<head>
4-
<title>CodeMirror 2: Ejs mode</title>
4+
<title>CodeMirror 2: Html Embedded Scripts mode</title>
55
<link rel="stylesheet" href="../../lib/codemirror.css">
66
<script src="../../lib/codemirror.js"></script>
77
<script src="../xml/xml.js"></script>
88
<script src="../javascript/javascript.js"></script>
99
<script src="../css/css.js"></script>
1010
<script src="../htmlmixed/htmlmixed.js"></script>
11-
<script src="ejs.js"></script>
11+
<script src="htmlembedded.js"></script>
1212
<style type="text/css">.CodeMirror {border-top: 1px solid black; border-bottom: 1px solid black;}</style>
1313
<link rel="stylesheet" href="../../doc/docs.css">
1414
</head>
1515
<body>
16-
<h1>CodeMirror 2: Ejs mode</h1>
16+
<h1>CodeMirror 2: Html Embedded Scripts mode</h1>
1717

1818
<form><textarea id="code" name="code">
1919
<%
2020
function hello(who) {
2121
return "Hello " + who;
2222
}
2323
%>
24-
<p>The program says <%= hello("World") %>.</p>
24+
This is an example of EJS (embedded javascript)
25+
<p>The program says <%= hello("world") %>.</p>
2526
<script>
26-
alert("And here is some JS code"); // also colored
27+
alert("And here is some normal JS code"); // also colored
2728
</script>
2829
</textarea></form>
2930

@@ -39,9 +40,10 @@ <h1>CodeMirror 2: Ejs mode</h1>
3940
});
4041
</script>
4142

42-
<p>Simple HTML/EJS mode. Depends on XML,
43-
JavaScript, CSS and HtmlMixed modes.</p>
43+
<p>Mode for html embedded scripts like JSP and ASP.NET. Depends on HtmlMixed which in turn depends on
44+
JavaScript, CSS and XML.<br />Other dependancies include those of the scriping language chosen.</p>
4445

45-
<p><strong>MIME types defined:</strong> <code>application/x-ejs</code></p>
46+
<p><strong>MIME types defined:</strong> <code>application/x-aspx</code> (ASP.NET),
47+
<code>application/x-ejs</code> (Embedded Javascript), <code>application/x-jsp</code> (JavaServer Pages)</p>
4648
</body>
4749
</html>

0 commit comments

Comments
 (0)