深入理解Hugo: Content Map

package main
import (
    "bytes"
    "fmt"
    "github.com/sunwei/gobyexample/modules/radixtree"
    "golang.org/x/tools/txtar"
    "os"
    "path/filepath"
)
func main() {
    dir, _ := os.MkdirTemp("", "hugo")
    defer os.RemoveAll(dir)
    myContentPath := filepath.Join(dir, "blog")
    _ = os.Mkdir(myContentPath, os.ModePerm)
    mcs := "-- post.md --\n" +
        "---\n" +
        "title: \"Post Title\"\n" +
        "---\n" +
        "### first blog\n" +
        "Hugo & Caddy > WordPress & Apache"
    writeFiles(mcs, myContentPath)
    bundlePath := filepath.Join(myContentPath, "post")
    f, err := os.Stat(bundlePath + ".md")
    if err != nil {
        fmt.Println(err)
        return
    }
    n := &contentNode{
        fi:   f,
        path: "blog/post.md",
    }
    baseKey := myContentPath + "/"
    m := newContentMap()
    m.sections.Insert(baseKey, &contentNode{
        path: "blog",
    })
    key := baseKey + cmBranchSeparator + "post" + cmLeafSeparator
    m.pages.Insert(key, n)
    v, _ := m.sections.Get(baseKey)
    fmt.Println("=--=", baseKey, v)
    v, _ = m.pages.Get(key)
    fmt.Println("=--=", key, v)
    cn := v.(*contentNode)
    fmt.Println(cn.fi.Name(), cn.fi.Size())
}
const (
    cmBranchSeparator = "__hb_"
    cmLeafSeparator   = "__hl_"
)

Set if source is a file. We will soon get other sources.

type contentNode struct {
    fi os.FileInfo

The source path. Unix slashes. No leading slash.

    path string
}
type contentTree struct {
    Name string
    *radixtree.Tree
}
type contentMap struct {
    pages    *contentTree
    sections *contentTree
}
func newContentMap() *contentMap {
    return &contentMap{
        pages: &contentTree{
            Name: "pages",
            Tree: radixtree.New()},
        sections: &contentTree{
            Name: "sections",
            Tree: radixtree.New()},
    }
}
func writeFiles(s string, dir string) {
    data := txtar.Parse([]byte(s))
    for _, f := range data.Files {
        if err := os.WriteFile(
            filepath.Join(dir, f.Name),
            bytes.TrimSuffix(f.Data, []byte("\n")),
            os.ModePerm); err != nil {
            panic(err)
        }
    }
}
=--= /tmp/hugo1800886898/blog/ &{<nil> blog}
=--= /tmp/hugo1800886898/blog/__hb_post__hl_ \
&{0xc000096340 blog/post.md}
post.md 76
Program exited.

Next example: Page Processor.