dati

A Go library/binary to parse & execute data against template langauges.
git clone git://src.gearsix.net/dati
Log | Files | Refs | Atom | README | LICENSE

commit 802cafa6d4dd7fbcc0017e6b371c2a40c8faf7b9
parent 26fe1b7de491af91b5a1a0ed1c466244c4f07dca
Author: gearsix <gearsix@tuta.io>
Date:   Sat, 27 May 2023 13:49:05 +0100

template: changes 'hmpl' extension to 'htmpl'

Diffstat:
Mtemplate.go | 31+++++++++++++------------------
Mtemplate_test.go | 60++++++++++++++++++++++++++++++------------------------------
2 files changed, 43 insertions(+), 48 deletions(-)

diff --git a/template.go b/template.go @@ -21,7 +21,7 @@ import ( "bytes" "errors" "fmt" - hmpl "html/template" + htmpl "html/template" "io" "io/ioutil" "os" @@ -42,9 +42,9 @@ func (t TemplateLanguage) String() string { } const ( - TMPL TemplateLanguage = "tmpl" - HMPL TemplateLanguage = "hmpl" - MST TemplateLanguage = "mst" + TMPL TemplateLanguage = "tmpl" + HTMPL TemplateLanguage = "htmpl" + MST TemplateLanguage = "mst" ) var ( @@ -76,7 +76,7 @@ func ReadTemplateLangauge(path string) TemplateLanguage { ext := filepath.Ext(path) if len(ext) == 0 { - ext = path // assume `path` the name of the format + ext = path // assume `path` is the name of the format } ext = strings.ToLower(ext) @@ -84,7 +84,7 @@ func ReadTemplateLangauge(path string) TemplateLanguage { ext = ext[1:] } - for _, fmt := range []TemplateLanguage{TMPL, HMPL, MST} { + for _, fmt := range []TemplateLanguage{TMPL, HTMPL, MST} { if fmt.String() == ext { return fmt } @@ -92,10 +92,6 @@ func ReadTemplateLangauge(path string) TemplateLanguage { return "" } -func getTemplateType(path string) string { - return strings.TrimPrefix(filepath.Ext(path), ".") -} - // Template is a wrapper to interface with any template parsed by dati. // Ideally it would have just been an interface{} that defines Execute but // the libaries being used aren't that uniform. @@ -137,8 +133,8 @@ func (t *Template) Execute(data interface{}) (result bytes.Buffer, err error) { // ExecuteToFile writes the result of `(*Template).Execute(data)` to the file at `path` (if no errors occurred). // If `force` is true, any existing file at `path` will be overwritten. -func (t *Template)ExecuteToFile(data interface{}, path string, force bool) (f *os.File, err error) { - if f, err := os.Open(path); os.IsNotExist(err) { +func (t *Template) ExecuteToFile(data interface{}, path string, force bool) (f *os.File, err error) { + if f, err = os.Open(path); os.IsNotExist(err) { f, err = os.Create(path) } else if !force { err = os.ErrExist @@ -229,8 +225,8 @@ func LoadTemplate(lang TemplateLanguage, rootName string, root io.Reader, partia switch TemplateLanguage(lang) { case TMPL: t.T, err = loadTemplateTmpl(rootName, root, partials) - case HMPL: - t.T, err = loadTemplateHmpl(rootName, root, partials) + case HTMPL: + t.T, err = loadTemplateHtmpl(rootName, root, partials) case MST: t.T, err = loadTemplateMst(rootName, root, partials) default: @@ -260,12 +256,12 @@ func loadTemplateTmpl(rootName string, root io.Reader, partials map[string]io.Re return template, nil } -func loadTemplateHmpl(rootName string, root io.Reader, partials map[string]io.Reader) (*hmpl.Template, error) { - var template *hmpl.Template +func loadTemplateHtmpl(rootName string, root io.Reader, partials map[string]io.Reader) (*htmpl.Template, error) { + var template *htmpl.Template if buf, err := ioutil.ReadAll(root); err != nil { return nil, err - } else if template, err = hmpl.New(rootName).Parse(string(buf)); err != nil { + } else if template, err = htmpl.New(rootName).Parse(string(buf)); err != nil { return nil, err } @@ -301,4 +297,3 @@ func loadTemplateMst(rootName string, root io.Reader, partials map[string]io.Rea return template, nil } - diff --git a/template_test.go b/template_test.go @@ -33,11 +33,11 @@ const tmplResult = `0 0` const tmplRootBad = `{{ example }}} {{{ template \"tmplPartialBad\" . }}` const tmplPartialBad = `{{{ .example }}` -const hmplRootGood = `<!DOCTYPE html><html><p>{{.eg}} {{ template "hmplPartialGood" . }}</p></html>` -const hmplPartialGood = `<b>{{.eg}}</b>` -const hmplResult = `<!DOCTYPE html><html><p>0 <b>0</b></p></html>` -const hmplRootBad = `{{ example }} {{{ template "hmplPartialBad" . }}` -const hmplPartialBad = `<b>{{{ .example2 }}</b>` +const htmplRootGood = `<!DOCTYPE html><html><p>{{.eg}} {{ template "htmplPartialGood" . }}</p></html>` +const htmplPartialGood = `<b>{{.eg}}</b>` +const htmplResult = `<!DOCTYPE html><html><p>0 <b>0</b></p></html>` +const htmplRootBad = `{{ example }} {{{ template "htmplPartialBad" . }}` +const htmplPartialBad = `<b>{{{ .example2 }}</b>` const mstRootGood = `{{eg}} {{> mstPartialGood}}` const mstPartialGood = `{{eg}}` @@ -47,7 +47,7 @@ const mstPartialBad = `p{{$}}{{ > noexist}` var templateExts = []string{ ".tmpl", "tmpl", "TMPL", ".TMPL", - ".hmpl", "hmpl", "HMPL", ".HMPL", + ".htmpl", "htmpl", "HTMPL", ".HTMPL", ".mst", "mst", "MST", ".MST", ".NONE", "-", ".", "", } @@ -74,7 +74,7 @@ func TestReadTemplateLanguage(t *testing.T) { if i < 4 { target = TMPL } else if i < 8 { - target = HMPL + target = HTMPL } else if i < 12 { target = MST } else { @@ -93,9 +93,9 @@ func TestReadTemplateLanguage(t *testing.T) { func validateTemplate(t *testing.T, template Template, templateType string, rootName string, partialNames ...string) { types := map[string]string{ - "tmpl": "*template.Template", - "hmpl": "*template.Template", - "mst": "*mustache.Template", + "tmpl": "*template.Template", + "htmpl": "*template.Template", + "mst": "*mustache.Template", } rt := reflect.TypeOf(template.T).String() @@ -121,7 +121,7 @@ func validateTemplate(t *testing.T, template Template, templateType string, root } func validateTemplateFile(t *testing.T, template Template, rootPath string, partialPaths ...string) { - rType := getTemplateType(rootPath) + rType := ReadTemplateLangauge(rootPath) rName := filepath.Base(rootPath) if rType == "mst" { rName = strings.TrimSuffix(rName, filepath.Ext(rName)) @@ -135,7 +135,7 @@ func validateTemplateFile(t *testing.T, template Template, rootPath string, part pNames = append(pNames, name) } - validateTemplate(t, template, rType, rName, pNames...) + validateTemplate(t, template, rootPath, rName, pNames...) } func TestLoadTemplateFile(t *testing.T) { @@ -159,14 +159,14 @@ func TestLoadTemplateFile(t *testing.T) { badPartials = append(badRoots, tdir+"/badPartials.tmpl") createFile(badPartials[len(badPartials)-1], tmplPartialBad) - goodRoots = append(goodRoots, tdir+"/goodRoot.hmpl") - createFile(goodRoots[len(goodRoots)-1], hmplRootGood) - goodPartials = append(goodPartials, tdir+"/goodPartial.hmpl") - createFile(goodPartials[len(goodPartials)-1], hmplPartialGood) - badRoots = append(badRoots, tdir+"/badRoot.hmpl") - createFile(badRoots[len(badRoots)-1], hmplRootBad) - badPartials = append(badRoots, tdir+"/badPartials.hmpl") - createFile(badPartials[len(badPartials)-1], hmplPartialBad) + goodRoots = append(goodRoots, tdir+"/goodRoot.htmpl") + createFile(goodRoots[len(goodRoots)-1], htmplRootGood) + goodPartials = append(goodPartials, tdir+"/goodPartial.htmpl") + createFile(goodPartials[len(goodPartials)-1], htmplPartialGood) + badRoots = append(badRoots, tdir+"/badRoot.htmpl") + createFile(badRoots[len(badRoots)-1], htmplRootBad) + badPartials = append(badRoots, tdir+"/badPartials.htmpl") + createFile(badPartials[len(badPartials)-1], htmplPartialBad) goodRoots = append(goodRoots, tdir+"/goodRoot.mst") createFile(goodRoots[len(goodRoots)-1], mstRootGood) @@ -223,17 +223,17 @@ func TestLoadTemplateString(t *testing.T) { testInvalid(templateType, template) } - templateType = "hmpl" - if template, err = LoadTemplateString(templateType, name, hmplRootGood, - map[string]string{"hmplPartialGood": hmplPartialGood}); err != nil { + templateType = "htmpl" + if template, err = LoadTemplateString(templateType, name, htmplRootGood, + map[string]string{"htmplPartialGood": htmplPartialGood}); err != nil { t.Fatalf("'%s' template failed to load", templateType) } - if template, err = LoadTemplateString(templateType, name, hmplRootBad, - map[string]string{"hmplPartialGood": hmplPartialGood}); err == nil { + if template, err = LoadTemplateString(templateType, name, htmplRootBad, + map[string]string{"htmplPartialGood": htmplPartialGood}); err == nil { testInvalid(templateType, template) } - if template, err = LoadTemplateString(templateType, name, hmplRootGood, - map[string]string{"hmplPartialGood": hmplPartialBad}); err == nil { + if template, err = LoadTemplateString(templateType, name, htmplRootGood, + map[string]string{"htmplPartialGood": htmplPartialBad}); err == nil { testInvalid(templateType, template) } @@ -280,15 +280,15 @@ func TestExecute(t *testing.T) { results, err = tmpl.Execute(data) validateExecute(t, results.String(), tmplResult, err) - if tmpl, err = LoadTemplateString("hmpl", "hmplRootGood", hmplRootGood, - map[string]string{"hmplPartialGood": hmplPartialGood}); err != nil { + if tmpl, err = LoadTemplateString("htmpl", "htmplRootGood", htmplRootGood, + map[string]string{"htmplPartialGood": htmplPartialGood}); err != nil { t.Skip("setup failure:", err) } if err = LoadData("yaml", strings.NewReader(good["yaml"]), &data); err != nil { t.Skip("setup failure:", err) } results, err = tmpl.Execute(data) - validateExecute(t, results.String(), hmplResult, err) + validateExecute(t, results.String(), htmplResult, err) if tmpl, err = LoadTemplateString("mst", "mstRootGood", mstRootGood, map[string]string{"mstPartialGood": mstPartialGood}); err != nil {