kiln/main.go
2020-11-25 14:31:58 -05:00

89 lines
1.7 KiB
Go

package main
import (
"bytes"
"flag"
"log"
pathpkg "path"
"strings"
"git.sr.ht/~adnano/go-gemini"
)
func main() {
if err := run(); err != nil {
log.Fatal(err)
}
}
func run() error {
// whether or not to output HTML
var toHTML bool
flag.BoolVar(&toHTML, "html", false, "output HTML")
flag.Parse()
// Load config
cfg := NewConfig()
if err := cfg.Load("config.ini"); err != nil {
return err
}
if err := cfg.LoadTemplates("templates"); err != nil {
return err
}
// Load content
dir := NewDir("")
if err := dir.read("src", ""); err != nil {
return err
}
dir.sort()
// Manipulate content
if err := dir.manipulate(cfg); err != nil {
return err
}
// Write content
if err := dir.write("dst", outputGemini, cfg); err != nil {
return err
}
if toHTML {
if err := dir.write("html", outputHTML, cfg); err != nil {
return err
}
}
return nil
}
// outputFormat represents an output format.
type outputFormat func(*Page, *Config) (path string, content []byte)
// outputGemini outputs the page as Gemini text.
func outputGemini(p *Page, cfg *Config) (path string, content []byte) {
path = p.Path + "index.gmi"
content = []byte(p.Content)
return
}
// outputHTML outputs the page as HTML.
func outputHTML(p *Page, cfg *Config) (path string, content []byte) {
path = p.Path + "index.html"
r := strings.NewReader(p.Content)
text := gemini.ParseText(r)
content = textToHTML(text)
// html template context
type htmlCtx struct {
Title string // page title
Content string // page HTML contents
}
var b bytes.Buffer
tmpl := cfg.Templates.FindTemplate(pathpkg.Dir(path), "output.html")
tmpl.Execute(&b, &htmlCtx{
Title: p.Title,
Content: string(content),
})
content = b.Bytes()
return
}