summaryrefslogtreecommitdiff
path: root/templates.go
diff options
context:
space:
mode:
Diffstat (limited to 'templates.go')
-rw-r--r--templates.go42
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 {