~adnano/gmnitohtml

d796439619713d8ae1258ad3662ae01d5fcb1a25 — Adnan Maolood 3 years ago 28489ca
Allow specifiying a custom output template to use
1 files changed, 45 insertions(+), 1 deletions(-)

M main.go
M main.go => main.go +45 -1
@@ 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)