genlog/genlog.sh

64 lines
1.7 KiB
Bash
Raw Normal View History

2022-11-04 19:13:22 +01:00
#!/usr/bin/env bash
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
2023-08-18 01:56:52 +02:00
# generating the current date for the footer
date="$(date)"
2022-05-07 17:43:35 +02:00
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"
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
2022-05-07 17:43:35 +02:00
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."