htmlembedded.js 3.0 KB

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