1
0
Fork 0
mirror of https://git.sr.ht/~adnano/kiln synced 2024-06-08 04:06:08 +02:00
kiln/main.go

91 lines
1.8 KiB
Go
Raw Normal View History

2020-09-22 22:42:14 +02:00
package main
import (
2020-10-01 03:57:59 +02:00
"bytes"
2020-09-29 01:54:48 +02:00
"flag"
2020-09-22 22:42:14 +02:00
"log"
2020-11-20 18:07:38 +01:00
pathpkg "path"
2020-10-01 03:57:59 +02:00
"strings"
2020-09-29 01:54:48 +02:00
2020-10-24 23:11:30 +02:00
"git.sr.ht/~adnano/go-gemini"
2020-09-29 01:54:48 +02:00
)
2020-09-22 22:42:14 +02:00
func main() {
2020-10-01 03:57:59 +02:00
if err := run(); err != nil {
2020-09-22 22:42:14 +02:00
log.Fatal(err)
}
}
2020-11-20 18:07:38 +01:00
func run() error {
// whether or not to output HTML
var toHTML bool
flag.BoolVar(&toHTML, "html", false, "output HTML")
flag.Parse()
2020-10-01 03:57:59 +02:00
2020-11-20 18:07:38 +01:00
// Load config
cfg := NewConfig()
if err := cfg.Load("config.ini"); err != nil {
return err
}
if err := cfg.LoadTemplates("templates"); err != nil {
return err
2020-10-01 04:14:10 +02:00
}
2020-10-01 03:57:59 +02:00
// Load content
dir := NewDir("")
2020-11-20 18:07:38 +01:00
if err := dir.read("src", ""); err != nil {
2020-09-22 22:42:14 +02:00
return err
}
2020-10-01 03:57:59 +02:00
dir.sort()
// Manipulate content
2020-11-20 18:07:38 +01:00
if err := dir.manipulate(cfg); err != nil {
2020-09-22 22:42:14 +02:00
return err
}
2020-10-01 03:57:59 +02:00
// Write content
2020-11-20 18:07:38 +01:00
if err := dir.write("dst", outputGemini, cfg); err != nil {
2020-09-22 22:42:14 +02:00
return err
}
2020-11-20 18:07:38 +01:00
if toHTML {
if err := dir.write("html", outputHTML, cfg); err != nil {
2020-09-29 16:57:15 +02:00
return err
}
}
2020-09-22 22:42:14 +02:00
return nil
}
2020-09-29 01:54:48 +02:00
2020-11-20 18:07:38 +01:00
// outputFormat represents an output format.
type outputFormat func(*Page, *Config) (path string, content []byte)
2020-11-20 18:07:38 +01:00
// outputGemini outputs the page as Gemini text.
func outputGemini(p *Page, cfg *Config) (path string, content []byte) {
2020-11-25 20:31:58 +01:00
path = p.Path + "index.gmi"
2020-11-20 18:07:38 +01:00
content = []byte(p.Content)
return
2020-10-01 03:57:59 +02:00
}
2020-11-20 18:07:38 +01:00
// outputHTML outputs the page as HTML.
func outputHTML(p *Page, cfg *Config) (path string, content []byte) {
2020-11-25 20:31:58 +01:00
path = p.Path + "index.html"
2020-10-01 03:57:59 +02:00
2020-11-20 18:07:38 +01:00
r := strings.NewReader(p.Content)
2020-11-01 05:54:07 +01:00
text := gemini.ParseText(r)
2020-11-20 18:07:38 +01:00
content = textToHTML(text)
2020-10-01 03:57:59 +02:00
2020-11-20 18:07:38 +01:00
// html template context
type htmlCtx struct {
Title string // page title
Content string // page HTML contents
2020-10-01 03:57:59 +02:00
}
var b bytes.Buffer
2020-11-28 00:49:53 +01:00
// clean path to remove trailing slash
dir := pathpkg.Dir(pathpkg.Clean(p.Path))
tmpl := cfg.Templates.FindTemplate(dir, "output.html")
2020-11-20 18:07:38 +01:00
tmpl.Execute(&b, &htmlCtx{
2020-10-01 03:57:59 +02:00
Title: p.Title,
Content: string(content),
})
content = b.Bytes()
return
}