Skip to content

Commit

Permalink
Improved support for Go templates (#401)
Browse files Browse the repository at this point in the history
  • Loading branch information
cmaster11 committed Sep 22, 2020
1 parent 290ff86 commit 6665753
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 11 deletions.
20 changes: 10 additions & 10 deletions lexers/g/go.go
Expand Up @@ -60,13 +60,13 @@ var Go = internal.Register(MustNewLexer(

var goTemplateRules = Rules{
"root": {
{`{{(- )?/\*(.|\n)*?\*/( -)?}}`, CommentMultiline, nil},
{`{{[-]?`, CommentPreproc, Push("template")},
{`[^{]+`, Other, nil},
{`{`, Other, nil},
},
"template": {
{`[-]?}}`, CommentPreproc, Pop(1)},
{`/\*.*?\*/`, Comment, nil},
{`(?=}})`, CommentPreproc, Pop(1)}, // Terminate the pipeline
{`\(`, Operator, Push("subexpression")},
{`"(\\\\|\\"|[^"])*"`, LiteralString, nil},
Expand All @@ -80,19 +80,19 @@ var goTemplateRules = Rules{
{`\s+`, Whitespace, nil},
{`\(`, Operator, Push("subexpression")},
{`(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},
{`\||:=`, Operator, nil},
{`\||:?=`, Operator, nil},
{`[$]?[^\W\d]\w*`, NameOther, nil},
{`[$]?\.(?:[^\W\d]\w*)?`, NameAttribute, nil},
{`"(\\\\|\\"|[^"])*"`, LiteralString, nil},
{`\d+i`, LiteralNumber, nil},
{`\d+\.\d*([Ee][-+]\d+)?i`, LiteralNumber, nil},
{`-?\d+i`, LiteralNumber, nil},
{`-?\d+\.\d*([Ee][-+]\d+)?i`, LiteralNumber, nil},
{`\.\d+([Ee][-+]\d+)?i`, LiteralNumber, nil},
{`\d+[Ee][-+]\d+i`, LiteralNumber, nil},
{`\d+(\.\d+[eE][+\-]?\d+|\.\d*|[eE][+\-]?\d+)`, LiteralNumberFloat, nil},
{`\.\d+([eE][+\-]?\d+)?`, LiteralNumberFloat, nil},
{`0[0-7]+`, LiteralNumberOct, nil},
{`0[xX][0-9a-fA-F]+`, LiteralNumberHex, nil},
{`(0|[1-9][0-9]*)`, LiteralNumberInteger, nil},
{`-?\d+[Ee][-+]\d+i`, LiteralNumber, nil},
{`-?\d+(\.\d+[eE][+\-]?\d+|\.\d*|[eE][+\-]?\d+)`, LiteralNumberFloat, nil},
{`-?\.\d+([eE][+\-]?\d+)?`, LiteralNumberFloat, nil},
{`-?0[0-7]+`, LiteralNumberOct, nil},
{`-?0[xX][0-9a-fA-F]+`, LiteralNumberHex, nil},
{`-?(0|[1-9][0-9]*)`, LiteralNumberInteger, nil},
{`'(\\['"\\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},
{"`[^`]*`", LiteralString, nil},
},
Expand Down
72 changes: 72 additions & 0 deletions lexers/g/go_test.go
Expand Up @@ -23,6 +23,9 @@ func TestGoHTMLTemplateIssue126(t *testing.T) {
{{ with .OutputFormats.Get "RSS" }}
{{ printf "<atom:link href=%q rel=\"self\" type=%q />" .Permalink .MediaType | safeHTML }}
{{ end }}
{{/*
Print all pages
*/}}
{{ range .Data.Pages }}
<item>
<title>{{ .Title }}</title>
Expand All @@ -48,3 +51,72 @@ func TestGoHTMLTemplateIssue126(t *testing.T) {
assert.Equal(t, source, chroma.Stringify(tokens...))
}
}

func TestGoHTMLTemplateMultilineComments(t *testing.T) {
for _, source := range []string{
`
{{/*
This is a multiline comment
*/}}
`,
`
{{- /*
This is a multiline comment
*/}}
`,
`
{{/*
This is a multiline comment
*/ -}}
`,
`
{{- /*
This is a multiline comment
*/ -}}
`,
} {
tokens, err := chroma.Tokenise(GoHTMLTemplate, nil, source)
assert.NoError(t, err)
assert.Equal(t, source, chroma.Stringify(tokens...))

// Make sure that there are no errors
for _, token := range tokens {
assert.NotEqual(t, chroma.Error, token.Type)
}

// Make sure that multiline comments are printed
found := false
for _, token := range tokens {
if token.Type == chroma.CommentMultiline {
found = true
}
}
assert.True(t, found)
}
}

func TestGoHTMLTemplateNegativeNumber(t *testing.T) {
for _, source := range []string{
`
{{ fn -3 }}
`,
} {
tokens, err := chroma.Tokenise(GoHTMLTemplate, nil, source)
assert.NoError(t, err)
assert.Equal(t, source, chroma.Stringify(tokens...))

// Make sure that there are no errors
for _, token := range tokens {
assert.NotEqual(t, chroma.Error, token.Type)
}

// Make sure that negative number is found
found := false
for _, token := range tokens {
if token.Type == chroma.LiteralNumberInteger {
found = true
}
}
assert.True(t, found)
}
}
2 changes: 1 addition & 1 deletion lexers/lexers_test.go
Expand Up @@ -55,7 +55,7 @@ func TestLexers(t *testing.T) {
continue
}

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

Expand Down
3 changes: 3 additions & 0 deletions lexers/testdata/go-html-template.actual
@@ -0,0 +1,3 @@
{{/*
This is a multiline comment
*/}}
3 changes: 3 additions & 0 deletions lexers/testdata/go-html-template.expected
@@ -0,0 +1,3 @@
[
{"type":"CommentMultiline","value":"{{/*\n This is a multiline comment\n*/}}"}
]
6 changes: 6 additions & 0 deletions lexers/testdata/go-text-template.actual
@@ -0,0 +1,6 @@
{{/*
This is a multiline comment
*/}}

{{ $myVar := 2 }}
{{ $myVar = 4 }}
23 changes: 23 additions & 0 deletions lexers/testdata/go-text-template.expected
@@ -0,0 +1,23 @@
[
{"type":"CommentMultiline","value":"{{/*\n This is a multiline comment\n*/}}"},
{"type":"Other","value":"\n\n"},
{"type":"CommentPreproc","value":"{{"},
{"type":"TextWhitespace","value":" "},
{"type":"NameOther","value":"$myVar"},
{"type":"TextWhitespace","value":" "},
{"type":"Operator","value":":="},
{"type":"TextWhitespace","value":" "},
{"type":"NameOther","value":"2"},
{"type":"TextWhitespace","value":" "},
{"type":"CommentPreproc","value":"}}"},
{"type":"Other","value":"\n"},
{"type":"CommentPreproc","value":"{{"},
{"type":"TextWhitespace","value":" "},
{"type":"NameOther","value":"$myVar"},
{"type":"TextWhitespace","value":" "},
{"type":"Operator","value":"="},
{"type":"TextWhitespace","value":" "},
{"type":"NameOther","value":"4"},
{"type":"TextWhitespace","value":" "},
{"type":"CommentPreproc","value":"}}"}
]
File renamed without changes.
File renamed without changes.

0 comments on commit 6665753

Please sign in to comment.