mirror of
https://git.sr.ht/~adnano/kiln
synced 2024-10-30 01:13:08 +00:00
e3a479f921
Use content, public, and layouts instead of src, dst, and templates.
95 lines
1.9 KiB
Go
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("layouts"); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Load content
|
|
dir := NewDir("")
|
|
if err := dir.read("content", ""); err != nil {
|
|
return err
|
|
}
|
|
dir.sort()
|
|
// Manipulate content
|
|
if err := dir.manipulate(cfg); err != nil {
|
|
return err
|
|
}
|
|
// Write content
|
|
if err := dir.write("public", 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
|
|
}
|