From 31087c4349cee8922c4aa5d6a09196dd52529d10 Mon Sep 17 00:00:00 2001 From: David Eisinger Date: Sun, 16 Feb 2025 14:47:02 -0500 Subject: [PATCH] Format Gemini link list --- themes/v2/layouts/partials/gemtext.gmi | 80 ++++++++++++++++++-------- 1 file changed, 57 insertions(+), 23 deletions(-) diff --git a/themes/v2/layouts/partials/gemtext.gmi b/themes/v2/layouts/partials/gemtext.gmi index 525f76d..6ffb7fa 100644 --- a/themes/v2/layouts/partials/gemtext.gmi +++ b/themes/v2/layouts/partials/gemtext.gmi @@ -1,23 +1,57 @@ -{{ $content := .content -}} -{{ $page := .page -}} -{{ $content = $content | replaceRE `\n+` "" -}} -{{ $content = $content | replaceRE `\[([^\]]+)\]\[(\d+)\]` "$1[$2]" -}} -{{ $content = $content | replaceRE `(?m)^\[(\d+)\]: (.*)$` "=> $2 [$1] $2" -}} -{{ $content = $content | replaceRE `\[([^\]]+)\]\([^\)]+\)` "$1" -}} -{{ $content = $content | replaceRE `(?m)^ >` ">" -}} -{{ $content = $content | replaceRE `(?m)^ \*` "* ⇢" -}} -{{ $content = $content | replaceRE `(?m)^- ` "* " -}} -{{ $content = $content | replaceRE `(?m)^[\*-]{3}$` "✱ ✱ ✱" -}} -{{ $content = $content | replaceRE `=> (/.+\.pdf)` (printf "=> %s$1" site.BaseURL) -}} -{{ $audioRE := `` -}} -{{ range findRE $audioRE $content -}} -{{ $url := . | replaceRE $audioRE "$1" | replaceRE ` ` "%20" -}} -{{ $title := . | replaceRE $audioRE "$2" -}} -{{ $content = replace $content . (printf "=> %s %s" $url (substr $title 0 -4)) -}} -{{ end -}} -{{ range findRE `{{<(dither|thumbnail) [^/]+/>}}` $content -}} -{{ $rendered := $page.RenderString . -}} -{{ $url := $rendered | replaceRE `(?s).*src="([^"]+)".*` "=> $1" -}} -{{ $content = replace $content . $url -}} -{{ end -}} -{{ $content | safeHTML -}} +{{- $content := .content -}} +{{- $page := .page -}} + +{{- /* Convert link lists */ -}} +{{- $listPattern := `(?m)^\* \[([^\]]+)\]\[(\d+)\]$` -}} +{{- range findRE $listPattern $content }} + {{- /* Extract text and reference number */ -}} + {{- $fullMatch := . -}} + {{- $text := replaceRE $listPattern `$1` . -}} + {{- $refNum := replaceRE $listPattern `$2` . -}} + + {{- /* Step 2: Find the corresponding reference link */ -}} + {{- $refPattern := printf `(?m)^\[%s\]: (.+)$` $refNum -}} + {{- $refMatch := findRE $refPattern $content -}} + + {{- if gt (len $refMatch) 0 }} + {{- $url := replaceRE $refPattern `$1` (index $refMatch 0) -}} + + {{- /* Step 3: Replace original list item with => link format */ -}} + {{- $newLine := printf "=> %s %s" $url $text -}} + {{- $content = replace $content $fullMatch $newLine -}} + + {{- /* Step 4: Remove the reference definition */ -}} + {{- $content = replace $content (index $refMatch 0) "" -}} + {{- end -}} +{{- end -}} + +{{- /* Convert links */ -}} +{{- $content = $content | replaceRE `\[([^\]]+)\]\[(\d+)\]` "$1[$2]" -}} +{{- $content = $content | replaceRE `(?m)^\[(\d+)\]: (.*)$` "=> $2 [$1] $2" -}} +{{- $content = $content | replaceRE `\[([^\]]+)\]\([^\)]+\)` "$1" -}} + +{{- /* Replace audio tags */ -}} +{{- $audioRE := `` -}} +{{- range findRE $audioRE $content -}} + {{- $url := . | replaceRE $audioRE "$1" | replaceRE ` ` "%20" -}} + {{- $title := . | replaceRE $audioRE "$2" -}} + {{- $content = replace $content . (printf "=> %s %s" $url (substr $title 0 -4)) -}} +{{- end -}} + +{{- /* Photo processing */ -}} +{{- range findRE `{{<(dither|thumbnail) [^/]+/>}}` $content -}} + {{- $rendered := $page.RenderString . -}} + {{- $url := $rendered | replaceRE `(?s).*src="([^"]+)".*` "=> $1" -}} + {{- $content = replace $content . $url -}} +{{- end -}} + +{{- /* Misc. markup fixes */ -}} +{{- $content = $content | replaceRE `\n+` "" -}} +{{- $content = $content | replaceRE `(?m)^ >` ">" -}} +{{- $content = $content | replaceRE `(?m)^ \*` "* ⇢" -}} +{{- $content = $content | replaceRE `(?m)^- ` "* " -}} +{{- $content = $content | replaceRE `(?m)^[\*-]{3}$` "✱ ✱ ✱" -}} +{{- $content = $content | replaceRE `=> (/.+\.pdf)` (printf "=> %s$1" site.BaseURL) -}} +{{- $content = $content | replaceRE `(?m)\n{3,}` "\n\n" }} + +{{- $content | safeHTML -}}