ucl/cmd/gendocs/main.go
Leon Mika 42d5a89471
All checks were successful
Build / build (push) Successful in 1m49s
Started working on more package docs
2025-01-19 10:11:55 +11:00

72 lines
1.3 KiB
Go

package main
import (
"bytes"
"embed"
"flag"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/parser"
"go.abhg.dev/goldmark/frontmatter"
"html/template"
"log"
"os"
"path/filepath"
)
//go:embed frame.tmpl
var frameTmpl embed.FS
func main() {
flag.Parse()
if flag.NArg() != 2 {
log.Fatalln("usage: gendocs MARKDOWN OUTFILE")
}
srcFile := flag.Arg(0)
dstFile := flag.Arg(1)
md := goldmark.New(goldmark.WithExtensions(&frontmatter.Extender{}))
mdData, err := os.ReadFile(srcFile)
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)
}
destDir := filepath.Dir(dstFile)
if err := os.MkdirAll(destDir, 0755); err != nil {
log.Fatal(err)
}
if err := os.WriteFile(dstFile, res.Bytes(), 0644); err != nil {
log.Fatal(err)
}
}