Skip to content

Commit 6665753

Browse files
authored
Improved support for Go templates (#401)
1 parent 290ff86 commit 6665753

9 files changed

+118
-11
lines changed

Diff for: lexers/g/go.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,13 @@ var Go = internal.Register(MustNewLexer(
6060

6161
var goTemplateRules = Rules{
6262
"root": {
63+
{`{{(- )?/\*(.|\n)*?\*/( -)?}}`, CommentMultiline, nil},
6364
{`{{[-]?`, CommentPreproc, Push("template")},
6465
{`[^{]+`, Other, nil},
6566
{`{`, Other, nil},
6667
},
6768
"template": {
6869
{`[-]?}}`, CommentPreproc, Pop(1)},
69-
{`/\*.*?\*/`, Comment, nil},
7070
{`(?=}})`, CommentPreproc, Pop(1)}, // Terminate the pipeline
7171
{`\(`, Operator, Push("subexpression")},
7272
{`"(\\\\|\\"|[^"])*"`, LiteralString, nil},
@@ -80,19 +80,19 @@ var goTemplateRules = Rules{
8080
{`\s+`, Whitespace, nil},
8181
{`\(`, Operator, Push("subexpression")},
8282
{`(range|if|else|while|with|template|end|true|false|nil|and|call|html|index|js|len|not|or|print|printf|println|urlquery|eq|ne|lt|le|gt|ge)\b`, Keyword, nil},
83-
{`\||:=`, Operator, nil},
83+
{`\||:?=`, Operator, nil},
8484
{`[$]?[^\W\d]\w*`, NameOther, nil},
8585
{`[$]?\.(?:[^\W\d]\w*)?`, NameAttribute, nil},
8686
{`"(\\\\|\\"|[^"])*"`, LiteralString, nil},
87-
{`\d+i`, LiteralNumber, nil},
88-
{`\d+\.\d*([Ee][-+]\d+)?i`, LiteralNumber, nil},
87+
{`-?\d+i`, LiteralNumber, nil},
88+
{`-?\d+\.\d*([Ee][-+]\d+)?i`, LiteralNumber, nil},
8989
{`\.\d+([Ee][-+]\d+)?i`, LiteralNumber, nil},
90-
{`\d+[Ee][-+]\d+i`, LiteralNumber, nil},
91-
{`\d+(\.\d+[eE][+\-]?\d+|\.\d*|[eE][+\-]?\d+)`, LiteralNumberFloat, nil},
92-
{`\.\d+([eE][+\-]?\d+)?`, LiteralNumberFloat, nil},
93-
{`0[0-7]+`, LiteralNumberOct, nil},
94-
{`0[xX][0-9a-fA-F]+`, LiteralNumberHex, nil},
95-
{`(0|[1-9][0-9]*)`, LiteralNumberInteger, nil},
90+
{`-?\d+[Ee][-+]\d+i`, LiteralNumber, nil},
91+
{`-?\d+(\.\d+[eE][+\-]?\d+|\.\d*|[eE][+\-]?\d+)`, LiteralNumberFloat, nil},
92+
{`-?\.\d+([eE][+\-]?\d+)?`, LiteralNumberFloat, nil},
93+
{`-?0[0-7]+`, LiteralNumberOct, nil},
94+
{`-?0[xX][0-9a-fA-F]+`, LiteralNumberHex, nil},
95+
{`-?(0|[1-9][0-9]*)`, LiteralNumberInteger, nil},
9696
{`'(\\['"\\abfnrtv]|\\x[0-9a-fA-F]{2}|\\[0-7]{1,3}|\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8}|[^\\])'`, LiteralStringChar, nil},
9797
{"`[^`]*`", LiteralString, nil},
9898
},

Diff for: lexers/g/go_test.go

+72
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ func TestGoHTMLTemplateIssue126(t *testing.T) {
2323
{{ with .OutputFormats.Get "RSS" }}
2424
{{ printf "<atom:link href=%q rel=\"self\" type=%q />" .Permalink .MediaType | safeHTML }}
2525
{{ end }}
26+
{{/*
27+
Print all pages
28+
*/}}
2629
{{ range .Data.Pages }}
2730
<item>
2831
<title>{{ .Title }}</title>
@@ -48,3 +51,72 @@ func TestGoHTMLTemplateIssue126(t *testing.T) {
4851
assert.Equal(t, source, chroma.Stringify(tokens...))
4952
}
5053
}
54+
55+
func TestGoHTMLTemplateMultilineComments(t *testing.T) {
56+
for _, source := range []string{
57+
`
58+
{{/*
59+
This is a multiline comment
60+
*/}}
61+
`,
62+
`
63+
{{- /*
64+
This is a multiline comment
65+
*/}}
66+
`,
67+
`
68+
{{/*
69+
This is a multiline comment
70+
*/ -}}
71+
`,
72+
`
73+
{{- /*
74+
This is a multiline comment
75+
*/ -}}
76+
`,
77+
} {
78+
tokens, err := chroma.Tokenise(GoHTMLTemplate, nil, source)
79+
assert.NoError(t, err)
80+
assert.Equal(t, source, chroma.Stringify(tokens...))
81+
82+
// Make sure that there are no errors
83+
for _, token := range tokens {
84+
assert.NotEqual(t, chroma.Error, token.Type)
85+
}
86+
87+
// Make sure that multiline comments are printed
88+
found := false
89+
for _, token := range tokens {
90+
if token.Type == chroma.CommentMultiline {
91+
found = true
92+
}
93+
}
94+
assert.True(t, found)
95+
}
96+
}
97+
98+
func TestGoHTMLTemplateNegativeNumber(t *testing.T) {
99+
for _, source := range []string{
100+
`
101+
{{ fn -3 }}
102+
`,
103+
} {
104+
tokens, err := chroma.Tokenise(GoHTMLTemplate, nil, source)
105+
assert.NoError(t, err)
106+
assert.Equal(t, source, chroma.Stringify(tokens...))
107+
108+
// Make sure that there are no errors
109+
for _, token := range tokens {
110+
assert.NotEqual(t, chroma.Error, token.Type)
111+
}
112+
113+
// Make sure that negative number is found
114+
found := false
115+
for _, token := range tokens {
116+
if token.Type == chroma.LiteralNumberInteger {
117+
found = true
118+
}
119+
}
120+
assert.True(t, found)
121+
}
122+
}

Diff for: lexers/lexers_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func TestLexers(t *testing.T) {
5555
continue
5656
}
5757

58-
base := strings.Split(strings.TrimSuffix(file.Name(), filepath.Ext(file.Name())), "-")[0]
58+
base := strings.Split(strings.TrimSuffix(file.Name(), filepath.Ext(file.Name())), ".")[0]
5959
lexer := lexers.Get(base)
6060
assert.NotNil(t, lexer)
6161

Diff for: lexers/testdata/go-html-template.actual

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{{/*
2+
This is a multiline comment
3+
*/}}

Diff for: lexers/testdata/go-html-template.expected

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[
2+
{"type":"CommentMultiline","value":"{{/*\n This is a multiline comment\n*/}}"}
3+
]

Diff for: lexers/testdata/go-text-template.actual

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{{/*
2+
This is a multiline comment
3+
*/}}
4+
5+
{{ $myVar := 2 }}
6+
{{ $myVar = 4 }}

Diff for: lexers/testdata/go-text-template.expected

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[
2+
{"type":"CommentMultiline","value":"{{/*\n This is a multiline comment\n*/}}"},
3+
{"type":"Other","value":"\n\n"},
4+
{"type":"CommentPreproc","value":"{{"},
5+
{"type":"TextWhitespace","value":" "},
6+
{"type":"NameOther","value":"$myVar"},
7+
{"type":"TextWhitespace","value":" "},
8+
{"type":"Operator","value":":="},
9+
{"type":"TextWhitespace","value":" "},
10+
{"type":"NameOther","value":"2"},
11+
{"type":"TextWhitespace","value":" "},
12+
{"type":"CommentPreproc","value":"}}"},
13+
{"type":"Other","value":"\n"},
14+
{"type":"CommentPreproc","value":"{{"},
15+
{"type":"TextWhitespace","value":" "},
16+
{"type":"NameOther","value":"$myVar"},
17+
{"type":"TextWhitespace","value":" "},
18+
{"type":"Operator","value":"="},
19+
{"type":"TextWhitespace","value":" "},
20+
{"type":"NameOther","value":"4"},
21+
{"type":"TextWhitespace","value":" "},
22+
{"type":"CommentPreproc","value":"}}"}
23+
]
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)