mirror of
https://git.sr.ht/~adnano/kiln
synced 2024-11-27 20:14:20 +00:00
Embed built-in templates with embed.FS
This commit is contained in:
parent
6ab9313c6d
commit
7ee6591e9c
68
templates.go
68
templates.go
|
@ -1,6 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"embed"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
pathpkg "path"
|
pathpkg "path"
|
||||||
|
@ -9,6 +10,9 @@ import (
|
||||||
"text/template"
|
"text/template"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
//go:embed templates
|
||||||
|
var builtinTemplates embed.FS
|
||||||
|
|
||||||
// Templates contains site templates.
|
// Templates contains site templates.
|
||||||
type Templates struct {
|
type Templates struct {
|
||||||
tmpls map[string]*template.Template
|
tmpls map[string]*template.Template
|
||||||
|
@ -31,17 +35,25 @@ func (t *Templates) Funcs(funcs template.FuncMap) {
|
||||||
// LoadDefault loads the default templates.
|
// LoadDefault loads the default templates.
|
||||||
// Should be called after Funcs.
|
// Should be called after Funcs.
|
||||||
func (t *Templates) LoadDefault() {
|
func (t *Templates) LoadDefault() {
|
||||||
t.LoadTemplate("/_default/index.gmi", index_gmi)
|
t.loadDefault("index.gmi")
|
||||||
t.LoadTemplate("/_default/page.gmi", page_gmi)
|
t.loadDefault("page.gmi")
|
||||||
t.LoadTemplate("/_default/atom.xml", atom_xml)
|
t.loadDefault("atom.xml")
|
||||||
t.LoadTemplate("/_default/output.html", output_html)
|
t.loadDefault("output.html")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Templates) loadDefault(name string) {
|
||||||
|
b, err := builtinTemplates.ReadFile("templates/" + name)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
t.LoadTemplate("/_default/"+name, b)
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadTemplate loads a template from the provided path and content.
|
// LoadTemplate loads a template from the provided path and content.
|
||||||
func (t *Templates) LoadTemplate(path string, content string) {
|
func (t *Templates) LoadTemplate(path string, content []byte) {
|
||||||
tmpl := template.New(path)
|
tmpl := template.New(path)
|
||||||
tmpl.Funcs(t.funcs)
|
tmpl.Funcs(t.funcs)
|
||||||
template.Must(tmpl.Parse(content))
|
template.Must(tmpl.Parse(string(content)))
|
||||||
t.tmpls[path] = tmpl
|
t.tmpls[path] = tmpl
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,7 +70,7 @@ func (t *Templates) Load(dir string) error {
|
||||||
}
|
}
|
||||||
// Remove directory from beginning of path
|
// Remove directory from beginning of path
|
||||||
path = strings.TrimPrefix(path, dir)
|
path = strings.TrimPrefix(path, dir)
|
||||||
t.LoadTemplate(path, string(b))
|
t.LoadTemplate(path, b)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
@ -72,45 +84,3 @@ func (t *Templates) FindTemplate(path string, tmpl string) *template.Template {
|
||||||
}
|
}
|
||||||
return t.tmpls[pathpkg.Join("/_default", tmpl)]
|
return t.tmpls[pathpkg.Join("/_default", tmpl)]
|
||||||
}
|
}
|
||||||
|
|
||||||
// Default index template
|
|
||||||
const index_gmi = `# {{ .Title }}
|
|
||||||
{{ if .Content }}
|
|
||||||
{{ .Content }}{{ end }}
|
|
||||||
{{ range .Pages }}=> {{ .Path }} {{ if not .Date.IsZero -}}
|
|
||||||
{{.Date.Format "2006-01-02"}} {{end}}{{.Title}}
|
|
||||||
{{ end -}}`
|
|
||||||
|
|
||||||
// Default page template
|
|
||||||
const page_gmi = `# {{ .Title }}
|
|
||||||
{{- if not .Date.IsZero }}
|
|
||||||
Posted on {{ .Date.Format "2006-01-02" }}{{ end }}
|
|
||||||
|
|
||||||
{{ .Content }}`
|
|
||||||
|
|
||||||
// Default template for html output
|
|
||||||
const output_html = `<!DOCTYPE html>
|
|
||||||
<meta charset="utf-8">
|
|
||||||
<meta name="viewport" content="width=device-width,initial-scale=1">
|
|
||||||
<title>{{ .Title }}</title>
|
|
||||||
|
|
||||||
{{ .Content }}`
|
|
||||||
|
|
||||||
// Default atom feed template
|
|
||||||
const atom_xml = `<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<feed xmlns="http://www.w3.org/2005/Atom">
|
|
||||||
<id>{{ index site.URLs 0 }}{{ .Path }}</id>
|
|
||||||
<title>{{ .Title }}</title>
|
|
||||||
<updated>{{ .Updated.Format "2006-01-02T15:04:05Z07:00" }}</updated>
|
|
||||||
<link href="{{ index site.URLs 0 }}{{ .Path }}" rel="alternate"/>
|
|
||||||
{{ range .Entries }}<entry>
|
|
||||||
<id>{{ index site.URLs 0 }}{{ .Path }}</id>
|
|
||||||
<title>{{ .Title }}</title>
|
|
||||||
<updated>{{ .Date.Format "2006-01-02T15:04:05Z07:00" }}</updated>
|
|
||||||
{{- $path := .Path }}
|
|
||||||
{{- range site.URLs }}
|
|
||||||
<link href="{{ . }}{{ $path }}" rel="alternate"/>
|
|
||||||
{{- end }}
|
|
||||||
</entry>
|
|
||||||
{{ end -}}
|
|
||||||
</feed>`
|
|
||||||
|
|
Loading…
Reference in a new issue