package site
import (
"path"
"adammathes.com/snkt/render"
"adammathes.com/snkt/config"
"os"
"log"
"gopkg.in/yaml.v2"
)
type skeleton struct {
Dir string
Filename string
Content []byte
}
var skeletons = []skeleton {
{
Dir: "tmpl",
Filename: "base",
Content: []byte(
`{{define "base"}}
{{template "title" .}}
{{template "content" .}}
Home
Archive
{{end}}
{{define "title"}}{{end}}
{{define "content"}}{{end}}
`)},
{
Dir: "tmpl",
Filename: "home",
Content: []byte(
`{{define "title"}}{{.Site.Title}}{{end}}
{{define "content"}}
{{.Site.Title}}
{{range .Posts.Limit 15}}
{{.Content}}
{{end}}
{{end}}
`)},
{
Dir: "tmpl",
Filename: "post",
Content: []byte(
`{{define "title"}}{{.Post.Title}} - {{.Site.Title}}{{end}}
{{define "content"}}
{{.Site.Title}}
{{.Post.Title}}
{{.Post.Content}}
{{if .Post.Next.Title}}
{{.Post.Next.Title}}
{{end}}
{{if .Post.Prev.Title}}
{{.Post.Prev.Title}}
{{end}}
{{end}}
`)},
{
Dir: "tmpl",
Filename: "archive",
Content: []byte(
`{{define "title"}}{{.Site.Title}} Archives{{end}}
{{define "content"}}
{{ .Site.Title }}
Archives
{{range .Posts}}
{{if .}}{{end}}
{{end}}
{{end}}
`)},
{
Dir: "tmpl",
Filename: "rss",
Content: []byte(
`{{define "base"}}
{{ .Site.Title }}
{{ .Site.URL }}
{{ range .Posts.Limit 15 }}
-
{{ .Permalink }}
{{ .RssDate }}
{{ end }}
{{end}}
`)}}
/*
Init initializes a new site in `directory` This includes: create `directory` and populate with:
config.json with sane defaults
txt, html directories for input/output
tmpl directory with barebones [base,post,archive,rss] templates
*/
var init_dir = ""
func Init(directory string) {
init_dir = directory
if init_dir[0] != '/' {
wd, err := os.Getwd()
if err == nil {
init_dir = path.Join(wd, init_dir)
}
}
var cfg = config.Settings{
SiteTitle: "snkt",
SiteURL: "",
TxtDir: path.Join(init_dir, "txt"),
HtmlDir: path.Join(init_dir, "html"),
TmplDir: path.Join(init_dir, "tmpl"),
}
cyaml, err := yaml.Marshal(cfg)
if err != nil {
log.Fatal("marshalling yaml error: ", err)
}
c := skeleton{
Dir: "",
Filename: "config.yml",
Content: cyaml,
}
skeletons = append(skeletons, c)
os.MkdirAll( cfg.TxtDir , 0755)
os.MkdirAll( cfg.HtmlDir , 0755)
os.MkdirAll( cfg.TmplDir , 0755)
writeSkeletons()
}
func (s skeleton) Render() []byte {
return s.Content
}
func (s skeleton) Target() string {
return path.Join(init_dir, s.Dir, s.Filename)
}
func writeSkeletons() {
for _,skeleton := range skeletons {
render.Write(skeleton)
}
}