2020-09-29 20:42:27 +00:00
|
|
|
package main
|
|
|
|
|
2020-11-20 17:07:38 +00:00
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2020-11-22 20:51:07 +00:00
|
|
|
pathpkg "path"
|
2020-11-20 17:07:38 +00:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"text/template"
|
|
|
|
)
|
2020-11-11 00:33:45 +00:00
|
|
|
|
2020-11-20 17:07:38 +00:00
|
|
|
// Templates contains site templates.
|
|
|
|
type Templates struct {
|
|
|
|
tmpls map[string]*template.Template
|
|
|
|
funcs template.FuncMap
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewTemplates returns a new Templates with the default templates.
|
|
|
|
func NewTemplates() *Templates {
|
|
|
|
t := &Templates{
|
|
|
|
tmpls: map[string]*template.Template{},
|
|
|
|
}
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
// Funcs sets the functions available to newly created templates.
|
|
|
|
func (t *Templates) Funcs(funcs template.FuncMap) {
|
|
|
|
t.funcs = funcs
|
|
|
|
}
|
|
|
|
|
2020-11-22 20:14:50 +00:00
|
|
|
// LoadDefault loads the default templates.
|
|
|
|
// Should be called after Funcs.
|
|
|
|
func (t *Templates) LoadDefault() {
|
2020-11-22 20:51:07 +00:00
|
|
|
t.LoadTemplate("/_default/index.gmi", index_gmi)
|
|
|
|
t.LoadTemplate("/_default/page.gmi", page_gmi)
|
|
|
|
t.LoadTemplate("/_default/atom.xml", atom_xml)
|
|
|
|
t.LoadTemplate("/_default/output.html", output_html)
|
2020-11-22 20:14:50 +00:00
|
|
|
}
|
|
|
|
|
2020-11-20 17:07:38 +00:00
|
|
|
// LoadTemplate loads a template from the provided path and content.
|
|
|
|
func (t *Templates) LoadTemplate(path string, content string) {
|
|
|
|
tmpl := template.New(path)
|
|
|
|
tmpl.Funcs(t.funcs)
|
|
|
|
template.Must(tmpl.Parse(content))
|
|
|
|
t.tmpls[path] = tmpl
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load loads templates from the provided directory
|
|
|
|
func (t *Templates) Load(dir string) error {
|
|
|
|
return filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if info.Mode().IsRegular() {
|
|
|
|
b, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Remove directory from beginning of path
|
|
|
|
path = strings.TrimPrefix(path, dir)
|
|
|
|
t.LoadTemplate(path, string(b))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
2020-09-29 20:42:27 +00:00
|
|
|
|
2020-11-22 20:51:07 +00:00
|
|
|
// FindTemplate returns a template for the given path, or the default template if it does not exist.
|
2020-11-20 17:07:38 +00:00
|
|
|
func (t *Templates) FindTemplate(path string, tmpl string) *template.Template {
|
2020-11-22 20:51:07 +00:00
|
|
|
tmplPath := pathpkg.Join(path, tmpl)
|
|
|
|
if t, ok := t.tmpls[tmplPath]; ok {
|
|
|
|
return t
|
2020-11-20 17:07:38 +00:00
|
|
|
}
|
2020-11-22 20:51:07 +00:00
|
|
|
return t.tmpls[pathpkg.Join("/_default", tmpl)]
|
2020-11-20 17:07:38 +00:00
|
|
|
}
|
2020-09-29 20:42:27 +00:00
|
|
|
|
2020-11-20 17:07:38 +00:00
|
|
|
// Default index template
|
2020-11-22 20:14:50 +00:00
|
|
|
const index_gmi = `# {{ .Title }}
|
|
|
|
{{ if .Content }}
|
|
|
|
{{ .Content }}{{ end }}
|
|
|
|
{{ range .Pages }}=> {{ .Path }} {{ if not .Date.IsZero -}}
|
|
|
|
{{.Date.Format "2006-01-02"}} {{end}}{{.Title}}
|
2020-09-30 01:30:50 +00:00
|
|
|
{{ end -}}`
|
2020-09-29 20:42:27 +00:00
|
|
|
|
2020-10-01 01:57:59 +00:00
|
|
|
// Default page template
|
2020-09-29 20:42:27 +00:00
|
|
|
const page_gmi = `# {{ .Title }}
|
2020-11-22 20:14:50 +00:00
|
|
|
{{- if not .Date.IsZero }}
|
2020-11-25 01:09:55 +00:00
|
|
|
Posted on {{ .Date.Format "2006-01-02" }}{{ end }}
|
2020-09-29 20:42:27 +00:00
|
|
|
|
|
|
|
{{ .Content }}`
|
|
|
|
|
2020-10-01 01:57:59 +00:00
|
|
|
// 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 }}`
|
2020-11-22 20:14:50 +00:00
|
|
|
|
|
|
|
// Default atom feed template
|
|
|
|
const atom_xml = `<?xml version="1.0" encoding="utf-8"?>
|
|
|
|
<feed xmlns="http://www.w3.org/2005/Atom">
|
|
|
|
<id>{{ site.URL }}{{ .Path }}</id>
|
|
|
|
<title>{{ .Title }}</title>
|
|
|
|
<updated>{{ .Updated.Format "2006-01-02T15:04:05Z07:00" }}</updated>
|
|
|
|
<link href="{{ site.URL }}{{ .Path }}" rel="alternate">
|
|
|
|
{{ range .Entries }}<entry>
|
|
|
|
<id>{{ site.URL }}{{ .Path }}</id>
|
|
|
|
<title>{{ .Title }}</title>
|
|
|
|
<updated>{{ .Date.Format "2006-01-02T15:04:05Z07:00" }}</updated>
|
|
|
|
<link href="{{ site.URL }}{{ .Path }}" rel="alternate">
|
|
|
|
</entry>
|
|
|
|
{{ end -}}
|
|
|
|
</feed>`
|