summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicholas Johnson <nick@nicholasjohnson.ch>2023-01-24 00:00:00 +0000
committerNicholas Johnson <nick@nicholasjohnson.ch>2023-01-24 00:00:00 +0000
commit3fb3e566afd980915e0b95022aa18c87eedb0438a36c8f8e6b9e03d0386dd360 (patch)
tree04090b3b5b318ee7c2ea3d34f8afd0d04b1af96dbc7f0b25ddaa3123605a779f
parent8cf654fae391eb99d75a9d89750ec7acf1cd5fad60f60846adf2265fd868bb18 (diff)
downloadhugo-theme-journal-0.3.0.tar.gz
hugo-theme-journal-0.3.0.zip
Rewrite Gemini chunk and link parsing logicv0.3.0
Improved the chunk-finding regex and added safeguards to prevent unintended behavior. Chunk rendering logic had a bug. If the pre-formatted text was identical to the regular text, it would be wrongly overwritten. To resolve this, the find-and-replace-style logic was removed. The new logic splits chunks into parts, renders them, then reassembles them in in order.
-rw-r--r--layouts/_default/single.gmi29
1 files changed, 20 insertions, 9 deletions
diff --git a/layouts/_default/single.gmi b/layouts/_default/single.gmi
index 2ea32cd..ce0d0ce 100644
--- a/layouts/_default/single.gmi
+++ b/layouts/_default/single.gmi
@@ -9,39 +9,50 @@
# {{ .Name | safeHTML }}
{{ $content := .RawContent }}
{{- $scratch := newScratch }}
-{{- $chunks := findRE `(?s)(.*?)(?:(?:\n|^)\x60{3}.*?\n\x60{3}(?:\n|$)|$)` $content }}
+{{- $chunks := findRE `(?sm)(?:(.*?)(^\x60{3}.*?^\x60{3}$))|(.*$)` $content }}
{{- range $chunks }}
- {{- $before_chunk := (. | replaceRE `(?s)(.*?)(?:(?:(?:\n|^)\x60{3}.*?\n\x60{3}(?:\n|$))|$)` "$1") }}
- {{- $cur_chunk := $before_chunk }}
+ {{- $before_pre := replaceRE `(?sm)(?:(.*?)(^\x60{3}.*?^\x60{3}$))|(.*$)` "$1" . 1 }}
+ {{- $pre := replaceRE `(?sm)(?:(.*?)(^\x60{3}.*?^\x60{3}$))|(.*$)` "$2" . 1 }}
+ {{- $after_pre := replaceRE `(?sm)(?:(.*?)(^\x60{3}.*?^\x60{3}$))|(.*$)` "$3" . 1 }}
+
+ {{- $cur_chunk := print $before_pre $after_pre }}
{{- $scratch.Set "refs" (findRE `\[.+?\]\(.+?\)` $cur_chunk) }}
{{- $refs := $scratch.Get "refs" }}
{{- if ($.Page.Params.makerefs | default true) }}
{{- range $ref_index, $ref_val := $refs }}
{{- $ref_num := add $ref_index 1 }}
- {{- $ref_text := $ref_val | replaceRE `\[(.+?)\]\((.+?)\)` "$1" }}
+ {{- $ref_text := replaceRE `\[(.+?)\]\((.+?)\)` "$1" $ref_val 1 }}
{{- $cur_chunk = replace $cur_chunk $ref_val (print $ref_text "[" $ref_num "]") 1 }}
{{- end }}
{{- else }}
- {{- $cur_chunk = $cur_chunk | replaceRE `(?m)\[(.+?)\]\((.+?)\)` (print "=> $2 " $emoji "$1") }}
+ {{- $cur_chunk = $cur_chunk | replaceRE `(?m)^\[(.+?)\]\((.+?)\)$` (print "=> $2 " $emoji "$1") }}
{{- end }}
{{- $cur_chunk = $cur_chunk | replaceRE `(?m)^####{1,3}` "###" }}
{{- $cur_chunk = $cur_chunk | replaceRE `\*{3}(\S.+?\S)\*{3}|\*{2}(\S.+?\S)\*{2}|\*{1}(\S.+?\S)\*{1}` "$1$2$3" }}
{{- $cur_chunk = $cur_chunk | replaceRE `(?m)^- ` "* " }}
{{- $cur_chunk = $cur_chunk | emojify }}
- {{- $content = replace $content $before_chunk $cur_chunk 1 }}
+
+ {{- if $before_pre }}
+ {{- $before_pre = $cur_chunk }}
+ {{- $before_pre | safeHTML }}
+ {{- $pre | safeHTML }}
+ {{- else }}
+ {{- $after_pre = $cur_chunk }}
+ {{- $pre | safeHTML }}
+ {{- $after_pre | safeHTML }}
+ {{- end }}
{{- end }}
-{{- $content | safeHTML }}
{{- $refs := $scratch.Get "refs" }}
{{- if and (.Page.Params.makerefs | default true) $refs }}
## Links
{{- range $ref_index, $ref_val := $refs }}
{{- $ref_num := add $ref_index 1 }}
- {{- $ref_text := $ref_val | replaceRE `\[(.+?)\]\((.+?)\)` "$1" }}
- {{- $ref_link := $ref_val | replaceRE `\[(.+?)\]\((.+?)\)` "$2" }}
+ {{- $ref_text := replaceRE `\[(.+?)\]\((.+?)\)` "$1" $ref_val 1 }}
+ {{- $ref_link := replaceRE `\[(.+?)\]\((.+?)\)` "$2" $ref_val 1 }}
{{ print "=> " $ref_link " " $emoji $ref_num ": " $ref_text | safeHTML }}
{{- end }}
{{- end }}