Commit | Line | Data |
---|---|---|
837afb80 TD |
1 | (function(mod) { |
2 | if (typeof exports == "object" && typeof module == "object") // CommonJS | |
3 | mod(require("../../lib/codemirror"), require("../htmlmixed/htmlmixed")); | |
4 | else if (typeof define == "function" && define.amd) // AMD | |
5 | define(["../../lib/codemirror", "../htmlmixed/htmlmixed"], mod); | |
6 | else // Plain browser env | |
7 | mod(CodeMirror); | |
8 | })(function(CodeMirror) { | |
9 | "use strict"; | |
10 | ||
77b7b761 TD |
11 | CodeMirror.defineMode("htmlembedded", function(config, parserConfig) { |
12 | ||
13 | //config settings | |
14 | var scriptStartRegex = parserConfig.scriptStartRegex || /^<%/i, | |
15 | scriptEndRegex = parserConfig.scriptEndRegex || /^%>/i; | |
16 | ||
17 | //inner modes | |
18 | var scriptingMode, htmlMixedMode; | |
19 | ||
20 | //tokenizer when in html mode | |
21 | function htmlDispatch(stream, state) { | |
22 | if (stream.match(scriptStartRegex, false)) { | |
23 | state.token=scriptingDispatch; | |
24 | return scriptingMode.token(stream, state.scriptState); | |
25 | } | |
26 | else | |
27 | return htmlMixedMode.token(stream, state.htmlState); | |
28 | } | |
29 | ||
30 | //tokenizer when in scripting mode | |
31 | function scriptingDispatch(stream, state) { | |
32 | if (stream.match(scriptEndRegex, false)) { | |
33 | state.token=htmlDispatch; | |
34 | return htmlMixedMode.token(stream, state.htmlState); | |
35 | } | |
36 | else | |
37 | return scriptingMode.token(stream, state.scriptState); | |
38 | } | |
39 | ||
40 | ||
41 | return { | |
42 | startState: function() { | |
43 | scriptingMode = scriptingMode || CodeMirror.getMode(config, parserConfig.scriptingModeSpec); | |
44 | htmlMixedMode = htmlMixedMode || CodeMirror.getMode(config, "htmlmixed"); | |
45 | return { | |
46 | token : parserConfig.startOpen ? scriptingDispatch : htmlDispatch, | |
47 | htmlState : CodeMirror.startState(htmlMixedMode), | |
48 | scriptState : CodeMirror.startState(scriptingMode) | |
49 | }; | |
50 | }, | |
51 | ||
52 | token: function(stream, state) { | |
53 | return state.token(stream, state); | |
54 | }, | |
55 | ||
56 | indent: function(state, textAfter) { | |
57 | if (state.token == htmlDispatch) | |
58 | return htmlMixedMode.indent(state.htmlState, textAfter); | |
59 | else if (scriptingMode.indent) | |
60 | return scriptingMode.indent(state.scriptState, textAfter); | |
61 | }, | |
62 | ||
63 | copyState: function(state) { | |
64 | return { | |
65 | token : state.token, | |
66 | htmlState : CodeMirror.copyState(htmlMixedMode, state.htmlState), | |
67 | scriptState : CodeMirror.copyState(scriptingMode, state.scriptState) | |
68 | }; | |
69 | }, | |
70 | ||
77b7b761 TD |
71 | innerMode: function(state) { |
72 | if (state.token == scriptingDispatch) return {state: state.scriptState, mode: scriptingMode}; | |
73 | else return {state: state.htmlState, mode: htmlMixedMode}; | |
74 | } | |
75 | }; | |
76 | }, "htmlmixed"); | |
77 | ||
78 | CodeMirror.defineMIME("application/x-ejs", { name: "htmlembedded", scriptingModeSpec:"javascript"}); | |
79 | CodeMirror.defineMIME("application/x-aspx", { name: "htmlembedded", scriptingModeSpec:"text/x-csharp"}); | |
80 | CodeMirror.defineMIME("application/x-jsp", { name: "htmlembedded", scriptingModeSpec:"text/x-java"}); | |
81 | CodeMirror.defineMIME("application/x-erb", { name: "htmlembedded", scriptingModeSpec:"ruby"}); | |
837afb80 TD |
82 | |
83 | }); |