1
0
Fork 0
mirror of https://git.sr.ht/~adnano/kiln synced 2024-05-14 20:46:05 +02:00

Write default templates when creating a new site

This commit is contained in:
Adnan Maolood 2021-04-21 14:03:41 -04:00
parent dbc3791879
commit 793116c853
4 changed files with 11 additions and 28 deletions

View File

@ -66,6 +66,5 @@ func (c *Config) LoadTemplates(path string) error {
}
},
})
c.Templates.LoadDefault()
return c.Templates.Load(path)
}

11
main.go
View File

@ -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)
}
}

View File

@ -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)

View File

@ -1,6 +0,0 @@
<!DOCTYPE html>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>{{ .Title }}</title>
{{ .Content }}