From 0134492489ac70306486678aae81192d9cc0228d Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Thu, 4 Jul 2024 17:36:48 +0200 Subject: Split gemtext function output into Body and Title --- README.md | 16 ++++++++++++++-- example/tpl/render_gemtext.html | 5 +++-- http/handlers/templates/functions/gemtext.go | 23 ++++++++++++++++++----- 3 files changed, 35 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 092f348..a914f51 100644 --- a/README.md +++ b/README.md @@ -25,8 +25,20 @@ templates { } ``` -See the `template.localhost` virtual host in `example/Caddyfile` for an example -of using the `gemtext` template to render a gemtext file within an HTML file. +Within a template being rendered the `gemtext` function will be available and +can be passed any string. The function will return a struct with the following +fields: + +* `Body`: The result of converting each line of the input string into an + equivalent line of HTML. This will not include any wrapping HTML tags like + `
` or ``. + +* `Title`: A suggested title, based on the first `# Header` line found in the + gemtext input. + +See the `template.localhost` virtual host in `example/Caddyfile`, and the +associated `example/tpl/render_gemtext.html` template file, for an example of +how to use the template function. [gemtext]: https://geminiprotocol.net/docs/gemtext.gmi [mdfunc]: https://caddyserver.com/docs/modules/http.handlers.templates#markdown diff --git a/example/tpl/render_gemtext.html b/example/tpl/render_gemtext.html index 34a8c7c..72e35a4 100644 --- a/example/tpl/render_gemtext.html +++ b/example/tpl/render_gemtext.html @@ -2,13 +2,14 @@ {{ $base := last $pathSplit | default "index.gmi" }} {{ $filePath := append (initial $pathSplit) $base | join "/" | printf "static%s" }} {{ if not (fileExists $filePath) }}{{ httpError 404 }}{{ end }} +{{ $gemtextRes := gemtext (include $filePath) }} - TODO Title + {{ $gemtextRes.Title | default "Example Gemtext File" }} - {{ gemtext (include $filePath) }} + {{ $gemtextRes.Body }} diff --git a/http/handlers/templates/functions/gemtext.go b/http/handlers/templates/functions/gemtext.go index 5419290..57ea751 100644 --- a/http/handlers/templates/functions/gemtext.go +++ b/http/handlers/templates/functions/gemtext.go @@ -51,10 +51,16 @@ func sanitizeText(str string) string { return html.EscapeString(strings.TrimSpace(str)) } -func (*Gemtext) funcGemtext(input any) (string, error) { +type gemtextResult struct { + Title string + Body string +} + +func (*Gemtext) funcGemtext(input any) (gemtextResult, error) { var ( r = bufio.NewReader(strings.NewReader(caddy.ToString(input))) w = new(bytes.Buffer) + title string pft, list bool writeErr error ) @@ -69,7 +75,7 @@ func (*Gemtext) funcGemtext(input any) (string, error) { loop: for { if writeErr != nil { - return "", fmt.Errorf("writing line: %w", writeErr) + return gemtextResult{}, fmt.Errorf("writing line: %w", writeErr) } line, err := r.ReadString('\n') @@ -79,7 +85,7 @@ loop: break loop case err != nil: - return "", fmt.Errorf("reading next line: %w", err) + return gemtextResult{}, fmt.Errorf("reading next line: %w", err) case strings.HasPrefix(line, "```"): if !pft { @@ -132,7 +138,11 @@ loop: write("

%s

\n", sanitizeText(line[2:])) case strings.HasPrefix(line, "#"): - write("

%s

\n", sanitizeText(line[1:])) + line = sanitizeText(line[1:]) + if title == "" { + title = line + } + write("

%s

\n", line) case strings.HasPrefix(line, ">"): write("
%s
\n", sanitizeText(line[1:])) @@ -143,7 +153,10 @@ loop: } } - return w.String(), nil + return gemtextResult{ + Title: title, + Body: w.String(), + }, nil } // UnmarshalCaddyfile implements caddyfile.Unmarshaler. -- cgit v1.2.3