blob: fa9080f8515dcc76da0b1d1890ba8db43a644106 (
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
|
# Convert specia characters to their HTML codes
html::encode () {
$SED '
s|\&|\&|g;
s|<|\<|g;
s|>|\>|g;
s|'\''|\'|g;
' <<< "$@"
}
# Make a HTML paragraph.
html::make_paragraph () {
local -r text="$1"; shift
if [ -n "$text" ]; then
echo "<span>$(html::encode "$text")</span><br />"
else
echo '<br />'
fi
}
# Make a HTML header.
html::make_heading () {
local -r text=$($SED -E 's/^#+ //' <<< "$1"); shift
local -r level="$1"; shift
local -r id="$(generate::internal_link_id "$text")"
echo "<h${level} style='display: inline' id='${id}'>$(html::encode "$text")</h${level}><br />"
}
# Make a HTML quotation
html::make_quote () {
local -r quote="${1/> }"
echo "<span class='quote'>$(html::encode "$quote")</span><br />"
}
# Make a HTML image
html::make_img () {
local link="$1"; shift
local descr="$1"; shift
if [ -z "$descr" ]; then
echo "<a href='$link'><img src='$link' /></a><br />"
else
echo "<a href='$link'><img alt='$descr' title='$descr' src='$link' /></a><br />"
fi
}
# Make a HTML hyperlink
html::make_link () {
local link="$1"; shift
local descr="$1"; shift
if ! $GREP -F -q '://' <<< "$link"; then
link=${link/.gmi/.html}
fi
if [[ -z "$descr" ]]; then
descr="$link"
fi
local mastodon_verify=''
if [[ "$link" = "$MASTODON_URI" ]]; then
mastodon_verify=" rel='me'"
fi
echo "<a class='textlink' href='$(html::encode "$link")'$mastodon_verify>$descr</a><br />"
}
html::process_inline () {
$SED -E "s|\`([^\`]+)\`|<span class='inlinecode'>\\1</span>|g"
}
html::theme () {
local -r html_base_dir="$CONTENT_BASE_DIR/html"
log INFO "Installing theme $HTML_THEME_DIR"
html::theme::styles "$html_base_dir"
html::theme::webfonts "$html_base_dir"
}
html::theme::styles () {
local -r html_base_dir="$1"; shift
log INFO 'Installing theme CSS files'
cp "$HTML_CSS_STYLE" "$html_base_dir/"
while read -r section_dir; do
local override_source="$HTML_THEME_DIR/style-$(basename "$section_dir")-override.css"
local override_dest="$section_dir/style-override.css"
if [ ! -f "$override_source" ]; then
touch "$override_dest" # Empty override
else
cp "$override_source" "$override_dest"
fi
done < <(find "$html_base_dir" -mindepth 1 -maxdepth 1 -type d | $GREP -E -v '(\.git)')
}
# Install theme webfonts, copying each configured font to the HTML output dir.
# Uses ${VAR:-} to safely handle potentially unset webfont variables under set -u.
html::theme::webfonts () {
local -r html_base_dir="$1"; shift
log INFO 'Installing theme webfonts'
if [ -f "${HTML_WEBFONT_TEXT:-}" ]; then
cp "$HTML_WEBFONT_TEXT" "$html_base_dir/text.ttf"
fi
if [ -f "${HTML_WEBFONT_HEADING:-}" ]; then
cp "$HTML_WEBFONT_HEADING" "$html_base_dir/heading.ttf"
elif [ -f "${HTML_WEBFONT_TEXT:-}" ]; then
cp "$HTML_WEBFONT_TEXT" "$html_base_dir/heading.ttf"
fi
if [ -f "${HTML_WEBFONT_CODE:-}" ]; then
cp "$HTML_WEBFONT_CODE" "$html_base_dir/code.ttf"
fi
if [ -f "${HTML_WEBFONT_HANDNOTES:-}" ]; then
cp "$HTML_WEBFONT_HANDNOTES" "$html_base_dir/handnotes.ttf"
fi
if [ -f "${HTML_WEBFONT_TYPEWRITER:-}" ]; then
cp "$HTML_WEBFONT_TYPEWRITER" "$html_base_dir/typewriter.ttf"
fi
}
html::source_highlight () {
local -r bare_text="$1"; shift
local -r language="$1"; shift
# Trim whitespace from language (handles cases like "``` ")
local -r lang_trimmed="$($SED -E 's/^ +| +$//g' <<< "$language")"
if [[ -z "$lang_trimmed" || -z "$SOURCE_HIGHLIGHT" ]]; then
echo '<pre>'
html::encode "$bare_text"
echo '</pre>'
else
if [[ "$lang_trimmed" == "AUTO" ]]; then
log WARN "GNU Source Highlight auto detection not yet supported!"
echo '<pre>'
html::encode "$bare_text"
echo '</pre>'
else
# Build command safely to avoid empty args and word splitting
local -a cmd=("$SOURCE_HIGHLIGHT" "--src-lang=$lang_trimmed")
if [ -n "$SOURCE_HIGHLIGHT_CSS" ]; then
cmd+=("--style-css-file=$SOURCE_HIGHLIGHT_CSS")
fi
if ! "${cmd[@]}" <<< "$bare_text" 2>/dev/null | $SED 's|<tt>||; s|</tt>||;'; then
# Fallback if source-highlight doesn't support the language
echo '<pre>'
html::encode "$bare_text"
echo '</pre>'
fi
fi
fi
}
html::list::encode () {
local text="$1"; shift
if [[ "$text" != '⇢ '* ]]; then
# No ToC
html::encode "$text"
return
fi
local -i toc_indent=0
# If there's a . (dot) in the liste element, it then indicates a ToC element
while [[ "$text" == '⇢ '* ]]; do
text="$($SED 's/⇢ //' <<< "$text")"
: $(( toc_indent++ ))
done
while [ $toc_indent -ge 2 ]; do
echo -n '⇢ '
: $(( toc_indent-- ))
done
local -r id="$(generate::internal_link_id "$text")"
echo "<a href='#$id'>$(html::encode "$text")</a>"
}
# Convert Gemtext to HTML
html::fromgmi () {
local is_list=no
local is_bare=no
local bare_text=''
local language=''
while IFS='' read -r line; do
if [[ "$is_list" == yes ]]; then
if [[ "$line" == '* '* ]]; then
echo "<li>$(html::list::encode "${line/\* /}")</li>" | html::process_inline
else
is_list=no
echo "</ul><br />"
fi
continue
elif [[ "$is_bare" == yes ]]; then
if [[ "$line" == '```'* ]]; then
html::source_highlight "$bare_text" "$language"
is_bare=no
bare_text=''
language=''
elif [ -z "$bare_text" ]; then
bare_text="$line"
else
bare_text="$bare_text
$line"
fi
continue
fi
case "$line" in
'* '*)
is_list=yes
echo "<ul>"
echo "<li>$(html::list::encode "${line/\* /}")</li>" | html::process_inline
;;
'```'*)
# Extract language after the opening backticks and trim spaces
language=$($SED -E 's/^```\s*//; s/\s+$//' <<< "$line")
is_bare=yes
;;
'# '*)
html::make_heading "$line" 1 | html::process_inline
;;
'## '*)
html::make_heading "$line" 2 | html::process_inline
;;
'### '*)
html::make_heading "$line" 3 | html::process_inline
;;
'> '*)
html::make_quote "$line" | html::process_inline
;;
'=> '*)
generate::make_link html "$line" | html::process_inline
;;
*)
html::make_paragraph "$line" | html::process_inline
;;
esac
done
}
html::test () {
local line='Hello world! This is a paragraph.'
assert::equals "$(html::make_paragraph "$line")" "<span>Hello world! This is a paragraph.</span><br />"
line=''
assert::equals "$(html::make_paragraph "$line")" '<br />'
line='Foo &<>& Bar!'
assert::equals "$(html::make_paragraph "$line")" "<span>Foo &<>& Bar!</span><br />"
line='echo foo 2>&1'
assert::equals "$(html::make_paragraph "$line")" "<span>echo foo 2>&1</span><br />"
line='# Header 1'
local id='header-1'
assert::equals "$(html::make_heading "$line" 1)" "<h1 style='display: inline' id='${id}'>Header 1</h1><br />"
line='## Header 2'
id='header-2'
assert::equals "$(html::make_heading "$line" 2)" "<h2 style='display: inline' id='${id}'>Header 2</h2><br />"
line='### Header 3'
id='header-3'
assert::equals "$(html::make_heading "$line" 3)" "<h3 style='display: inline' id='${id}'>Header 3</h3><br />"
line='> This is a quote'
assert::equals "$(html::make_quote "$line")" "<span class='quote'>This is a quote</span><br />"
MASTODON_URI=''
line='Hello world! This is a paragraph.'
assert::equals "$(html::make_paragraph "$line")" '<span>Hello world! This is a paragraph.</span><br />'
line=''
assert::equals "$(html::make_paragraph "$line")" '<br />'
line='Foo &<>& Bar!'
assert::equals "$(html::make_paragraph "$line")" '<span>Foo &<>& Bar!</span><br />'
line='echo foo 2>&1'
assert::equals "$(html::make_paragraph "$line")" '<span>echo foo 2>&1</span><br />'
line='> This is a quote'
assert::equals "$(html::make_quote "$line")" "<span class='quote'>This is a quote</span><br />"
line='Testing: `hello_world.sh --debug` :-) `another one`!'
assert::equals "$(echo "$line" | html::process_inline)" \
"Testing: <span class='inlinecode'>hello_world.sh --debug</span> :-) <span class='inlinecode'>another one</span>!"
line='=> https://example.org'
assert::equals "$(generate::make_link html "$line")" \
"<a class='textlink' href='https://example.org'>https://example.org</a><br />"
line="=> https://example.org/foo'bar"
assert::equals "$(generate::make_link html "$line")" \
"<a class='textlink' href='https://example.org/foo'bar'>https://example.org/foo'bar</a><br />"
line='=> index.html'
assert::equals "$(generate::make_link html "$line")" \
"<a class='textlink' href='index.html'>index.html</a><br />"
line='=> http://example.org Description of the link'
assert::equals "$(generate::make_link html "$line")" \
"<a class='textlink' href='http://example.org'>Description of the link</a><br />"
# Test Mastodon verification.
MASTODON_URI='https://fosstodon.org/@snonux'
line='=> https://fosstodon.org/@snonux Me at Mastodon'
assert::equals "$(generate::make_link html "$line")" \
"<a class='textlink' href='https://fosstodon.org/@snonux' rel='me'>Me at Mastodon</a><br />"
MASTODON_URI=''
line='=> http://example.org/image.png'
assert::equals "$(generate::make_link html "$line")" \
"<a href='http://example.org/image.png'><img src='http://example.org/image.png' /></a><br />"
line='=> http://example.org/image.png Image description'
assert::equals "$(generate::make_link html "$line")" \
"<a href='http://example.org/image.png'><img alt='Image description' title='Image description' src='http://example.org/image.png' /></a><br />"
local input_block='```
this
is
a
bare block
```'
local output_block='<pre>
this
is
a
bare block
</pre>'
assert::equals "$(html::fromgmi <<< "$input_block")" "$output_block"
# Trailing space after fence should not trigger source-highlight
input_block='```
code
```'
output_block='<pre>
code
</pre>'
assert::equals "$(html::fromgmi <<< "$input_block")" "$output_block"
if [ -n "$SOURCE_HIGHLIGHT" ]; then
input_block='```bash
if [ -z $foo ]; then
echo $foo
fi
```'
assert::contains "$(html::fromgmi <<< "$input_block")" 'GNU source-highlight'
fi
}
|