htmlembedded.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. CodeMirror.defineMode("htmlembedded", function(config, parserConfig) {
  2. //config settings
  3. var scriptStartRegex = parserConfig.scriptStartRegex || /^<%/i,
  4. scriptEndRegex = parserConfig.scriptEndRegex || /^%>/i;
  5. //inner modes
  6. var scriptingMode, htmlMixedMode;
  7. //tokenizer when in html mode
  8. function htmlDispatch(stream, state) {
  9. if (stream.match(scriptStartRegex, false)) {
  10. state.token=scriptingDispatch;
  11. return scriptingMode.token(stream, state.scriptState);
  12. }
  13. else
  14. return htmlMixedMode.token(stream, state.htmlState);
  15. }
  16. //tokenizer when in scripting mode
  17. function scriptingDispatch(stream, state) {
  18. if (stream.match(scriptEndRegex, false)) {
  19. state.token=htmlDispatch;
  20. return htmlMixedMode.token(stream, state.htmlState);
  21. }
  22. else
  23. return scriptingMode.token(stream, state.scriptState);
  24. }
  25. return {
  26. startState: function() {
  27. scriptingMode = scriptingMode || CodeMirror.getMode(config, parserConfig.scriptingModeSpec);
  28. htmlMixedMode = htmlMixedMode || CodeMirror.getMode(config, "htmlmixed");
  29. return {
  30. token : parserConfig.startOpen ? scriptingDispatch : htmlDispatch,
  31. htmlState : CodeMirror.startState(htmlMixedMode),
  32. scriptState : CodeMirror.startState(scriptingMode)
  33. };
  34. },
  35. token: function(stream, state) {
  36. return state.token(stream, state);
  37. },
  38. indent: function(state, textAfter) {
  39. if (state.token == htmlDispatch)
  40. return htmlMixedMode.indent(state.htmlState, textAfter);
  41. else if (scriptingMode.indent)
  42. return scriptingMode.indent(state.scriptState, textAfter);
  43. },
  44. copyState: function(state) {
  45. return {
  46. token : state.token,
  47. htmlState : CodeMirror.copyState(htmlMixedMode, state.htmlState),
  48. scriptState : CodeMirror.copyState(scriptingMode, state.scriptState)
  49. };
  50. },
  51. electricChars: "/{}:",
  52. innerMode: function(state) {
  53. if (state.token == scriptingDispatch) return {state: state.scriptState, mode: scriptingMode};
  54. else return {state: state.htmlState, mode: htmlMixedMode};
  55. }
  56. };
  57. }, "htmlmixed");
  58. CodeMirror.defineMIME("application/x-ejs", { name: "htmlembedded", scriptingModeSpec:"javascript"});
  59. CodeMirror.defineMIME("application/x-aspx", { name: "htmlembedded", scriptingModeSpec:"text/x-csharp"});
  60. CodeMirror.defineMIME("application/x-jsp", { name: "htmlembedded", scriptingModeSpec:"text/x-java"});
  61. CodeMirror.defineMIME("application/x-erb", { name: "htmlembedded", scriptingModeSpec:"ruby"});