diff --git a/main.go b/main.go index 493c59d..977bbbd 100644 --- a/main.go +++ b/main.go @@ -17,16 +17,16 @@ import ( ) const ( - srcDir = "src" // site source - dstDir = "dst" // site destination - 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 + srcDir = "src" // site source + dstDir = "dst" // site destination + 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 ) // 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 }