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) return c.Templates.Load(path)
} }

11
main.go
View file

@ -1,6 +1,7 @@
package main package main
import ( import (
"embed"
"flag" "flag"
"fmt" "fmt"
"io" "io"
@ -138,11 +139,15 @@ func copyAll(srcDir, dstDir string) error {
}) })
} }
//go:embed templates config.toml
var builtin embed.FS
func newSite(name string) { func newSite(name string) {
name = path.Clean(name) name = path.Clean(name)
os.Mkdir(name, 0755) os.Mkdir(name, 0755)
os.Mkdir(path.Join(name, "content"), 0755) os.Mkdir(path.Join(name, "content"), 0755)
os.Mkdir(path.Join(name, "templates"), 0755) os.Mkdir(path.Join(name, "templates"), 0755)
os.Mkdir(path.Join(name, "templates/_default"), 0755)
os.Mkdir(path.Join(name, "static"), 0755) os.Mkdir(path.Join(name, "static"), 0755)
config, _ := builtin.ReadFile("config.toml") config, _ := builtin.ReadFile("config.toml")
@ -150,4 +155,10 @@ func newSite(name string) {
index := []byte("# Hello, world!\n") index := []byte("# Hello, world!\n")
os.WriteFile(path.Join(name, "content/index.gmi"), index, 0644) 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 package main
import ( import (
"embed"
"io/ioutil" "io/ioutil"
"log" "log"
"os" "os"
@ -11,9 +10,6 @@ import (
"text/template" "text/template"
) )
//go:embed templates config.toml
var builtin 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
@ -33,23 +29,6 @@ func (t *Templates) Funcs(funcs template.FuncMap) {
t.funcs = funcs 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. // LoadTemplate loads a template from the provided path and content.
func (t *Templates) LoadTemplate(path string, content []byte) { func (t *Templates) LoadTemplate(path string, content []byte) {
tmpl := template.New(path) 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 }}