aboutsummaryrefslogtreecommitdiff
path: root/http/handlers/gemtext.go
diff options
context:
space:
mode:
authorBrian Picciano <me@mediocregopher.com>2024-11-04 20:00:57 +0100
committerBrian Picciano <me@mediocregopher.com>2024-11-04 20:00:57 +0100
commit81bf0016b3a776732bba430872ce5719f3251e70 (patch)
tree33d922d3dd195193fceea4005f0d17bd823d5fdf /http/handlers/gemtext.go
parent7ea9fb22ab67603f4bc6eb68b76c66177b434fe3 (diff)
Add heading_template to gemtext middlewareHEADmain
Diffstat (limited to 'http/handlers/gemtext.go')
-rw-r--r--http/handlers/gemtext.go34
1 files changed, 33 insertions, 1 deletions
diff --git a/http/handlers/gemtext.go b/http/handlers/gemtext.go
index bc617f4..7f12799 100644
--- a/http/handlers/gemtext.go
+++ b/http/handlers/gemtext.go
@@ -64,6 +64,20 @@ type Gemtext struct {
//
TemplatePath string `json:"template"`
+ // Path to a template which will be used for rendering headings. If not
+ // given then headings will be rendered with appropriate HTML header tags.
+ //
+ // The template will be rendered with these extra data fields:
+ //
+ // ##### `.Level`
+ //
+ // Which level of heading is being rendered, 1, 2, or 3.
+ //
+ // ##### `.Text`
+ //
+ // The text of the heading.
+ HeadingTemplatePath string `json:"heading_template"`
+
// Path to a template which will be used for rendering links. If not given
// then links will be rendered using an anchor tag wrapped in a paragraph
// tag.
@@ -181,9 +195,23 @@ func (g *Gemtext) ServeHTTP(
Req: r,
RespHeader: templates.WrappedHeader{Header: rec.Header()},
}
+
+ parser gemtext.HTMLTranslator
)
- parser := gemtext.HTMLTranslator{}
+ if g.HeadingTemplatePath != "" {
+ parser.RenderHeading = func(w io.Writer, level int, text string) error {
+ payload := struct {
+ *templates.TemplateContext
+ Level int
+ Text string
+ }{
+ ctx, level, text,
+ }
+
+ return g.render(w, ctx, osFS, g.HeadingTemplatePath, payload)
+ }
+ }
if g.LinkTemplatePath != "" {
parser.RenderLink = func(w io.Writer, url, label string) error {
@@ -258,6 +286,10 @@ func gemtextParseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler,
if !h.Args(&g.TemplatePath) {
return nil, h.ArgErr()
}
+ case "heading_template":
+ if !h.Args(&g.HeadingTemplatePath) {
+ return nil, h.ArgErr()
+ }
case "link_template":
if !h.Args(&g.LinkTemplatePath) {
return nil, h.ArgErr()