genlog/genlog.sh

79 lines
2.4 KiB
Bash
Raw Permalink Normal View History

2022-11-04 19:13:22 +01:00
#!/usr/bin/env bash
2023-08-19 02:10:12 +02:00
:' 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 🏴'
2023-08-18 01:56:52 +02:00
# some useful things
2022-11-04 19:13:22 +01:00
# https://sharats.me/posts/shell-script-best-practices/
set -o errexit
set -o pipefail
2022-03-12 01:00:58 +01:00
2023-08-18 01:56:52 +02:00
# creating a temporary directory for our working stuff
2022-04-13 23:35:38 +02:00
tempdir="$(mktemp -d)"
2022-03-12 01:00:58 +01:00
2023-08-18 01:56:52 +02:00
# checking args
2022-11-04 19:13:22 +01:00
if [[ -n "$1" ]]
2022-05-06 21:59:45 +02:00
then
2023-08-18 01:56:52 +02:00
# if there's an arg, using it as a working path
2022-05-06 21:59:45 +02:00
source_path="$1"
else
2023-08-18 01:56:52 +02:00
# if not, using the 'content' directory as working path
2022-05-07 17:43:35 +02:00
source_path=content
2022-05-06 21:59:45 +02:00
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"
2023-08-18 01:56:52 +02:00
# finding recursively all ".gmi" files on the working path
2022-05-06 21:59:45 +02:00
find "$source_path" -wholename "*.gmi" -type f | while read -r gmi_file
2022-03-12 01:00:58 +01:00
do
2023-08-18 01:56:52 +02:00
# deleting the '#' of the first line and save it as title
2022-05-06 23:52:16 +02:00
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")
2022-03-12 01:00:58 +01:00
2023-08-18 01:56:52 +02:00
# 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"
2022-03-12 01:00:58 +01:00
2023-08-18 01:56:52 +02:00
# convertig .gmi files in .html
/usr/local/bin/gmnitohtml < "$gmi_file" > "$tempdir/body.html"
2022-03-12 01:00:58 +01:00
2023-08-18 01:56:52 +02:00
# retrieving of the path of the current .gmi file
file_path="$(dirname "$gmi_file")"
2023-08-18 01:56:52 +02:00
# retrieving of the filename of the current .gmi file, without its extension
file_name="$(basename "$gmi_file" .gmi)"
2022-03-12 01:00:58 +01:00
2023-08-18 01:56:52 +02:00
# 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"
2022-03-12 01:00:58 +01:00
2023-08-18 01:56:52 +02:00
# i think it's all good
2022-05-06 22:27:41 +02:00
echo "OK: $title"
echo "$file_path/$file_name.html"
2022-03-12 01:00:58 +01:00
done
2023-08-18 01:56:52 +02:00
# removing the temporary directory, it's useless now
2022-05-06 22:41:59 +02:00
rm -r "$tempdir"
2022-03-12 01:00:58 +01:00
2023-08-18 01:56:52 +02:00
# this time it's really the end
2022-05-06 22:44:17 +02:00
echo "All done."