scripts/CodeMirror/mode/handlebars/handlebars.js (view raw)
1// CodeMirror, copyright (c) by Marijn Haverbeke and others
2// Distributed under an MIT license: https://codemirror.net/LICENSE
3
4(function(mod) {
5 if (typeof exports == "object" && typeof module == "object") // CommonJS
6 mod(require("../../lib/codemirror"), require("../../addon/mode/simple"), require("../../addon/mode/multiplex"));
7 else if (typeof define == "function" && define.amd) // AMD
8 define(["../../lib/codemirror", "../../addon/mode/simple", "../../addon/mode/multiplex"], mod);
9 else // Plain browser env
10 mod(CodeMirror);
11})(function(CodeMirror) {
12 "use strict";
13
14 CodeMirror.defineSimpleMode("handlebars-tags", {
15 start: [
16 { regex: /\{\{\{/, push: "handlebars_raw", token: "tag" },
17 { regex: /\{\{!--/, push: "dash_comment", token: "comment" },
18 { regex: /\{\{!/, push: "comment", token: "comment" },
19 { regex: /\{\{/, push: "handlebars", token: "tag" }
20 ],
21 handlebars_raw: [
22 { regex: /\}\}\}/, pop: true, token: "tag" },
23 ],
24 handlebars: [
25 { regex: /\}\}/, pop: true, token: "tag" },
26
27 // Double and single quotes
28 { regex: /"(?:[^\\"]|\\.)*"?/, token: "string" },
29 { regex: /'(?:[^\\']|\\.)*'?/, token: "string" },
30
31 // Handlebars keywords
32 { regex: />|[#\/]([A-Za-z_]\w*)/, token: "keyword" },
33 { regex: /(?:else|this)\b/, token: "keyword" },
34
35 // Numeral
36 { regex: /\d+/i, token: "number" },
37
38 // Atoms like = and .
39 { regex: /=|~|@|true|false/, token: "atom" },
40
41 // Paths
42 { regex: /(?:\.\.\/)*(?:[A-Za-z_][\w\.]*)+/, token: "variable-2" }
43 ],
44 dash_comment: [
45 { regex: /--\}\}/, pop: true, token: "comment" },
46
47 // Commented code
48 { regex: /./, token: "comment"}
49 ],
50 comment: [
51 { regex: /\}\}/, pop: true, token: "comment" },
52 { regex: /./, token: "comment" }
53 ],
54 meta: {
55 blockCommentStart: "{{--",
56 blockCommentEnd: "--}}"
57 }
58 });
59
60 CodeMirror.defineMode("handlebars", function(config, parserConfig) {
61 var handlebars = CodeMirror.getMode(config, "handlebars-tags");
62 if (!parserConfig || !parserConfig.base) return handlebars;
63 return CodeMirror.multiplexingMode(
64 CodeMirror.getMode(config, parserConfig.base),
65 {open: "{{", close: /\}\}\}?/, mode: handlebars, parseDelimiters: true}
66 );
67 });
68
69 CodeMirror.defineMIME("text/x-handlebars-template", "handlebars");
70});