Skip to content

Commit

Permalink
refactor: generator
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy committed Feb 7, 2022
1 parent 6434658 commit 1585395
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions 4_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"io/ioutil"
"regexp"

"github.com/Masterminds/sprig/v3"
sprig "github.com/Masterminds/sprig/v3"

"github.com/pkg/errors"
"github.com/release-lab/whatchanged/internal/client"
Expand Down Expand Up @@ -191,20 +191,23 @@ func GenerateFromContext(g *client.GitClient, contexts []*TemplateContext, forma
}
}

regexCache := make(map[string]*regexp.Regexp)

for _, ctx := range contexts {
t := template.New(ctx.Version)

// commom function for sprig
t.Funcs(sprig.FuncMap())
// overwrite regex function with cached one for better performance.
t.Funcs(getRegexFuncs(regexCache))

// custom function
t.Funcs(template.FuncMap{
"unescape": func(s string) template.HTML {
return template.HTML(s)
},
})

// Overwrite regex function with cached one for better performance.
regexCache := make(map[string]*regexp.Regexp)
t.Funcs(getRegexFuncs(regexCache))

if t, err := t.Parse(templateStr); err != nil {
return nil, errors.WithStack(err)
} else {
Expand Down

0 comments on commit 1585395

Please sign in to comment.