kiln/main.go
2021-02-27 15:34:05 -05:00

95 lines
1.9 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 {
var format string
flag.StringVar(&format, "format", "gemini", "output format to use. Supported formats include gemini and html")
flag.Parse()
var output outputFormat
switch format {
case "gemini":
output = outputGemini
case "html":
output = outputHTML
default:
log.Fatalf("unknown output format %q", format)
}
// 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", output, 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
// clean path to remove trailing slash
dir := pathpkg.Dir(pathpkg.Clean(p.Path))
tmpl := cfg.Templates.FindTemplate(dir, "output.html")
tmpl.Execute(&b, &htmlCtx{
Title: p.Title,
Content: string(content),
})
content = b.Bytes()
return
}