mirror of
https://git.sr.ht/~adnano/kiln
synced 2024-10-30 01:13:08 +00:00
Make templates optional
This commit is contained in:
parent
27684b26f3
commit
f054cdf391
35
dir.go
35
dir.go
|
@ -2,6 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
|
@ -89,22 +90,26 @@ func (d *Dir) Process(cfg *Config, task *Task) error {
|
||||||
if task.TemplateExt != "" {
|
if task.TemplateExt != "" {
|
||||||
// Create index
|
// Create index
|
||||||
if d.index != nil {
|
if d.index != nil {
|
||||||
var b strings.Builder
|
tmpl, ok := cfg.Templates.FindTemplate(d.Path, "index"+task.TemplateExt)
|
||||||
tmpl := cfg.Templates.FindTemplate(d.Path, "index"+task.TemplateExt)
|
if ok {
|
||||||
if err := tmpl.Execute(&b, d); err != nil {
|
var b strings.Builder
|
||||||
return err
|
if err := tmpl.Execute(&b, d); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
d.index.Content = b.String()
|
||||||
}
|
}
|
||||||
d.index.Content = b.String()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process pages
|
// Process pages
|
||||||
for i := range d.Pages {
|
for i := range d.Pages {
|
||||||
var b strings.Builder
|
var b strings.Builder
|
||||||
tmpl := cfg.Templates.FindTemplate(d.Path, "page"+task.TemplateExt)
|
tmpl, ok := cfg.Templates.FindTemplate(d.Path, "page"+task.TemplateExt)
|
||||||
if err := tmpl.Execute(&b, d.Pages[i]); err != nil {
|
if ok {
|
||||||
return err
|
if err := tmpl.Execute(&b, d.Pages[i]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
d.Pages[i].Content = b.String()
|
||||||
}
|
}
|
||||||
d.Pages[i].Content = b.String()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -125,11 +130,15 @@ func (d *Dir) Process(cfg *Config, task *Task) error {
|
||||||
Updated: time.Now(),
|
Updated: time.Now(),
|
||||||
Entries: d.Pages,
|
Entries: d.Pages,
|
||||||
}
|
}
|
||||||
tmpl := cfg.Templates.FindTemplate(d.Path, "atom.xml")
|
tmpl, ok := cfg.Templates.FindTemplate(d.Path, "atom.xml")
|
||||||
if err := tmpl.Execute(&b, feed); err != nil {
|
if ok {
|
||||||
return err
|
if err := tmpl.Execute(&b, feed); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
d.feed = b.Bytes()
|
||||||
|
} else {
|
||||||
|
fmt.Printf("Warning: failed to generate feed %q: missing template \"atom.xml\"\n", title)
|
||||||
}
|
}
|
||||||
d.feed = b.Bytes()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process subdirectories
|
// Process subdirectories
|
||||||
|
|
13
templates.go
13
templates.go
|
@ -2,7 +2,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
pathpkg "path"
|
pathpkg "path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
@ -56,15 +55,15 @@ func (t *Templates) Load(dir string) error {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindTemplate returns a template for the given path, or the default template if it does not exist.
|
// FindTemplate returns the template for the given path.
|
||||||
func (t *Templates) FindTemplate(path string, tmpl string) *template.Template {
|
func (t *Templates) FindTemplate(path string, tmpl string) (*template.Template, bool) {
|
||||||
tmplPath := pathpkg.Join(path, tmpl)
|
tmplPath := pathpkg.Join(path, tmpl)
|
||||||
if t, ok := t.tmpls[tmplPath]; ok {
|
if t, ok := t.tmpls[tmplPath]; ok {
|
||||||
return t
|
return t, true
|
||||||
}
|
}
|
||||||
if t, ok := t.tmpls[pathpkg.Join("/_default", tmpl)]; ok {
|
if t, ok := t.tmpls[pathpkg.Join("/_default", tmpl)]; ok {
|
||||||
return t
|
return t, true
|
||||||
}
|
}
|
||||||
log.Fatalf("failed to find template %q", tmpl)
|
// Failed to find template
|
||||||
return nil
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue