2023-02-22 20:24:51 +01:00
|
|
|
#!/usr/bin/env bash
|
2022-03-05 22:24:31 +01:00
|
|
|
|
2022-05-14 01:51:28 +02:00
|
|
|
################################################################################
|
2022-03-05 22:24:31 +01: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-14 01:51:28 +02:00
|
|
|
################################################################################
|
2022-03-05 22:24:31 +01:00
|
|
|
|
2023-01-17 23:32:47 +01:00
|
|
|
set -e
|
|
|
|
|
2022-03-05 22:24:31 +01:00
|
|
|
# liste des fichiers html à générer
|
2023-01-17 23:32:47 +01:00
|
|
|
langDir="lang"
|
2023-01-13 00:28:59 +01:00
|
|
|
tmpHeader="/tmp/header.html"
|
2022-04-02 16:02:09 +02:00
|
|
|
annexes="css img"
|
2022-03-05 22:24:31 +01:00
|
|
|
|
2022-04-03 03:13:47 +02:00
|
|
|
pathLinks="/home/site/a/"
|
2022-03-05 22:24:31 +01:00
|
|
|
target="www"
|
2023-01-13 00:28:59 +01:00
|
|
|
defaultLang="fr"
|
2022-03-05 22:24:31 +01:00
|
|
|
header="header.html"
|
|
|
|
footer="footer.html"
|
2022-05-14 01:47:50 +02:00
|
|
|
org=1
|
|
|
|
|
2022-05-14 01:51:28 +02:00
|
|
|
################################################################################
|
|
|
|
# Génère un message d'aide
|
|
|
|
################################################################################
|
2022-05-14 01:47:50 +02:00
|
|
|
function print_help {
|
2023-02-14 23:12:38 +01:00
|
|
|
cat << EOF
|
|
|
|
Utilisation :
|
2023-02-23 00:56:24 +01:00
|
|
|
$(basename $0) [options] [<dossier>]
|
2023-02-14 23:12:38 +01:00
|
|
|
|
|
|
|
génère les fichiers HTML du site. Il est possible de changer le dossier de
|
2023-02-23 00:56:24 +01:00
|
|
|
destination ($target par défaut).
|
2023-02-14 23:12:38 +01:00
|
|
|
|
|
|
|
Options :
|
2023-02-23 00:56:24 +01:00
|
|
|
-h génère ce message et arrête le script
|
2023-02-14 23:12:38 +01:00
|
|
|
-c nettoie les fichiers générés
|
|
|
|
-o ne génère pas les fichiers org
|
|
|
|
-t lance un docker pour pouvoir tester le site
|
|
|
|
|
|
|
|
Les options suivantes seront codées dans un futur proche :
|
2023-02-23 00:56:24 +01:00
|
|
|
-d défini la langue par défaut (son index sera mit à la racine);
|
|
|
|
fr par défaut
|
|
|
|
-n génère la langue dans le nom du fichier et non dans le chemin
|
2023-02-14 23:12:38 +01:00
|
|
|
(example.org/fr/index.html -> example.org/index.fr.html)
|
|
|
|
EOF
|
2023-01-13 00:28:59 +01:00
|
|
|
}
|
|
|
|
|
2023-02-23 00:56:24 +01:00
|
|
|
# pas besoin de continuer si on demande juste l'aide
|
|
|
|
if [[ $@ =~ "-h" ]]
|
|
|
|
then
|
|
|
|
print_help
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
# vérification des arguments passés
|
|
|
|
while [ -n "$1" ]
|
|
|
|
do
|
|
|
|
case $1 in
|
|
|
|
"-o") # pas de génération des fichiers org
|
|
|
|
org=0
|
|
|
|
;;
|
|
|
|
"-c") # on nettoie les fichiers
|
|
|
|
# TODO si on précise un dossier, nettoyer ce dossier
|
|
|
|
echo "Nettoyage de $target"
|
|
|
|
rm -rf $target
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
"-t") # lancer un docker de test
|
|
|
|
docker build . --tag site && echo -e \
|
|
|
|
"\n\033[32mSite de test déployé sur l'adresse" \
|
|
|
|
"localhost:8080\033[0m" || exit 1
|
|
|
|
echo -e "\033[32mC-c pour quitter\033[0m\n"
|
|
|
|
|
|
|
|
docker run --rm -p 8080:80 site
|
|
|
|
docker rmi site
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
"-d") ;&
|
|
|
|
"-n")
|
|
|
|
echo "Commande non implémentée."
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
"-"*)
|
|
|
|
print_help
|
|
|
|
exit 2
|
|
|
|
;;
|
|
|
|
*) target=$1 ;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
shift
|
|
|
|
done
|
|
|
|
|
2023-01-13 00:28:59 +01:00
|
|
|
###############################################################################
|
|
|
|
# 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>"
|
|
|
|
}
|
2023-01-13 18:21:20 +01:00
|
|
|
}' $1 >> $tmpHeader
|
2023-01-13 00:28:59 +01:00
|
|
|
echo -e " </div>\n" >> $tmpHeader
|
2022-05-14 01:47:50 +02:00
|
|
|
}
|
|
|
|
|
2022-04-02 16:02:09 +02:00
|
|
|
if [ -d $target ]
|
|
|
|
then
|
|
|
|
# TODO demander à l'utilisateur
|
|
|
|
rm -rf $target
|
|
|
|
fi
|
|
|
|
|
|
|
|
mkdir $target
|
|
|
|
|
2023-02-22 20:24:51 +01:00
|
|
|
for l in $(find $langDir -mindepth 1 -type d -prune -printf "%P\n")
|
2022-03-05 22:24:31 +01:00
|
|
|
do
|
2023-01-13 00:28:59 +01:00
|
|
|
generate_header lang/$l/header.link
|
|
|
|
echo "Create folder $l in $target..."
|
|
|
|
mkdir "$target/$l"
|
2023-02-22 20:24:51 +01:00
|
|
|
for i in $(find $langDir/$l -type f -name "*.html" -printf "%P\n")
|
2023-01-13 00:28:59 +01:00
|
|
|
do
|
|
|
|
echo "[$l] Generate $i..."
|
|
|
|
file="$target/$l/$i"
|
|
|
|
#cat $header > $file
|
|
|
|
cat $tmpHeader > $file
|
2023-01-13 19:06:54 +01:00
|
|
|
# on extrait le bloc contenant les link, on les enlève et rajoute
|
|
|
|
# <link />
|
2023-01-13 20:12:02 +01:00
|
|
|
# merci
|
|
|
|
# https://www.theunixschool.com/2012/12/sed-10-examples-to-print-lines-from-file.html
|
|
|
|
# https://stackoverflow.com/questions/68573654/copy-a-content-from-one-file-and-need-to-replace-in-another-file-using-sed
|
|
|
|
# https://unix.stackexchange.com/questions/26284/how-can-i-use-sed-to-replace-a-multi-line-string
|
2023-01-13 00:28:59 +01:00
|
|
|
# TODO pouvoir mettre plusieurs lignes link
|
2023-01-17 23:57:39 +01:00
|
|
|
newHeader=$(sed -n -e "/%link%/,/%endlink%/p" $langDir/$l/$i | \
|
|
|
|
sed -e "/%link%/d;/%endlink%/d" -e 's/\//\\\//g' -e "i<link " \
|
|
|
|
-e 'a\ \\\/>' | tr -d "\n")
|
2023-01-13 00:28:59 +01:00
|
|
|
|
2023-01-13 19:06:54 +01:00
|
|
|
# je suppose que s'il y a moins de 10 caractères, alors on ne prend pas
|
|
|
|
# en compte le nouveau link.
|
2023-01-13 00:28:59 +01:00
|
|
|
if [ $(echo $newHeader | wc -c) -lt 10 ]
|
|
|
|
then
|
|
|
|
sed -i -e "/%links%/d" $file
|
|
|
|
else
|
|
|
|
# pour bien aligner
|
|
|
|
newHeader="\ \ \ \ \ \ \ \ $newHeader"
|
2023-01-13 19:06:54 +01:00
|
|
|
sed -i -e "/%links%/a$newHeader" $file
|
2023-01-13 00:28:59 +01:00
|
|
|
fi
|
|
|
|
|
2023-02-23 00:56:24 +01:00
|
|
|
cat $langDir/$l/$i $footer >> $file
|
2023-01-13 00:28:59 +01:00
|
|
|
# nettoyage des balises précédemment utilisées
|
2023-01-12 01:23:59 +01:00
|
|
|
sed -i -e "/%links%/d" $file
|
2023-01-13 00:28:59 +01: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
|
2023-01-13 20:12:02 +01:00
|
|
|
sed -i -e "s/%gitfile%/lang\/$l\/$i/" $file
|
2023-01-13 00:28:59 +01:00
|
|
|
sed -i -e "s/%lang%/$l/" $file
|
|
|
|
done
|
2022-03-05 22:24:31 +01:00
|
|
|
done
|
2022-04-02 16:02:09 +02:00
|
|
|
|
2023-01-17 23:22:37 +01:00
|
|
|
echo "Header links translation..."
|
2023-02-23 00:56:24 +01:00
|
|
|
# cf. la page info 4.2 du manuel Bash (transforme un fichier en tableau)
|
2023-01-17 23:32:47 +01:00
|
|
|
mapfile translateHeader < $langDir/links.config
|
2023-01-17 23:22:37 +01:00
|
|
|
langs=${translateHeader[0]}
|
|
|
|
nbLang=$(echo "$langs" | grep -c "|")
|
|
|
|
for (( i=1; i<${#translateHeader[@]}; i++ ))
|
|
|
|
do
|
|
|
|
# pour chaque langue
|
|
|
|
for j in $(seq 0 $nbLang)
|
|
|
|
do
|
2023-01-17 23:57:39 +01:00
|
|
|
t=$(( $j + 1 ))
|
|
|
|
currentLang=$(echo ${translateHeader[0]} | cut -d\| -f $t | tr -d " ")
|
|
|
|
currentFile=$(echo ${translateHeader[$i]} | cut -d\| -f $t | tr -d " ")
|
2023-01-17 23:22:37 +01:00
|
|
|
|
|
|
|
# on doit modifier son lien de traduction
|
|
|
|
for k in $(seq 0 $nbLang)
|
|
|
|
do
|
|
|
|
[[ $k -eq $j ]] && continue
|
|
|
|
|
2023-01-17 23:57:39 +01:00
|
|
|
t=$(( $k + 1 ))
|
|
|
|
lang=$(echo ${translateHeader[0]} | cut -d\| -f $t | tr -d " ")
|
|
|
|
file=$(echo ${translateHeader[$i]} | cut -d\| -f $t | tr -d " ")
|
2023-01-17 23:22:37 +01:00
|
|
|
|
2023-01-17 23:57:39 +01:00
|
|
|
sed -i -e "s/href=\"\/$lang\/.*\.html/href=\"\/$lang\/$file.html/"\
|
|
|
|
$target/$currentLang/$currentFile.html
|
2023-01-17 23:22:37 +01:00
|
|
|
done
|
|
|
|
done
|
|
|
|
done
|
|
|
|
|
2023-04-02 18:41:46 +02:00
|
|
|
# pour chaque fichier, on vérifie s'il n'est pas dans le fichier links.config
|
|
|
|
# ou en double dans d'autres langues.
|
|
|
|
# TODO le faire de manière plus fine pour ne mettre que les langues traduites
|
|
|
|
echo "Remove useless lang header..."
|
|
|
|
for f in $(find $target -type f -name "*.html")
|
|
|
|
do
|
|
|
|
if [[ $(grep -c $(basename $f | cut -d. -f1) $langDir/links.config) -eq 0 ]] && \
|
|
|
|
[[ $(find $target -name $(basename $f) | grep -c \n) -eq 0 ]]
|
|
|
|
then
|
|
|
|
sed -i -e '/id="lang"/,/\/div/d' $f
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2023-01-13 17:57:31 +01:00
|
|
|
echo "Copy $annexes in $target..."
|
2022-04-02 16:02:09 +02:00
|
|
|
cp -t $target -r $annexes
|
2022-04-02 16:04:50 +02:00
|
|
|
|
2023-01-13 17:57:31 +01: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
|
|
|
|
}
|
2023-02-23 00:56:24 +01:00
|
|
|
}' $target/index.html)
|
2023-01-13 17:57:31 +01:00
|
|
|
do
|
|
|
|
sed -i -e "$c s/href=\"/href=\"$defaultLang\//" $target/index.html
|
|
|
|
done
|
|
|
|
|
2022-04-03 03:13:47 +02:00
|
|
|
echo "Link Links (lul)"
|
|
|
|
ln -s $pathLinks $target
|
|
|
|
|
2022-05-14 01:47:50 +02:00
|
|
|
if [ $org -eq 1 ]
|
|
|
|
then
|
|
|
|
echo "Generate org files..."
|
|
|
|
emacs -u $USER --script publish.el
|
|
|
|
fi
|