mirror of
https://git.sr.ht/~adnano/kiln
synced 2024-10-30 09:23:09 +00:00
99 lines
1.9 KiB
Go
99 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 {
|
|
// 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
|
|
if strings.HasSuffix(path, "/") {
|
|
path += "index.gmi"
|
|
} else {
|
|
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
|
|
if strings.HasSuffix(path, "/") {
|
|
path += "index.html"
|
|
} else {
|
|
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
|
|
}
|