You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

90 lines
1.8 KiB

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 + "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
}