ucl/cmd/gendocs/main.go

62 lines
1.1 KiB
Go
Raw Normal View History

2025-01-18 05:02:35 +00:00
package main
import (
"bytes"
"embed"
"flag"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/parser"
"go.abhg.dev/goldmark/frontmatter"
"html/template"
"log"
"os"
)
//go:embed frame.tmpl
var frameTmpl embed.FS
func main() {
flag.Parse()
if flag.NArg() != 1 {
log.Fatalln("usage: gendocs [markdown]")
}
md := goldmark.New(goldmark.WithExtensions(&frontmatter.Extender{}))
mdData, err := os.ReadFile(flag.Arg(0))
if err != nil {
log.Fatal(err)
}
ctx := parser.NewContext()
var buf bytes.Buffer
if err := md.Convert(mdData, &buf, parser.WithContext(ctx)); err != nil {
log.Fatal(err)
}
var frontMatter struct {
Title string `yaml:"title"`
}
if fm := frontmatter.Get(ctx); fm != nil {
if err := fm.Decode(&frontMatter); err != nil {
log.Fatal(err)
}
}
frameTmpls, err := template.ParseFS(frameTmpl, "*.tmpl")
if err != nil {
log.Fatal(err)
}
var res bytes.Buffer
if err := frameTmpls.ExecuteTemplate(&res, "frame.tmpl", map[string]interface{}{
"Title": frontMatter.Title,
"Body": template.HTML(buf.Bytes()),
}); err != nil {
log.Fatal(err)
}
os.Stdout.Write(res.Bytes())
}