From 01424c7dab5fe42a9f00e3c9bb285924e42136fc Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Sat, 21 May 2022 11:34:03 -0600 Subject: Standardize URL generation across the blog --- srv/src/http/tpl.go | 59 +++++++++++++++++++++++++++++++++++------------------ 1 file changed, 39 insertions(+), 20 deletions(-) (limited to 'srv/src/http/tpl.go') diff --git a/srv/src/http/tpl.go b/srv/src/http/tpl.go index dcd1551..2e22370 100644 --- a/srv/src/http/tpl.go +++ b/srv/src/http/tpl.go @@ -6,6 +6,7 @@ import ( "fmt" "html/template" "io/fs" + "log" "net/http" "path/filepath" "strings" @@ -28,37 +29,54 @@ func mustReadTplFile(fileName string) string { return string(b) } -func (a *api) parseTpl(tplBody string) (*template.Template, error) { +func (a *api) blogURL(path string, abs bool) string { + // filepath.Join strips trailing slash, but we want to keep it + trailingSlash := strings.HasSuffix(path, "/") - blogURL := func(path string) string { + res := filepath.Join("/", path) - // filepath.Join strips trailing slash, but we want to keep it - trailingSlash := strings.HasSuffix(path, "/") + if trailingSlash && res != "/" { + res += "/" + } - path = filepath.Join("/", path) + if abs { + res = a.params.PublicURL.String() + res + } - if trailingSlash && path != "/" { - path += "/" - } + return res +} - return path - } +func (a *api) postURL(id string, abs bool) string { + path := filepath.Join("posts", id) + return a.blogURL(path, abs) +} + +func (a *api) postsURL(abs bool) string { + return a.blogURL("posts", abs) +} + +func (a *api) assetsURL(abs bool) string { + return a.blogURL("assets", abs) +} + +func (a *api) parseTpl(name, tplBody string) (*template.Template, error) { tpl := template.New("root") tpl = tpl.Funcs(template.FuncMap{ - "BlogURL": blogURL, + "BlogURL": func(path string) string { + return a.blogURL(path, false) + }, "StaticURL": func(path string) string { path = filepath.Join("static", path) - return blogURL(path) + return a.blogURL(path, false) }, "AssetURL": func(id string) string { path := filepath.Join("assets", id) - return blogURL(path) + return a.blogURL(path, false) }, "PostURL": func(id string) string { - path := filepath.Join("posts", id) - return blogURL(path) + return a.postURL(id, false) }, "DateTimeFormat": func(t time.Time) string { return t.Format("2006-01-02") @@ -89,7 +107,7 @@ func (a *api) parseTpl(tplBody string) (*template.Template, error) { var err error - if tpl, err = tpl.New("").Parse(tplBody); err != nil { + if tpl, err = tpl.New(name).Parse(tplBody); err != nil { return nil, err } @@ -97,7 +115,7 @@ func (a *api) parseTpl(tplBody string) (*template.Template, error) { } func (a *api) mustParseTpl(name string) *template.Template { - return template.Must(a.parseTpl(mustReadTplFile(name))) + return template.Must(a.parseTpl(name, mustReadTplFile(name))) } func (a *api) mustParseBasedTpl(name string) *template.Template { @@ -140,12 +158,13 @@ func executeTemplate( } func (a *api) executeRedirectTpl( - rw http.ResponseWriter, r *http.Request, path string, + rw http.ResponseWriter, r *http.Request, url string, ) { + log.Printf("here url:%q", url) executeTemplate(rw, r, a.redirectTpl, struct { - Path string + URL string }{ - Path: path, + URL: url, }) } -- cgit v1.2.3