aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md16
-rw-r--r--example/tpl/render_gemtext.html5
-rw-r--r--http/handlers/templates/functions/gemtext.go23
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
+ `<div>` or `<body>`.
+
+* `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) }}
<!DOCTYPE html>
<html>
<head>
- <title>TODO Title</title>
+ <title>{{ $gemtextRes.Title | default "Example Gemtext File" }}</title>
<link rel="stylesheet" type="text/css" href="/bamboo.css" />
</head>
<body>
- {{ gemtext (include $filePath) }}
+ {{ $gemtextRes.Body }}
</body>
</html>
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("<h2>%s</h2>\n", sanitizeText(line[2:]))
case strings.HasPrefix(line, "#"):
- write("<h1>%s</h1>\n", sanitizeText(line[1:]))
+ line = sanitizeText(line[1:])
+ if title == "" {
+ title = line
+ }
+ write("<h1>%s</h1>\n", line)
case strings.HasPrefix(line, ">"):
write("<blockquote>%s</blockquote>\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.