handlebars.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://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. meta: {
  43. blockCommentStart: "{{--",
  44. blockCommentEnd: "--}}"
  45. }
  46. });
  47. CodeMirror.defineMode("handlebars", function(config, parserConfig) {
  48. var handlebars = CodeMirror.getMode(config, "handlebars-tags");
  49. if (!parserConfig || !parserConfig.base) return handlebars;
  50. return CodeMirror.multiplexingMode(
  51. CodeMirror.getMode(config, parserConfig.base),
  52. {open: "{{", close: "}}", mode: handlebars, parseDelimiters: true}
  53. );
  54. });
  55. CodeMirror.defineMIME("text/x-handlebars-template", "handlebars");
  56. });