A vibe coded tangled fork which supports pijul.

appview/pages: export a few helpers

Signed-off-by: Anirudh Oppiliappan <anirudh@tangled.org>

authored by

Anirudh Oppiliappan and committed by tangled.org 1f39f869 260976e4

+68 -7
+9 -7
appview/pages/funcmap.go
··· 526 526 signature := hex.EncodeToString(h.Sum(nil)) 527 527 528 528 // Get avatar CID for cache busting 529 - profile, err := db.GetProfile(p.db, did) 530 529 version := "" 531 - if err == nil && profile != nil && profile.Avatar != "" { 532 - // Use first 8 chars of avatar CID as version 533 - if len(profile.Avatar) > 8 { 534 - version = profile.Avatar[:8] 535 - } else { 536 - version = profile.Avatar 530 + if p.db != nil { 531 + profile, err := db.GetProfile(p.db, did) 532 + if err == nil && profile != nil && profile.Avatar != "" { 533 + // Use first 8 chars of avatar CID as version 534 + if len(profile.Avatar) > 8 { 535 + version = profile.Avatar[:8] 536 + } else { 537 + version = profile.Avatar 538 + } 537 539 } 538 540 } 539 541
+59
appview/pages/pages.go
··· 88 88 return "templates/" + s + ".html" 89 89 } 90 90 91 + // FuncMap returns the template function map for use by external template consumers. 92 + func (p *Pages) FuncMap() template.FuncMap { 93 + return p.funcMap() 94 + } 95 + 96 + // FragmentPaths returns all fragment template paths from the embedded FS. 97 + func (p *Pages) FragmentPaths() ([]string, error) { 98 + return p.fragmentPaths() 99 + } 100 + 101 + // EmbedFS returns the embedded filesystem containing templates and static assets. 102 + func (p *Pages) EmbedFS() fs.FS { 103 + return p.embedFS 104 + } 105 + 106 + // ParseWith parses the base layout together with all appview fragments and 107 + // an additional template from extraFS identified by extraPath (relative to 108 + // extraFS root). The returned template is ready to ExecuteTemplate with 109 + // "layouts/base" -- primarily for use with the blog. 110 + func (p *Pages) ParseWith(extraFS fs.FS, extraPath string) (*template.Template, error) { 111 + fragmentPaths, err := p.fragmentPaths() 112 + if err != nil { 113 + return nil, err 114 + } 115 + 116 + funcs := p.funcMap() 117 + tpl, err := template.New("layouts/base"). 118 + Funcs(funcs). 119 + ParseFS(p.embedFS, append(fragmentPaths, p.nameToPath("layouts/base"))...) 120 + if err != nil { 121 + return nil, err 122 + } 123 + 124 + err = fs.WalkDir(extraFS, ".", func(path string, d fs.DirEntry, err error) error { 125 + if err != nil { 126 + return err 127 + } 128 + if d.IsDir() || !strings.HasSuffix(path, ".html") { 129 + return nil 130 + } 131 + if path != extraPath && !strings.Contains(path, "fragments/") { 132 + return nil 133 + } 134 + data, err := fs.ReadFile(extraFS, path) 135 + if err != nil { 136 + return err 137 + } 138 + if _, err = tpl.New(path).Parse(string(data)); err != nil { 139 + return err 140 + } 141 + return nil 142 + }) 143 + if err != nil { 144 + return nil, err 145 + } 146 + 147 + return tpl, nil 148 + } 149 + 91 150 func (p *Pages) fragmentPaths() ([]string, error) { 92 151 var fragmentPaths []string 93 152 err := fs.WalkDir(p.embedFS, "templates", func(path string, d fs.DirEntry, err error) error {