|
|
|
@ -37,8 +37,7 @@ type rendererGetPostSeriesNextPreviousRes struct { |
|
|
|
|
type renderer struct { |
|
|
|
|
url *url.URL |
|
|
|
|
postStore post.Store |
|
|
|
|
gmiPublicURL *url.URL |
|
|
|
|
httpPublicURL *url.URL |
|
|
|
|
preprocessFuncs post.PreprocessFunctions |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (r renderer) GetPosts(page, count int) (rendererGetPostsRes, error) { |
|
|
|
@ -91,39 +90,9 @@ func (r renderer) GetPostSeriesNextPrevious(p post.StoredPost) (rendererGetPostS |
|
|
|
|
|
|
|
|
|
func (r renderer) PostBody(p post.StoredPost) (string, error) { |
|
|
|
|
|
|
|
|
|
preprocessFuncs := post.PreprocessFunctions{ |
|
|
|
|
BlogURL: func(path string) string { |
|
|
|
|
return filepath.Join("/", r.gmiPublicURL.Path, path) |
|
|
|
|
}, |
|
|
|
|
AssetURL: func(id string) string { |
|
|
|
|
return filepath.Join("/assets", id) |
|
|
|
|
}, |
|
|
|
|
PostURL: func(id string) string { |
|
|
|
|
return filepath.Join("/posts", id) |
|
|
|
|
}, |
|
|
|
|
StaticURL: func(path string) string { |
|
|
|
|
httpPublicURL := *r.httpPublicURL |
|
|
|
|
httpPublicURL.Path = filepath.Join(httpPublicURL.Path, "/static", path) |
|
|
|
|
return httpPublicURL.String() |
|
|
|
|
}, |
|
|
|
|
Image: func(args ...string) (string, error) { |
|
|
|
|
|
|
|
|
|
var ( |
|
|
|
|
id = args[0] |
|
|
|
|
descr = "Image" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
if len(args) > 1 { |
|
|
|
|
descr = args[1] |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return fmt.Sprintf("=> %s %s", filepath.Join("/assets", id), descr), nil |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer) |
|
|
|
|
|
|
|
|
|
if err := p.PreprocessBody(buf, preprocessFuncs); err != nil { |
|
|
|
|
if err := p.PreprocessBody(buf, r.preprocessFuncs); err != nil { |
|
|
|
|
return "", fmt.Errorf("preprocessing post body: %w", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -159,8 +128,66 @@ 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 { |
|
|
|
|
path = filepath.Join(a.params.PublicURL.Path, path) |
|
|
|
|
|
|
|
|
|
if !abs { |
|
|
|
|
return path |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
u := *a.params.PublicURL |
|
|
|
|
u.Path = path |
|
|
|
|
return u.String() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
preprocessFuncs := post.PreprocessFunctions{ |
|
|
|
|
BlogURL: func(path string) string { |
|
|
|
|
return blogURL(path, false) |
|
|
|
|
}, |
|
|
|
|
AssetURL: func(id string) string { |
|
|
|
|
path := filepath.Join("assets", id) |
|
|
|
|
return blogURL(path, false) |
|
|
|
|
}, |
|
|
|
|
PostURL: func(id string) string { |
|
|
|
|
path := filepath.Join("posts", id) + ".gmi" |
|
|
|
|
return blogURL(path, false) |
|
|
|
|
}, |
|
|
|
|
StaticURL: func(path string) string { |
|
|
|
|
httpPublicURL := *a.params.HTTPPublicURL |
|
|
|
|
httpPublicURL.Path = filepath.Join(httpPublicURL.Path, "/static", path) |
|
|
|
|
return httpPublicURL.String() |
|
|
|
|
}, |
|
|
|
|
Image: func(args ...string) (string, error) { |
|
|
|
|
|
|
|
|
|
var ( |
|
|
|
|
id = args[0] |
|
|
|
|
descr = "Image" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
if len(args) > 1 { |
|
|
|
|
descr = args[1] |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
path := blogURL(filepath.Join("assets", id), false) |
|
|
|
|
|
|
|
|
|
return fmt.Sprintf("=> %s %s", path, descr), nil |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
allTpls := template.New("") |
|
|
|
|
|
|
|
|
|
allTpls.Funcs(preprocessFuncs.ToFuncsMap()) |
|
|
|
|
|
|
|
|
|
allTpls.Funcs(template.FuncMap{ |
|
|
|
|
"BlogURLAbs": func(path string) string { |
|
|
|
|
return blogURL(path, true) |
|
|
|
|
}, |
|
|
|
|
"PostURLAbs": func(id string) string { |
|
|
|
|
path := filepath.Join("posts", id) + ".gmi" |
|
|
|
|
return blogURL(path, true) |
|
|
|
|
}, |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
err := fs.WalkDir(tplFS, "tpl", func(path string, d fs.DirEntry, err error) error { |
|
|
|
|
|
|
|
|
|
if err != nil { |
|
|
|
@ -221,8 +248,7 @@ func (a *api) tplHandler() (gemini.Handler, error) { |
|
|
|
|
err := tpl.Execute(buf, renderer{ |
|
|
|
|
url: r.URL, |
|
|
|
|
postStore: a.params.PostStore, |
|
|
|
|
gmiPublicURL: a.params.PublicURL, |
|
|
|
|
httpPublicURL: a.params.HTTPPublicURL, |
|
|
|
|
preprocessFuncs: preprocessFuncs, |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|