Skip to content

Commit

Permalink
feat: support go template headers
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed Jul 26, 2024
1 parent a4d7faf commit 9e842f2
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
55 changes: 54 additions & 1 deletion template.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gomplate

import (
"bufio"
"bytes"
"context"
"fmt"
Expand Down Expand Up @@ -203,6 +204,11 @@ func goTemplate(template Template, environment map[string]any) (string, error) {
}

if tpl == nil {
template, err := parseAndStripTemplateHeader(template)
if err != nil {
return "", err
}

tpl = gotemplate.New("")
if template.LeftDelim != "" {
tpl = tpl.Delims(template.LeftDelim, template.RightDelim)
Expand All @@ -215,7 +221,7 @@ func goTemplate(template Template, environment map[string]any) (string, error) {
for k, v := range template.Functions {
funcs[k] = v
}
var err error

tpl, err = tpl.Funcs(funcs).Parse(template.Template)
if err != nil {
return "", err
Expand Down Expand Up @@ -249,3 +255,50 @@ func LoadSharedLibrary(source string) error {
registry.Register(func() string { return string(data) })
return nil
}

func parseAndStripTemplateHeader(template Template) (Template, error) {
fm, content := extractFrontmatterAndContent(template.Template)
if fm == "" {
return template, nil
}

template.Template = content
scanner := bufio.NewScanner(strings.NewReader(fm))
for scanner.Scan() {
line := scanner.Text()
split := strings.SplitN(line, "=", 2)
if len(split) != 2 {
return template, fmt.Errorf("invalid header: %s", line)
}

switch split[0] {
case "right-delim":
template.RightDelim = split[1]
case "left-delim":
template.LeftDelim = split[1]
}
}

return template, nil
}

func extractFrontmatterAndContent(input string) (string, string) {
const delimiter = "---"

input = strings.TrimSpace(input)

if !strings.HasPrefix(input, delimiter) {
return "", input
}

endIndex := strings.Index(input[len(delimiter):], delimiter)
if endIndex == -1 {
return "", input
}

frontmatterEndIndex := endIndex + 2*len(delimiter)
frontmatter := strings.TrimSpace(input[len(delimiter) : endIndex+len(delimiter)])
content := strings.TrimSpace(input[frontmatterEndIndex:])

return frontmatter, content
}
28 changes: 28 additions & 0 deletions tests/gomplate_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tests

import (
"os"
"testing"
"time"

Expand Down Expand Up @@ -80,3 +81,30 @@ func TestGomplate(t *testing.T) {
})
}
}

func TestGomplateHeaders(t *testing.T) {
tests := []struct {
env map[string]interface{}
template string
out string
}{
{map[string]interface{}{"name": "world"}, readFile(t, "testdata/template-header.txt"), "Hello, world"},
}

for _, tc := range tests {
t.Run(tc.template, func(t *testing.T) {
out, err := gomplate.RunTemplate(tc.env, gomplate.Template{
Template: tc.template,
})
assert.ErrorIs(t, err, nil)
assert.Equal(t, tc.out, out)
})
}
}

func readFile(t *testing.T, filename string) string {
t.Helper()
data, err := os.ReadFile(filename)
assert.ErrorIs(t, err, nil)
return string(data)
}
5 changes: 5 additions & 0 deletions tests/testdata/template-header.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
left-delim=$[[
right-delim=]]
---
Hello, $[[.name]]

0 comments on commit 9e842f2

Please sign in to comment.