mirror of
https://git.sr.ht/~adnano/kiln
synced 2024-10-30 01:13:08 +00:00
Write default templates when creating a new site
This commit is contained in:
parent
e4d3b016d8
commit
27684b26f3
|
@ -66,6 +66,5 @@ func (c *Config) LoadTemplates(path string) error {
|
|||
}
|
||||
},
|
||||
})
|
||||
c.Templates.LoadDefault()
|
||||
return c.Templates.Load(path)
|
||||
}
|
||||
|
|
11
main.go
11
main.go
|
@ -1,6 +1,7 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
|
@ -138,11 +139,15 @@ func copyAll(srcDir, dstDir string) error {
|
|||
})
|
||||
}
|
||||
|
||||
//go:embed templates config.toml
|
||||
var builtin embed.FS
|
||||
|
||||
func newSite(name string) {
|
||||
name = path.Clean(name)
|
||||
os.Mkdir(name, 0755)
|
||||
os.Mkdir(path.Join(name, "content"), 0755)
|
||||
os.Mkdir(path.Join(name, "templates"), 0755)
|
||||
os.Mkdir(path.Join(name, "templates/_default"), 0755)
|
||||
os.Mkdir(path.Join(name, "static"), 0755)
|
||||
|
||||
config, _ := builtin.ReadFile("config.toml")
|
||||
|
@ -150,4 +155,10 @@ func newSite(name string) {
|
|||
|
||||
index := []byte("# Hello, world!\n")
|
||||
os.WriteFile(path.Join(name, "content/index.gmi"), index, 0644)
|
||||
|
||||
templates := []string{"atom.xml", "index.gmi", "page.gmi"}
|
||||
for _, template := range templates {
|
||||
b, _ := builtin.ReadFile(path.Join("templates", template))
|
||||
os.WriteFile(path.Join(name, "templates/_default", template), b, 0644)
|
||||
}
|
||||
}
|
||||
|
|
21
templates.go
21
templates.go
|
@ -1,7 +1,6 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
|
@ -11,9 +10,6 @@ import (
|
|||
"text/template"
|
||||
)
|
||||
|
||||
//go:embed templates config.toml
|
||||
var builtin embed.FS
|
||||
|
||||
// Templates contains site templates.
|
||||
type Templates struct {
|
||||
tmpls map[string]*template.Template
|
||||
|
@ -33,23 +29,6 @@ func (t *Templates) Funcs(funcs template.FuncMap) {
|
|||
t.funcs = funcs
|
||||
}
|
||||
|
||||
// LoadDefault loads the default templates.
|
||||
// Should be called after Funcs.
|
||||
func (t *Templates) LoadDefault() {
|
||||
t.loadDefault("index.gmi")
|
||||
t.loadDefault("page.gmi")
|
||||
t.loadDefault("atom.xml")
|
||||
t.loadDefault("output.html")
|
||||
}
|
||||
|
||||
func (t *Templates) loadDefault(name string) {
|
||||
b, err := builtin.ReadFile("templates/" + name)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
t.LoadTemplate("/_default/"+name, b)
|
||||
}
|
||||
|
||||
// LoadTemplate loads a template from the provided path and content.
|
||||
func (t *Templates) LoadTemplate(path string, content []byte) {
|
||||
tmpl := template.New(path)
|
||||
|
|
|
@ -1,6 +0,0 @@
|
|||
<!DOCTYPE html>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width,initial-scale=1">
|
||||
<title>{{ .Title }}</title>
|
||||
|
||||
{{ .Content }}
|
Loading…
Reference in a new issue