davideisinger.com

My personal website
Log | Files | Refs | README

gemtext.gmi (3637B)


      1 {{- $content := .content -}}
      2 {{- $page := .page -}}
      3 
      4 {{- /* Promote standalone list links, with an optional attribution link. */ -}}
      5 {{- $references := dict -}}
      6 {{- $refPattern := `(?m)^\[(\d+)\]:[ \t]+(.+)$` -}}
      7 {{- range findRE $refPattern $content -}}
      8     {{- $references = merge $references (dict (replaceRE $refPattern `$1` .) (replaceRE $refPattern `$2` . | strings.TrimSpace)) -}}
      9 {{- end -}}
     10 {{- $listPattern := `(?m)^[*-] \[([^\]]+)\]\[(\d+)\](?:[ 	]+\(\[via\]\[(\d+)\]\))?[ 	]*$` -}}
     11 {{- $lines := slice -}}
     12 {{- range split $content "\n" -}}
     13     {{- $line := . -}}
     14     {{- if findRE $listPattern . -}}
     15         {{- $text := replaceRE $listPattern `$1` . -}}
     16         {{- $refNum := replaceRE $listPattern `$2` . -}}
     17         {{- $viaNum := replaceRE $listPattern `$3` . -}}
     18         {{- $url := index $references $refNum -}}
     19         {{- $viaURL := index $references $viaNum -}}
     20         {{- if and $url (or (eq $viaNum "") $viaURL) -}}
     21             {{- $newLine := printf "=> %s %s" $url $text -}}
     22             {{- with $viaURL -}}
     23                 {{- $label := (urls.Parse .).Host | default . -}}
     24                 {{- $newLine = printf "%s\n=> %s via %s" $newLine . $label -}}
     25             {{- end -}}
     26             {{- $line = $newLine -}}
     27         {{- end -}}
     28     {{- end -}}
     29     {{- $lines = $lines | append $line -}}
     30 {{- end -}}
     31 
     32 {{- $content = delimit $lines "\n" -}}
     33 
     34 {{- /* Replace audio tags */ -}}
     35 {{- $audioRE := `<audio controls src="(.*/([^"]+))"></audio>` -}}
     36 {{- range findRE $audioRE $content -}}
     37 	{{- $url := . | replaceRE $audioRE "$1" | replaceRE ` ` "%20" -}}
     38 	{{- $title := . | replaceRE $audioRE "$2" -}}
     39 	{{- $content = replace $content . (printf "=> %s %s" $url (substr $title 0 -4)) -}}
     40 {{- end -}}
     41 
     42 {{- /* Render image descriptions before processing prose links. */ -}}
     43 {{- $imagePattern := `(?s){{<(?:dither|thumbnail)\s[^>]*[^/]>}}.*?{{</(?:dither|thumbnail)>}}|{{<(?:dither|thumbnail)\s[^>]*?/>}}` -}}
     44 {{- range findRE $imagePattern $content -}}
     45 	{{- $rendered := $page.RenderString . -}}
     46 	{{- $url := $rendered | replaceRE `(?s).*src="([^"]+)".*` "$1" -}}
     47 	{{- $label := "" -}}
     48 	{{- with findRE `\balt="[^"]*"` $rendered 1 -}}
     49 		{{- $label = index . 0 | replaceRE `^alt="([^"]*)"$` "$1" -}}
     50 	{{- end -}}
     51 	{{- if not $label -}}
     52 		{{- with findRE `(?s)<figcaption>.*?</figcaption>` $rendered 1 -}}
     53 			{{- $label = index . 0 -}}
     54 		{{- end -}}
     55 	{{- end -}}
     56 	{{- $label = $label | plainify | htmlUnescape | replaceRE `\s+` " " | strings.TrimSpace -}}
     57 	{{- $link := printf "=> %s" $url -}}
     58 	{{- with $label -}}
     59 		{{- $link = printf "%s Image: %s" $link . -}}
     60 	{{- end -}}
     61 	{{- $content = replace $content . $link -}}
     62 {{- end -}}
     63 
     64 {{- /* Place descriptive links after the paragraph that uses them. */ -}}
     65 {{- $content = partial "gemtext-links.gmi" (dict "content" $content "references" $references "page" $page) -}}
     66 
     67 {{- /* Misc. markup fixes */ -}}
     68 {{- $content = $content | replaceRE `<!--more-->\n+` "" -}}
     69 {{- $content = $content | replaceRE `(?m)^[ ]+>` ">" -}}
     70 {{- $content = $content | replaceRE `(?m)^  \*` "* ⇢" -}}
     71 {{- $content = $content | replaceRE `(?m)^- ` "* " -}}
     72 {{- $content = $content | replaceRE `(?m)^[\*-]{3}$` "✱ ✱ ✱" -}}
     73 {{- $content = $content | replaceRE `=> /(.+\.pdf)` (printf "=> %s$1" site.BaseURL) -}}
     74 {{- $content = $content | replaceRE `=> /(elsewhere[^ ])` (printf "=> %s$1" site.BaseURL) -}}
     75 {{- $content = $content | replaceRE `(?m)^(\* [^\n]+)\n\n  ` "$1 " -}}
     76 {{- $content = $content | replaceRE `(?m)\n{3,}` "\n\n" }}
     77 {{- $content = $content | replaceRE `<abbr[^>]*>([^<]+)</abbr>` "$1" -}}
     78 {{- $content = $content | replaceRE `\n+$` "" }}
     79 
     80 {{- $content | safeHTML -}}