blob: 7666f8c08f6887d18367ca9c4bd1b7c384a1f111bf514869704df665afce1b2b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
{{ define "main" -}}
{{ $emoji := cond (.Page.Params.showlinkemoji | default true) (emojify ":link: ") "" -}}
=> {{ .Site.Home.RelPermalink | strings.TrimSuffix "index.gmi" }} {{ emojify ":link:" }} {{ i18n "returnHome" -}}
{{ if (.Page.Params.pageinfo | default true) }}
{{ partial "pageinfo.gmi" . -}}
{{ end }}
# {{ .Name }}
{{ $content := chomp (.RawContent) -}}
{{ $scratch := newScratch -}}
{{/* if content begins with a heading, add an extra newline */ -}}
{{ if (findRE `^#{1,6} ` $content 1) }}
{{ end -}}
{{/* split text into chunks, with each chunk containing 3 parts: above, within, and below preformatted text */ -}}
{{/* the regex matching below the preformatted text matches exactly once in the entire text */ -}}
{{ $chunks := findRESubmatch `(?sm)(?:(.+?)(^\x60{3}[^\x60].*?^\x60{3}$))|(.+$)` $content -}}
{{ $ref_index := 0 -}}
{{ range $chunks -}}
{{ $above_pre := index . 1 -}}
{{ $pre := index . 2 -}}
{{ $below_pre := index . 3 -}}
{{/* mux regex matches above and below preformatted text for rendering (the matches are mutually exclusive) */ -}}
{{ $rendered_text := print $above_pre $below_pre -}}
{{ if ($.Page.Params.makerefs | default true) -}}
{{/* find all the links within a chunk */ -}}
{{ $chunk_refs := findRESubmatch `!?\[[\t ]*(.+?)[\t ]*\]\([\t ]*(.+?)(?:[\t ]+"(.+?)")?[\t ]*\)` $rendered_text -}}
{{ range $chunk_refs -}}
{{ $ref_index = add $ref_index 1 -}}
{{ $chunk_ref := dict
"index" $ref_index
"text" (index . 1)
"link" (index . 2)
"title" (index . 3)
-}}
{{ $scratch.Add "refs" (slice $chunk_ref) -}}
{{ $ref_index := $chunk_ref.index -}}
{{ $ref_text := $chunk_ref.text -}}
{{/* create superscript of $ref_index */ -}}
{{ $superscript_map := dict
"0" "⁰"
"1" "¹"
"2" "²"
"3" "³"
"4" "⁴"
"5" "⁵"
"6" "⁶"
"7" "⁷"
"8" "⁸"
"9" "⁹"
-}}
{{ $ref_superscript := "" -}}
{{ range (split $ref_index "") -}}
{{ $ref_superscript = print $ref_superscript (index $superscript_map .) -}}
{{ end -}}
{{/* replace Markdown links with link text and their superscripted reference numbers */ -}}
{{ $rendered_text = replace $rendered_text (index . 0) (print $ref_text $ref_superscript) 1 -}}
{{ end -}}
{{ else -}}
{{/* render all links directly */ -}}
{{ $rendered_text = $rendered_text | replaceRE `(?m)^!?\[[\t ]*(.+?)[\t ]*\]\([\t ]*(.+?)(?:[\t ]+"(.+?)")?[\t ]*\)$` (print "=> $2 " $emoji "$1") -}}
{{ end -}}
{{/* trim extra heading symbols */ -}}
{{ $rendered_text = $rendered_text | replaceRE `(?m)^####{1,3} ` "### " -}}
{{/* convert Markdown alternative heading syntax to gemtext heading syntax */ -}}
{{ $rendered_text = $rendered_text | replaceRE `(?m)^[\t ]*(\S.+?)[\t ]*\n\=+$` "# $1" -}}
{{ $rendered_text = $rendered_text | replaceRE `(?m)^[\t ]*(\S.+?)[\t ]*\n\-+$` "## $1" -}}
{{/* convert Markdown alternative unordered list syntax to gemtext unordered list syntax */ -}}
{{ $rendered_text = $rendered_text | replaceRE `(?m)^[-+] ` "* " -}}
{{/* convert Markdown ordered list items to gemtext unordered list items */ -}}
{{ $rendered_text = $rendered_text | replaceRE `(?m)^[1-9]\d*\.[\t ]+(.*?)[\t ]*$` "* $1" -}}
{{/* remove bold and italics asterisk symbols */ -}}
{{/* note: this operation is performed in 3 steps to handle nested bold and italics */ -}}
{{/* note: this operation is performed last to avoid incorrectly rendering (un)ordered lists */ -}}
{{ $rendered_text = $rendered_text | replaceRE `\*{3}(\S(?:.*?\S)?)\*{3}` "$1" -}}
{{ $rendered_text = $rendered_text | replaceRE `\*{2}(\S(?:.*?\S)?)\*{2}` "$1" -}}
{{ $rendered_text = $rendered_text | replaceRE `\*{1}(\S(?:.*?\S)?)\*{1}` "$1" -}}
{{ $rendered_text = $rendered_text | emojify -}}
{{/* demux regex matches above and below preformatted text */ -}}
{{ if $above_pre -}}
{{ $rendered_text -}}
{{ $pre -}}
{{ else -}}
{{ $pre -}}
{{ $rendered_text -}}
{{ end -}}
{{ end -}}
{{ $refs := $scratch.Get "refs" -}}
{{ if and (.Page.Params.makerefs | default true) $refs }}
## {{ i18n "refs" }}
{{ range $refs -}}
{{ $ref_index := .index -}}
{{ $ref_text := .text -}}
{{ $ref_link := .link -}}
{{ $ref_title := .title -}}
{{/* render referenced links */}}
{{ print "=> " $ref_link (emojify " :link: [") $ref_index "]: " (cond (ne (len $ref_title) 0) $ref_title $ref_text) -}}
{{ end -}}
{{ end -}}
{{ end }}
|