Add BlogHTTPURL preprocess function

main
Brian Picciano 1 year ago
parent 26dbc6691d
commit 024f514886
  1. 28
      src/gmi/tpl.go
  2. 3
      src/http/posts.go
  3. 17
      src/post/preprocess.go

@ -134,11 +134,12 @@ func (r renderer) Add(a, b int) int { return a + b }
func (a *api) tplHandler() (gemini.Handler, error) { 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 // filepath.Join strips trailing slash, but we want to keep it
trailingSlash := strings.HasSuffix(path, "/") trailingSlash := strings.HasSuffix(path, "/")
path = filepath.Join("/", a.params.PublicURL.Path, path) path = filepath.Join("/", base.Path, path)
if trailingSlash && path != "/" { if trailingSlash && path != "/" {
path += "/" path += "/"
@ -148,27 +149,29 @@ func (a *api) tplHandler() (gemini.Handler, error) {
return path return path
} }
u := *a.params.PublicURL u := *base
u.Path = path u.Path = path
return u.String() return u.String()
} }
preprocessFuncs := post.PreprocessFunctions{ preprocessFuncs := post.PreprocessFunctions{
BlogURL: func(path string) string { 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 { AssetURL: func(id string) string {
path := filepath.Join("assets", id) path := filepath.Join("assets", id)
return blogURL(path, false) return blogURL(a.params.PublicURL, path, false)
}, },
PostURL: func(id string) string { PostURL: func(id string) string {
path := filepath.Join("posts", id) + ".gmi" path := filepath.Join("posts", id) + ".gmi"
return blogURL(path, false) return blogURL(a.params.PublicURL, path, false)
}, },
StaticURL: func(path string) string { StaticURL: func(path string) string {
httpPublicURL := *a.params.HTTPPublicURL path = filepath.Join("static", path)
httpPublicURL.Path = filepath.Join(httpPublicURL.Path, "/static", path) return blogURL(a.params.HTTPPublicURL, path, true)
return httpPublicURL.String()
}, },
Image: func(args ...string) (string, error) { Image: func(args ...string) (string, error) {
@ -181,7 +184,8 @@ func (a *api) tplHandler() (gemini.Handler, error) {
descr = args[1] 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 return fmt.Sprintf("\n=> %s %s", path, descr), nil
}, },
@ -193,11 +197,11 @@ func (a *api) tplHandler() (gemini.Handler, error) {
allTpls.Funcs(template.FuncMap{ allTpls.Funcs(template.FuncMap{
"BlogURLAbs": func(path string) string { "BlogURLAbs": func(path string) string {
return blogURL(path, true) return blogURL(a.params.PublicURL, path, true)
}, },
"PostURLAbs": func(id string) string { "PostURLAbs": func(id string) string {
path := filepath.Join("posts", id) + ".gmi" path := filepath.Join("posts", id) + ".gmi"
return blogURL(path, true) return blogURL(a.params.PublicURL, path, true)
}, },
}) })

@ -72,6 +72,9 @@ func (a *api) postToPostTplPayload(storedPost post.StoredPost) (postTplPayload,
BlogURL: func(path string) string { BlogURL: func(path string) string {
return a.blogURL(path, false) return a.blogURL(path, false)
}, },
BlogHTTPURL: func(path string) string {
return a.blogURL(path, false)
},
AssetURL: func(id string) string { AssetURL: func(id string) string {
return a.assetURL(id, false) return a.assetURL(id, false)
}, },

@ -17,6 +17,12 @@ type PreprocessFunctions struct {
// The given path should not have a leading slash. // The given path should not have a leading slash.
BlogURL func(path string) string BlogURL func(path string) string
// BlogURL returns the given string, rooted to the base URL of the blog's
// HTTP server (which may or may not include path components itself).
//
// The given path should not have a leading slash.
BlogHTTPURL func(path string) string
// AssetURL returns the URL of the asset with the given ID. // AssetURL returns the URL of the asset with the given ID.
AssetURL func(id string) string AssetURL func(id string) string
@ -39,11 +45,12 @@ type PreprocessFunctions struct {
func (funcs PreprocessFunctions) ToFuncsMap() template.FuncMap { func (funcs PreprocessFunctions) ToFuncsMap() template.FuncMap {
return template.FuncMap{ return template.FuncMap{
"BlogURL": funcs.BlogURL, "BlogURL": funcs.BlogURL,
"AssetURL": funcs.AssetURL, "BlogHTTPURL": funcs.BlogHTTPURL,
"PostURL": funcs.PostURL, "AssetURL": funcs.AssetURL,
"StaticURL": funcs.StaticURL, "PostURL": funcs.PostURL,
"Image": funcs.Image, "StaticURL": funcs.StaticURL,
"Image": funcs.Image,
} }
} }

Loading…
Cancel
Save