include css in test files

master
Bel LaPointe 2022-02-14 08:49:46 -07:00
parent c21a6a5b3b
commit d1e1991fe4
1 changed files with 19 additions and 3 deletions

View File

@ -1,6 +1,7 @@
package main
import (
"fmt"
"html/template"
"log"
"os"
@ -9,20 +10,25 @@ import (
)
func main() {
all := []string{}
always := []string{}
if err := recursePwd(func(p string) error {
switch path.Ext(p) {
case ".ctmpl":
if path.Base(p)[0] == '_' {
always = append(always, p)
all = append(all, p)
}
}
switch path.Base(p) {
case "_import.ctmpl":
always = append(always, strings.TrimSuffix(path.Base(p), path.Ext(p)))
}
return nil
}); err != nil {
panic(err)
}
t := func(p ...string) *template.Template {
p = append(always, p...)
p = append(all, p...)
oneT, err := template.ParseFiles(p...)
if err != nil {
panic(err)
@ -45,8 +51,18 @@ func main() {
}
defer f.Close()
templateToExecute := strings.TrimSuffix(path.Base(p), path.Ext(p))
tmpl := t(p)
defer log.Printf("rendering %s (...%s) as %s", templateToExecute, path.Join(path.Base(path.Dir(p)), path.Base(p)), target)
return t(p).Lookup(templateToExecute).Execute(f, data)
if strings.HasPrefix(templateToExecute, "_") {
testTemplate := `{{ define "test" }}`
for _, subtemplate := range always {
testTemplate += fmt.Sprintf(`{{ template %q . }}`, subtemplate)
}
testTemplate += fmt.Sprintf(`{{ template %q . }}{{ end }}`, templateToExecute)
tmpl = template.Must(tmpl.Parse(testTemplate))
templateToExecute = "test"
}
return tmpl.Lookup(templateToExecute).Execute(f, data)
}
return nil
}); err != nil {