mirror of
https://git.sr.ht/~adnano/kiln
synced 2024-12-29 06:43:45 +00:00
Search for templates recursively
This commit is contained in:
parent
919cdd7556
commit
e662a70990
31
main.go
31
main.go
|
@ -22,11 +22,11 @@ const (
|
|||
htmlDst = "dst.html" // html destination
|
||||
indexPath = "index.gmi" // path used for index files
|
||||
atomPath = "atom.xml" // path used for atom feeds
|
||||
tmplDir = "templates/" // templates directory
|
||||
atomTmpl = "atom.xml" // path to atom template
|
||||
indexTmpl = "index.gmi" // path to index template
|
||||
pageTmpl = "page.gmi" // path to page template
|
||||
htmlTmpl = "output.html" // path to html template
|
||||
tmplDir = "templates" // templates directory
|
||||
atomTmpl = "/atom.xml" // path to atom template
|
||||
indexTmpl = "/index.gmi" // path to index template
|
||||
pageTmpl = "/page.gmi" // path to page template
|
||||
htmlTmpl = "/output.html" // path to html template
|
||||
)
|
||||
|
||||
// site config
|
||||
|
@ -83,6 +83,23 @@ func loadTemplate(path string, content string) {
|
|||
templates[path] = tmpl
|
||||
}
|
||||
|
||||
// findTemplate searches recursively for a template for the given path.
|
||||
func findTemplate(path string, tmpl string) *template.Template {
|
||||
for {
|
||||
tmplPath := filepath.Join(path, tmpl)
|
||||
if t, ok := templates[tmplPath]; ok {
|
||||
return t
|
||||
}
|
||||
slash := path == "/"
|
||||
path = filepath.Dir(path)
|
||||
if slash && path == "/" {
|
||||
break
|
||||
}
|
||||
}
|
||||
// shouldn't happen
|
||||
return nil
|
||||
}
|
||||
|
||||
func run() error {
|
||||
// Load default templates
|
||||
loadTemplate(atomTmpl, atom_xml)
|
||||
|
@ -164,7 +181,7 @@ func manipulate(dir *Dir) error {
|
|||
// Write the directory index file, if it doesn't exist
|
||||
if dir.index == nil {
|
||||
var b bytes.Buffer
|
||||
tmpl := templates[indexTmpl]
|
||||
tmpl := findTemplate(dir.Permalink, indexTmpl)
|
||||
if err := tmpl.Execute(&b, dir); err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -176,7 +193,7 @@ func manipulate(dir *Dir) error {
|
|||
// Manipulate pages
|
||||
for i := range dir.Pages {
|
||||
var b bytes.Buffer
|
||||
tmpl := templates[pageTmpl]
|
||||
tmpl := findTemplate(dir.Pages[i].Permalink, pageTmpl)
|
||||
if err := tmpl.Execute(&b, dir.Pages[i]); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue