web/generate.sh

186 lines
5.5 KiB
Bash
Raw Normal View History

2022-03-05 21:24:31 +00:00
#!/bin/bash
2022-05-13 23:51:28 +00:00
################################################################################
2022-03-05 21:24:31 +00:00
# Génère les fichiers HTML de mon site
#
# Copyright (C) 2022 rick G. <rick@gnous.eu>
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# this program. If not, see <https://www.gnu.org/licenses/>.
2022-05-13 23:51:28 +00:00
################################################################################
2022-03-05 21:24:31 +00:00
# liste des fichiers html à générer
2023-01-12 23:28:59 +00:00
#files="autres.html copaings.html index.html ou.html projets.html rss.html apropos.html"
files="lang"
tmpHeader="/tmp/header.html"
2022-04-02 14:02:09 +00:00
annexes="css img"
2022-03-05 21:24:31 +00:00
2022-04-03 01:13:47 +00:00
pathLinks="/home/site/a/"
2022-03-05 21:24:31 +00:00
target="www"
2023-01-12 23:28:59 +00:00
defaultLang="fr"
2022-03-05 21:24:31 +00:00
header="header.html"
footer="footer.html"
org=1
2022-05-13 23:51:28 +00:00
################################################################################
# Génère un message d'aide
################################################################################
function print_help {
2023-01-12 23:28:59 +00:00
echo "usage : generate.sh [options] [<dossier>]"
echo -e "\tgénère les fichiers HTML du site. Il est possible de changer"
echo -e "\tle dossier de destination (www par défaut)."
echo
echo -e "\t-h\tgénère ce message et arrête le script."
2023-01-12 23:28:59 +00:00
echo -e "\t-c\tnettoie les fichiers générés"
echo -e "\t-o\tne génère pas les fichiers org"
2023-01-12 23:28:59 +00:00
echo -e "\t-n\tgénère la langue dans le nom du fichier et non dans le chemin."
echo -e "\t\t(example.org/fr/index.html -> example.org/index.fr.html)"
echo -e "\t-d\tdéfini la langue par défaut (son index sera mit à la racine)"
echo -e "\t-t\tlance un docker pour pouvoir tester le site"
}
###############################################################################
# Génère un fichier header propre à une langue
#
# $1 : le chemin vers le fichier header.link
###############################################################################
function generate_header {
cat $header > $tmpHeader
subtitle=$(sed -n -e "/%subtitle%/,/%endsubtitle%/p" $1 | sed -n -e "2p")
sed -i -e "s/%subtitle%/$subtitle/g" $tmpHeader
awk 'BEGIN {
FS = ":"
code = 0
}
{
if ($0 ~ /^%links%$/) {
code = 1
next
} else if ($0 ~ /^%endlinks%$/) {
code = 0
}
if (code && $1 !~ /^#/) {
gsub(/ $/, "", $1); gsub(/^ /, "", $2)
print " <a href=\""$1"\">"$2"</a>"
}
}' lang/fr/header.link >> $tmpHeader
echo -e " </div>\n" >> $tmpHeader
}
# vérification des arguments passés
if [[ $@ =~ "-h" ]]
then
print_help
exit 0
fi
2023-01-12 23:28:59 +00:00
if [[ $@ =~ "-c" ]]
then
rm -rf www
exit 0
fi
if [[ $@ =~ "-t" ]]
then
docker build . --tag site && echo -e "\n\033[32mC-c pour quitter\033[0m\n" || exit 1
docker run --rm -p 8080:80 site
exit 0
fi
if [[ $@ =~ "-o" ]]
then
org=0
fi
2022-03-05 21:24:31 +00:00
2022-04-02 14:02:09 +00:00
if [ -d $target ]
then
# TODO demander à l'utilisateur
rm -rf $target
fi
mkdir $target
2023-01-12 23:28:59 +00:00
for l in $(ls $files)
2022-03-05 21:24:31 +00:00
do
2023-01-12 23:28:59 +00:00
generate_header lang/$l/header.link
echo "Create folder $l in $target..."
mkdir "$target/$l"
for i in $(ls $files/$l)
do
echo "[$l] Generate $i..."
file="$target/$l/$i"
#cat $header > $file
cat $tmpHeader > $file
# on extrait le bloc contenant les link, on les enlève et rajoute <link />
# TODO pouvoir mettre plusieurs lignes link
newHeader=$(sed -n -e "/%link%/,/%endlink%/p" $files/$l/$i | sed -e "/%link%/d;/%endlink%/d" -e 's/\//\\\//g' -e "i<link " -e 'a \\\/>' | tr -d "\n")
# je suppose que s'il y a moins de 10 caractères, alors on ne prend
# pas en compte le nouveau link.
if [ $(echo $newHeader | wc -c) -lt 10 ]
then
sed -i -e "/%links%/d" $file
else
# pour bien aligner
newHeader="\ \ \ \ \ \ \ \ $newHeader"
sed -i -e "/%links%/a$newHeader/" $file
fi
cat $files/$l/$i >> $file
cat $footer >> $file
# nettoyage des balises précédemment utilisées
2023-01-12 00:23:59 +00:00
sed -i -e "/%links%/d" $file
2023-01-12 23:28:59 +00:00
sed -i -e "/%link%/,/%endlink%/d" $file
sed -i -e "s/img alt=\"$l/img id=\"choose\" alt=\"$l/" $file
sed -i -e "s/%file%/$i/" $file
sed -i -e "s/%lang%/$l/" $file
done
2022-03-05 21:24:31 +00:00
done
2022-04-02 14:02:09 +00:00
2023-01-13 16:57:31 +00:00
echo "Copy $annexes in $target..."
2022-04-02 14:02:09 +00:00
cp -t $target -r $annexes
2022-04-02 14:04:50 +00:00
2023-01-13 16:57:31 +00:00
echo "Generate default index page..."
cp $target/$defaultLang/index.html $target
for c in $(awk 'BEGIN {
FS = "\n";
code = 0
}
{
if ($0 ~ /id="header-links"/) {
code = 1
next
} else if ($0 ~ /\/div/) {
code = 0
} if (code) {
print NR
}
}' www/index.html)
do
sed -i -e "$c s/href=\"/href=\"$defaultLang\//" $target/index.html
done
2022-04-03 01:13:47 +00:00
echo "Link Links (lul)"
ln -s $pathLinks $target
if [ $org -eq 1 ]
then
echo "Generate org files..."
emacs -u $USER --script publish.el
fi