handlebars.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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("../../addon/mode/simple"), require("../../addon/mode/multiplex"));
  6. else if (typeof define == "function" && define.amd) // AMD
  7. define(["../../lib/codemirror", "../../addon/mode/simple", "../../addon/mode/multiplex"], mod);
  8. else // Plain browser env
  9. mod(CodeMirror);
  10. })(function(CodeMirror) {
  11. "use strict";
  12. CodeMirror.defineSimpleMode("handlebars-tags", {
  13. start: [
  14. { regex: /\{\{!--/, push: "dash_comment", token: "comment" },
  15. { regex: /\{\{!/, push: "comment", token: "comment" },
  16. { regex: /\{\{/, push: "handlebars", token: "tag" }
  17. ],
  18. handlebars: [
  19. { regex: /\}\}/, pop: true, token: "tag" },
  20. // Double and single quotes
  21. { regex: /"(?:[^\\"]|\\.)*"?/, token: "string" },
  22. { regex: /'(?:[^\\']|\\.)*'?/, token: "string" },
  23. // Handlebars keywords
  24. { regex: />|[#\/]([A-Za-z_]\w*)/, token: "keyword" },
  25. { regex: /(?:else|this)\b/, token: "keyword" },
  26. // Numeral
  27. { regex: /\d+/i, token: "number" },
  28. // Atoms like = and .
  29. { regex: /=|~|@|true|false/, token: "atom" },
  30. // Paths
  31. { regex: /(?:\.\.\/)*(?:[A-Za-z_][\w\.]*)+/, token: "variable-2" }
  32. ],
  33. dash_comment: [
  34. { regex: /--\}\}/, pop: true, token: "comment" },
  35. // Commented code
  36. { regex: /./, token: "comment"}
  37. ],
  38. comment: [
  39. { regex: /\}\}/, pop: true, token: "comment" },
  40. { regex: /./, token: "comment" }
  41. ]
  42. });
  43. CodeMirror.defineMode("handlebars", function(config, parserConfig) {
  44. var handlebars = CodeMirror.getMode(config, "handlebars-tags");
  45. if (!parserConfig || !parserConfig.base) return handlebars;
  46. return CodeMirror.multiplexingMode(
  47. CodeMirror.getMode(config, parserConfig.base),
  48. {open: "{{", close: "}}", mode: handlebars, parseDelimiters: true}
  49. );
  50. });
  51. CodeMirror.defineMIME("text/x-handlebars-template", "handlebars");
  52. });