From 1f39f869e7574dfc5218162725570c5c8ce0d970 Mon Sep 17 00:00:00 2001 From: Anirudh Oppiliappan Date: Sat, 14 Mar 2026 21:33:23 +0000 Subject: [PATCH] appview/pages: export a few helpers Signed-off-by: Anirudh Oppiliappan --- appview/pages/funcmap.go | 16 +++++++++------- appview/pages/pages.go | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 file(s) changed, 68 insertion(s)(+), 7 deletion(s)(-) diff --git a/appview/pages/funcmap.go b/appview/pages/funcmap.go --- a/appview/pages/funcmap.go +++ b/appview/pages/funcmap.go @@ -526,14 +526,16 @@ signature := hex.EncodeToString(h.Sum(nil)) // Get avatar CID for cache busting - profile, err := db.GetProfile(p.db, did) version := "" - if err == nil && profile != nil && profile.Avatar != "" { - // Use first 8 chars of avatar CID as version - if len(profile.Avatar) > 8 { - version = profile.Avatar[:8] - } else { - version = profile.Avatar + if p.db != nil { + profile, err := db.GetProfile(p.db, did) + if err == nil && profile != nil && profile.Avatar != "" { + // Use first 8 chars of avatar CID as version + if len(profile.Avatar) > 8 { + version = profile.Avatar[:8] + } else { + version = profile.Avatar + } } } diff --git a/appview/pages/pages.go b/appview/pages/pages.go --- a/appview/pages/pages.go +++ b/appview/pages/pages.go @@ -88,6 +88,65 @@ return "templates/" + s + ".html" } +// FuncMap returns the template function map for use by external template consumers. +func (p *Pages) FuncMap() template.FuncMap { + return p.funcMap() +} + +// FragmentPaths returns all fragment template paths from the embedded FS. +func (p *Pages) FragmentPaths() ([]string, error) { + return p.fragmentPaths() +} + +// EmbedFS returns the embedded filesystem containing templates and static assets. +func (p *Pages) EmbedFS() fs.FS { + return p.embedFS +} + +// ParseWith parses the base layout together with all appview fragments and +// an additional template from extraFS identified by extraPath (relative to +// extraFS root). The returned template is ready to ExecuteTemplate with +// "layouts/base" -- primarily for use with the blog. +func (p *Pages) ParseWith(extraFS fs.FS, extraPath string) (*template.Template, error) { + fragmentPaths, err := p.fragmentPaths() + if err != nil { + return nil, err + } + + funcs := p.funcMap() + tpl, err := template.New("layouts/base"). + Funcs(funcs). + ParseFS(p.embedFS, append(fragmentPaths, p.nameToPath("layouts/base"))...) + if err != nil { + return nil, err + } + + err = fs.WalkDir(extraFS, ".", func(path string, d fs.DirEntry, err error) error { + if err != nil { + return err + } + if d.IsDir() || !strings.HasSuffix(path, ".html") { + return nil + } + if path != extraPath && !strings.Contains(path, "fragments/") { + return nil + } + data, err := fs.ReadFile(extraFS, path) + if err != nil { + return err + } + if _, err = tpl.New(path).Parse(string(data)); err != nil { + return err + } + return nil + }) + if err != nil { + return nil, err + } + + return tpl, nil +} + func (p *Pages) fragmentPaths() ([]string, error) { var fragmentPaths []string err := fs.WalkDir(p.embedFS, "templates", func(path string, d fs.DirEntry, err error) error { -- tangled.sh