82 lines
1.7 KiB
Go
82 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
"html/template"
|
|
"log"
|
|
"os"
|
|
"path"
|
|
"strings"
|
|
)
|
|
|
|
func main() {
|
|
always := []string{}
|
|
if err := recursePwd(func(p string) error {
|
|
switch path.Ext(p) {
|
|
case ".ctmpl":
|
|
if path.Base(p)[0] == '_' {
|
|
always = append(always, p)
|
|
}
|
|
}
|
|
return nil
|
|
}); err != nil {
|
|
panic(err)
|
|
}
|
|
t := func(p ...string) *template.Template {
|
|
p = append(always, p...)
|
|
oneT, err := template.ParseFiles(p...)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return oneT
|
|
}
|
|
data := map[string]interface{}{
|
|
"Title": "my title here",
|
|
"ID": "my-id-here",
|
|
"PID": "my-pid-here",
|
|
"PTitle": "my parent title here",
|
|
}
|
|
if err := recursePwd(func(p string) error {
|
|
switch path.Ext(p) {
|
|
case ".ctmpl":
|
|
target := path.Join(path.Dir(p), "."+path.Base(p)+".html")
|
|
f, err := os.Create(path.Join(path.Dir(p), "."+path.Base(p)+".html"))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer f.Close()
|
|
templateToExecute := strings.TrimSuffix(path.Base(p), path.Ext(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)
|
|
}
|
|
return nil
|
|
}); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
func recursePwd(foo func(string) error) error {
|
|
wd, err := os.Getwd()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return recurseD(wd, foo)
|
|
}
|
|
|
|
func recurseD(d string, foo func(string) error) error {
|
|
entries, err := os.ReadDir(d)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, entry := range entries {
|
|
if entry.IsDir() {
|
|
if err := recurseD(path.Join(d, entry.Name()), foo); err != nil {
|
|
return err
|
|
}
|
|
} else if strings.HasPrefix(entry.Name(), ".") {
|
|
} else if err := foo(path.Join(d, entry.Name())); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|