-
Notifications
You must be signed in to change notification settings - Fork 0
/
genlog.sh
executable file
·78 lines (54 loc) · 2.42 KB
/
genlog.sh
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
#!/usr/bin/env bash
:' LICENCE
THE "HOT CHOCOLATE LICENSE ☕" (HCL revision 1312.2):
OniriCorpe wrote this file. As long as you retain this
notice and you are an anarchist/communist who supports oppressed and
marginalized groups (ie and not exclusively queer folks, BIPOC, intersex,
disabled comrades, etc), you can do whatever you want with this stuff.
If we meet some day, and you think this stuff is worth it, you can buy me a
hot chocolate or any other non-alcoholic drink that suits me in return.
OniriCorpe 🏴'
# some useful things
# https://sharats.me/posts/shell-script-best-practices/
set -o errexit
set -o pipefail
# creating a temporary directory for our working stuff
tempdir="$(mktemp -d)"
# checking args
if [[ -n "$1" ]]
then
# if there's an arg, using it as a working path
source_path="$1"
else
# if not, using the 'content' directory as working path
source_path=content
fi
# get the current date for the footer (UTC and ISO 8601 with hours and minutes)
date=$(date -u --iso-8601=minutes)
sed "s/GEN_DATE/${date}/" html/footer.html > "$tempdir/footer.html"
# finding recursively all ".gmi" files on the working path
find "$source_path" -wholename "*.gmi" -type f | while read -r gmi_file
do
# deleting the '#' of the first line and save it as title
title="$(sed -n '1{s/# //p}' "$gmi_file") $2"
# escaping eventual '&' (otherwise causing a bug where it is replaced by '<\!-- TITLE -->')
title=$(echo "$title" | sed "s#&#\\\&#g")
# in the header.html, replacing the "<\-- TITLE -->" by the previously
# saved title, and save the modified file in our temporary directory
sed "s#<\!-- TITLE -->#${title}#" html/header.html > "$tempdir/header.html"
# convertig .gmi files in .html
/usr/local/bin/gmnitohtml < "$gmi_file" > "$tempdir/body.html"
# retrieving of the path of the current .gmi file
file_path="$(dirname "$gmi_file")"
# retrieving of the filename of the current .gmi file, without its extension
file_name="$(basename "$gmi_file" .gmi)"
# assembling the header, the converted page and the footer and saving it in the working path
cat "$tempdir/header.html" "$tempdir/body.html" "$tempdir/footer.html" > "$file_path/$file_name.html"
# i think it's all good
echo "OK: $title"
echo " ⤷ $file_path/$file_name.html"
done
# removing the temporary directory, it's useless now
rm -r "$tempdir"
# this time it's really the end
echo "All done."