Auto-éditer un wikilivre/Annexer (Version internationalisée)/src/findbook.sh
Aller à la navigation
Aller à la recherche
Cette fonction est à mettre à jour dans les fichier ~/Annexer/lib/annex.lib et ~/Add_appendix/lib/annex.lib
Fonction findbook[modifier | modifier le wikicode]
#!bin/bash
#H File : findbook function findbook () in file annex.lib
#H Version : 210310
#H Usage : source $Libdir/annex.lib in master program
#H Syntax : findbook <url-book-name> for tests
#H Example : findbook https://fr.wikibooks.org/wiki/Utilisateur:Goelette_Cardabela/Sandbox/LivreTest/Contenu
#H Creation: 2020-06-16 by fr wikibookuser Goelette Cardabela
#H Updated : syntax : DATE by User
#H Update on fr:Wikibooks : 2020-06-16 by GC
#H Update on fr:Wikibooks : 2021-04-08 by GC
#H Updated : 2021-04-08 by user GC
VERSION=210408
#P . gettext for translation
. gettext.sh
function findbook ()
{
#P Print availables suffix in test mode
if [ -z $1 ]; then
echo " -- Suffix availables in function findbook() version $VERSION : "
echo " <book-name>/Contenus or Contenu or Contents, "
echo " <book-name>/Sommaire or Summary, "
echo " Compilations/<book-name> or Collections/<book-name>. "
fi
#P Find bookname and sitename
echo $1 | awk -F "/" '{for (i=1;i<=NF;i++) print $i}' > bookname.txt
cat bookname.txt | grep wikibooks > sitename.txt
read Site < sitename.txt
wc -l bookname.txt | cut -d" " -f1 > N.txt; read N < N.txt; #T echo " N Fields = "$N
let n=N;
#P Find media wiki url for prinable book with last fild 'Contenus', 'Contents',
#P 'Sommaire', Summary
let local m=N-1;
echo $1 | cut -d '/' -f5-$m > mwbookurl.txt
read "MWbookurl" < mwbookurl.txt
#P Select last field from first parameter
#let n-=1;
echo $1 | cut -d '/' -f$n > lastfield.txt
read Lastfield < lastfield.txt; #T echo "Lastfield = "$Lastfield
#P Set the suffix according to the last field
if [ "$Lastfield" = "Contenus" ] || [ "$Lastfield" = "Contenu" ] || [ "$Lastfield" = "Contents" ];
then Suffix=contents;
fi
if [ "$Lastfield" = "Sommaire" ] || [ "$Lastfield" = "Sommaire_du_livre" ] || [ "$Lastfield" = "Summary" ];
then Suffix=summary;
else Bookname=$Lastfield;
fi
#P Select bookurl from first parameter
if [ $Suffix = "contents" ] || $Suffix = "summary" ]
then
let local m=N-1;
echo $1 | cut -d '/' -f1-$m > bookurl.txt
read Bookfullurl < bookurl.txt
fi
#P Set the suffix according to the penultimate field
let n-=1; echo $1 | cut -d '/' -f$n > penultimatefield.txt
read Penultimatefield < penultimatefield.txt; #T echo "Penultimate field = "$Penultimatefield
if [ "$Penultimatefield" = "Compilations" ] || [ "$Penultimatefield" = "Collections" ];
then Suffix=compiled;
else Bookname=$Penultimatefield;
fi;
rm bookname.txt sitename.txt lastfield.txt penultimatefield.txt N.txt
}
Programme des tests de la fonction findbook[modifier | modifier le wikicode]
Ce programme de tests findbook.sh est à mettre à jour dans le répertoire ~/Annexer/unit-test/.
#!/bin/bash
#H File : findbook.sh executable for testing the function findbook ()
#H Syntax : ./findbook.sh <url-book-name> (on the terminal for testing)
#H Example : ./findbook.sh https://fr.wikibooks.org/wiki/Utilisateur:Goelette_Cardabela/Sandbox/LivreTest/Contenus
#H ./findbook.sh
Version=210320
clear
#P print prg, version and Wky
echo "findbook.sh $Version for testing findbook in annex.lib"
#P initialization of variables
Site=""
Bookname=""
Suffix=""
Return_value="non available"
#P find the current directory
pwd > pwd.txt
read Currentdir < pwd.txt
#P find the user home directory
echo -n "/" > homedir.txt
cat pwd.txt | head -n 3 | cut -d '/' -f 2-3 >> homedir.txt
read Homedir < homedir.txt
rm pwd.txt homedir.txt
#P print current and home directories
echo "Current directory = $Currentdir"
echo ""
echo "Home directory = $Homedir"
echo ""
#T exit
if [ "$Currentdir" = "$Homedir/Annexer" ] || [ "$Currentdir" = "$Homedir/Add_appendix" ];
then
Workingdir=$Currentdir
Libdir=$Workingdir/lib
Bindir=$Workingdir/bin
Sharedir=$Workingdir/share
else
echo "The standard working directory 'Annexer' or 'Add_appendix' is not found."
exit
fi
# print directories and if OK
echo -n "Working directory = $Workingdir"
if [ -d $Workingdir ]; then echo " found"; else echo "notfound";fi
echo -n "Lib directory = $Libdir"
if [ -d $Libdir ]; then echo " found"; else echo "notfound";fi
echo -n "bin directory = $Bindir"
if [ -d $Bindir ]; then echo " found"; else echo "notfound";fi
echo -n "share directory = $Sharedir"
if [ -d $Sharedir ]; then echo " found"; else echo "notfound";fi
echo "----"
#T exit
clear
#P include the lib annex.lib which contains funtion findbook()
source $Libdir/annex.lib
#P beginnig test
#P first test whith a existing parameter else whith urls
if [ $1 ]; then
{
echo "Test for input "$1;
findbook $1;
echo "* Site name :"$Site;
echo "* Bookname : "$Bookname;
echo "* Suffix : "$Suffix;
echo "* Bookname.Suffix = "$Bookname.$Suffix;
echo "----";
exit 0;
}
else
{
echo "Test for 'findbook https://fr.wikibooks.org/wiki/Utilisateur:Goelette_Cardabela/Sandbox/LivreTest/Contenus' "
findbook https://fr.wikibooks.org/wiki/Utilisateur:Goelette_Cardabela/Sandbox/LivreTest/Contenus
echo "* Site name :"$Site
echo "* Bookname : "$Bookname
echo "* Suffix : "$Suffix
echo "* Bookname.Suffix = "$Bookname.$Suffix
echo "----"
echo "Test for 'https://fr.wikibooks.org/wiki/Hélices_de_navires_à_déplacement/Sommaire' "
findbook https://fr.wikibooks.org/wiki/Hélices_de_navires_à_déplacement/Sommaire
echo "* Site name :"$Site
echo "* Bookname : "$Bookname
echo "* Suffix : "$Suffix
echo "* Bookname.Suffix = "$Bookname.$Suffix
echo "----"
echo "Test for 'findbook https://fr.wikibooks.org/wiki/Utilisateur:Goelette_Cardabela/Compilations/LivreTest' "
findbook https://en.wikibooks.org/wiki/Utilisateur:Goelette_Cardabela/Compilations/LivreTest
echo "* Site name :"$Site
echo "* Bookname : "$Bookname
echo "* Suffix : "$Suffix
echo "* Bookname.Suffix = "$Bookname.$Suffix
echo "----"
echo "Test for 'findbook LivreTest/Contenu' "
findbook LivreTest/Contenu
echo "* Site name :"$Site
echo "* Bookname : "$Bookname
echo "* Suffix : "$Suffix
echo "* Bookname.Suffix = "$Bookname.$Suffix
}
fi
# End test function findbook()