weiro/providers/sitebuilder/tmplfns.go

39 lines
783 B
Go
Raw Normal View History

2026-02-18 11:07:18 +00:00
package sitebuilder
import (
"html/template"
"net/url"
2026-03-08 22:32:32 +00:00
"strings"
2026-02-18 11:07:18 +00:00
"time"
2026-02-19 10:21:27 +00:00
"lmika.dev/lmika/weiro/models/pubmodel"
2026-02-18 11:07:18 +00:00
)
2026-02-19 10:21:27 +00:00
func templateFns(site pubmodel.Site, opts Options) template.FuncMap {
2026-02-18 11:07:18 +00:00
return template.FuncMap{
"url_abs": func(basePath string) (string, error) {
2026-02-19 10:21:27 +00:00
if site.BaseURL == "" {
2026-02-18 11:07:18 +00:00
return basePath, nil
}
2026-02-19 10:21:27 +00:00
pu, err := url.Parse(site.BaseURL)
2026-02-18 11:07:18 +00:00
if err != nil {
return "", err
}
2026-03-08 22:32:32 +00:00
pu.Path = joinPath(pu.Path, basePath)
2026-02-18 11:07:18 +00:00
return pu.String(), nil
},
"format_date": func(date time.Time) string {
loc := opts.RenderTZ
if loc == nil {
loc = time.Local
}
return date.In(loc).Format("02 Jan 2006")
},
}
}
2026-03-08 22:32:32 +00:00
func joinPath(basePath, path string) string {
return strings.TrimSuffix(basePath, "/") + "/" + strings.TrimPrefix(path, "/")
}