@@ 2,16 2,60 @@
package main
import (
+ "flag"
"fmt"
"html"
+ "html/template"
"io"
+ "log"
"os"
+ "strings"
"git.sr.ht/~adnano/go-gemini"
)
func main() {
- hw := HTMLWriter{
+ var templatePath string
+ flag.StringVar(&templatePath, "t", "", "the template to use")
+ flag.Parse()
+
+ if templatePath != "" {
+ tmpl, err := template.ParseFiles(templatePath)
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ ctx := new(struct {
+ Title string
+ Content template.HTML
+ })
+
+ b := new(strings.Builder)
+ hw := &HTMLWriter{
+ out: b,
+ }
+
+ var title bool
+ gemini.ParseLines(os.Stdin, func(line gemini.Line) {
+ if !title {
+ if h1, ok := line.(gemini.LineHeading1); ok {
+ ctx.Title = string(h1)
+ title = true
+ }
+ }
+ hw.Handle(line)
+ })
+ hw.Finish()
+ ctx.Content = template.HTML(b.String())
+
+ if err := tmpl.Execute(os.Stdout, ctx); err != nil {
+ log.Fatal(err)
+ }
+
+ return
+ }
+
+ hw := &HTMLWriter{
out: os.Stdout,
}
gemini.ParseLines(os.Stdin, hw.Handle)