handlebars.go

 1package h
 2
 3import (
 4	. "github.com/alecthomas/chroma" // nolint
 5	"github.com/alecthomas/chroma/lexers/internal"
 6)
 7
 8// Handlebars lexer.
 9var Handlebars = internal.Register(MustNewLexer(
10	&Config{
11		Name:      "Handlebars",
12		Aliases:   []string{"handlebars"},
13		Filenames: []string{"*.handlebars"},
14		MimeTypes: []string{},
15	},
16	Rules{
17		"root": {
18			{`[^{]+`, Other, nil},
19			{`\{\{!.*\}\}`, Comment, nil},
20			{`(\{\{\{)(\s*)`, ByGroups(CommentSpecial, Text), Push("tag")},
21			{`(\{\{)(\s*)`, ByGroups(CommentPreproc, Text), Push("tag")},
22		},
23		"tag": {
24			{`\s+`, Text, nil},
25			{`\}\}\}`, CommentSpecial, Pop(1)},
26			{`\}\}`, CommentPreproc, Pop(1)},
27			{`([#/]*)(each|if|unless|else|with|log|in(?:line)?)`, ByGroups(Keyword, Keyword), nil},
28			{`#\*inline`, Keyword, nil},
29			{`([#/])([\w-]+)`, ByGroups(NameFunction, NameFunction), nil},
30			{`([\w-]+)(=)`, ByGroups(NameAttribute, Operator), nil},
31			{`(>)(\s*)(@partial-block)`, ByGroups(Keyword, Text, Keyword), nil},
32			{`(#?>)(\s*)([\w-]+)`, ByGroups(Keyword, Text, NameVariable), nil},
33			{`(>)(\s*)(\()`, ByGroups(Keyword, Text, Punctuation), Push("dynamic-partial")},
34			Include("generic"),
35		},
36		"dynamic-partial": {
37			{`\s+`, Text, nil},
38			{`\)`, Punctuation, Pop(1)},
39			{`(lookup)(\s+)(\.|this)(\s+)`, ByGroups(Keyword, Text, NameVariable, Text), nil},
40			{`(lookup)(\s+)(\S+)`, ByGroups(Keyword, Text, UsingSelf("variable")), nil},
41			{`[\w-]+`, NameFunction, nil},
42			Include("generic"),
43		},
44		"variable": {
45			{`[a-zA-Z][\w-]*`, NameVariable, nil},
46			{`\.[\w-]+`, NameVariable, nil},
47			{`(this\/|\.\/|(\.\.\/)+)[\w-]+`, NameVariable, nil},
48		},
49		"generic": {
50			Include("variable"),
51			{`:?"(\\\\|\\"|[^"])*"`, LiteralStringDouble, nil},
52			{`:?'(\\\\|\\'|[^'])*'`, LiteralStringSingle, nil},
53			{`[0-9](\.[0-9]*)?(eE[+-][0-9])?[flFLdD]?|0[xX][0-9a-fA-F]+[Ll]?`, LiteralNumber, nil},
54		},
55	},
56))