From 024f51488614919240a71cae1cae1c8fe6df1229 Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Mon, 23 Jan 2023 21:44:10 +0100 Subject: Add BlogHTTPURL preprocess function --- src/gmi/tpl.go | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) (limited to 'src/gmi/tpl.go') diff --git a/src/gmi/tpl.go b/src/gmi/tpl.go index 8f4c3be..566ace0 100644 --- a/src/gmi/tpl.go +++ b/src/gmi/tpl.go @@ -134,11 +134,12 @@ func (r renderer) Add(a, b int) int { return a + b } func (a *api) tplHandler() (gemini.Handler, error) { - blogURL := func(path string, abs bool) string { + blogURL := func(base *url.URL, path string, abs bool) string { + // filepath.Join strips trailing slash, but we want to keep it trailingSlash := strings.HasSuffix(path, "/") - path = filepath.Join("/", a.params.PublicURL.Path, path) + path = filepath.Join("/", base.Path, path) if trailingSlash && path != "/" { path += "/" @@ -148,27 +149,29 @@ func (a *api) tplHandler() (gemini.Handler, error) { return path } - u := *a.params.PublicURL + u := *base u.Path = path return u.String() } preprocessFuncs := post.PreprocessFunctions{ BlogURL: func(path string) string { - return blogURL(path, false) + return blogURL(a.params.PublicURL, path, false) + }, + BlogHTTPURL: func(path string) string { + return blogURL(a.params.HTTPPublicURL, path, true) }, AssetURL: func(id string) string { path := filepath.Join("assets", id) - return blogURL(path, false) + return blogURL(a.params.PublicURL, path, false) }, PostURL: func(id string) string { path := filepath.Join("posts", id) + ".gmi" - return blogURL(path, false) + return blogURL(a.params.PublicURL, path, false) }, StaticURL: func(path string) string { - httpPublicURL := *a.params.HTTPPublicURL - httpPublicURL.Path = filepath.Join(httpPublicURL.Path, "/static", path) - return httpPublicURL.String() + path = filepath.Join("static", path) + return blogURL(a.params.HTTPPublicURL, path, true) }, Image: func(args ...string) (string, error) { @@ -181,7 +184,8 @@ func (a *api) tplHandler() (gemini.Handler, error) { descr = args[1] } - path := blogURL(filepath.Join("assets", id), false) + path := filepath.Join("assets", id) + path = blogURL(a.params.PublicURL, path, false) return fmt.Sprintf("\n=> %s %s", path, descr), nil }, @@ -193,11 +197,11 @@ func (a *api) tplHandler() (gemini.Handler, error) { allTpls.Funcs(template.FuncMap{ "BlogURLAbs": func(path string) string { - return blogURL(path, true) + return blogURL(a.params.PublicURL, path, true) }, "PostURLAbs": func(id string) string { path := filepath.Join("posts", id) + ".gmi" - return blogURL(path, true) + return blogURL(a.params.PublicURL, path, true) }, }) -- cgit v1.2.3