handlebars.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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: "handlebars_raw", token: "tag" },
  15. { regex: /\{\{!--/, push: "dash_comment", token: "comment" },
  16. { regex: /\{\{!/, push: "comment", token: "comment" },
  17. { regex: /\{\{/, push: "handlebars", token: "tag" }
  18. ],
  19. handlebars_raw: [
  20. { regex: /\}\}\}/, pop: true, token: "tag" },
  21. ],
  22. handlebars: [
  23. { regex: /\}\}/, pop: true, token: "tag" },
  24. // Double and single quotes
  25. { regex: /"(?:[^\\"]|\\.)*"?/, token: "string" },
  26. { regex: /'(?:[^\\']|\\.)*'?/, token: "string" },
  27. // Handlebars keywords
  28. { regex: />|[#\/]([A-Za-z_]\w*)/, token: "keyword" },
  29. { regex: /(?:else|this)\b/, token: "keyword" },
  30. // Numeral
  31. { regex: /\d+/i, token: "number" },
  32. // Atoms like = and .
  33. { regex: /=|~|@|true|false/, token: "atom" },
  34. // Paths
  35. { regex: /(?:\.\.\/)*(?:[A-Za-z_][\w\.]*)+/, token: "variable-2" }
  36. ],
  37. dash_comment: [
  38. { regex: /--\}\}/, pop: true, token: "comment" },
  39. // Commented code
  40. { regex: /./, token: "comment"}
  41. ],
  42. comment: [
  43. { regex: /\}\}/, pop: true, token: "comment" },
  44. { regex: /./, token: "comment" }
  45. ],
  46. meta: {
  47. blockCommentStart: "{{--",
  48. blockCommentEnd: "--}}"
  49. }
  50. });
  51. CodeMirror.defineMode("handlebars", function(config, parserConfig) {
  52. var handlebars = CodeMirror.getMode(config, "handlebars-tags");
  53. if (!parserConfig || !parserConfig.base) return handlebars;
  54. return CodeMirror.multiplexingMode(
  55. CodeMirror.getMode(config, parserConfig.base),
  56. {open: "{{", close: /\}\}\}?/, mode: handlebars, parseDelimiters: true}
  57. );
  58. });
  59. CodeMirror.defineMIME("text/x-handlebars-template", "handlebars");
  60. });