diff options
author | tjpcc <tjp@ctrl-c.club> | 2023-09-17 14:00:03 -0600 |
---|---|---|
committer | tjpcc <tjp@ctrl-c.club> | 2023-09-17 14:00:03 -0600 |
commit | ce0def95f3924a10b0faceb72aa5df18bf813fb1 (patch) | |
tree | 2f0300d84738343ea2fb9f13db1af1e649b41917 /templates.go | |
parent | ebea6fe755fb052b79cbe05b6280611337e1e5f6 (diff) |
GOPHER
Diffstat (limited to 'templates.go')
-rw-r--r-- | templates.go | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/templates.go b/templates.go index 3f8cf42..4c226f6 100644 --- a/templates.go +++ b/templates.go @@ -2,6 +2,7 @@ package syw import ( "embed" + "net/url" "text/template" ) @@ -11,6 +12,47 @@ var ( geminiTemplate = template.Must(template.ParseFS(geminiTemplateFS, "templates/*.gmi")) ) +var ( + //go:embed templates/*.gophermap templates/*.gophertext + gopherTemplateFS embed.FS + gopherTemplate = template.Must( + template.New("gopher").Funcs(template.FuncMap{ + "combine": gopherCombine, + "join": gopherJoin, + }).ParseFS( + gopherTemplateFS, + "templates/*.gophermap", + "templates/*.gophertext", + ), + ) +) + +func gopherCombine(base string, relative ...string) (string, error) { + bu, err := url.Parse(base) + if err != nil { + return "", err + } + + for _, rel := range relative { + ru, err := url.Parse(rel) + if err != nil { + return "", err + } + bu = bu.ResolveReference(ru) + } + + return bu.String(), nil +} + +func gopherJoin(base string, relative ...string) (string, error) { + bu, err := url.Parse(base) + if err != nil { + return "", err + } + + return bu.JoinPath(relative...).Path, nil +} + func addTemplates(base *template.Template, additions *template.Template) (*template.Template, error) { base, err := base.Clone() if err != nil { |