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