« Auto-éditer un wikilivre/Annexer (Version internationalisée)/src/header.inc.sh » : différence entre les versions

Contenu supprimé Contenu ajouté
m →‎tests/header.inc/test_header.inc.sh : suppression d'une coquille
→‎src/header.inc.sh : Nouvelle version
Ligne 1 :
{{Remarqueampoule|Ce fichier d'entête doit être inclus dans tous les modules par la commande 'source' : <br />'''''source $Bindir/header.inc'''''<br />Dans ses dernières versions : <br />- ''header.inc.sh'' est un fichier source et ''header.inc'' est un bichier exécutable<br />- Les commentaires d'oganigramme #O sont supprimés. Les commentaires #P pour les programmeurs sont en anglais, les commentaires de structure #S ont été réduits au minimum.<br />- Les messages en anglais sont introduits pour l'internalisation avec gettext.<br />{{Orange|En deuxième partie: exemple de fichier de commande d'un test '''''tests/header.inc/test_header.inc.sh'''''}}}}
== src/header.inc.sh ==
# <syntaxhighlight lang="bash">
#!/bin/bash
#H File : bin/header.inc or src/header.inc.sh
Ligne 13 :
#H Updated : 2020-06-15 by GC to update for translations and tests
#H Update on fr:Wikibooks : 2020-06-17
#H Last update : 2021-0405-0119
#H
#w NEW 17/05/2021: The books are now in the Book directory
#
#F Documentation en français
#F Le langage shell bash a été choisi plutôt d'autres shells pour son
#F universalité, c'est le langage de base pour apprendre. On trouve
#F la documentation dans toutes les langues et c'est aussi le plus
#F rapide à l'exécution, ce n'est pas un langage interprété, c'est du
#F code quasi binaire en dehors du temps consacré au contrôle de la
#F syntaxe à chaque exécution.
#F Le fichier de commandes header.sh doit être inclus dans tous les
#F modules de test du programme principal. Il initialise le
#F répertoire des commandes, le répertoire de travail du projet;
#F le répertoire du projet est le nom du livre, de
#F l'article ou de la page. Si ces répertoires n'existent pas ils
#F seront créés.
#F
#F Ce projet éducatif et de tests est destiné à être utilisé en liens
#F avec wikilivres en langue anglaise et caractères UTF8.
#F Les langues supportées pour les messages sont obtenues par édition
#F des fichiers .pot avec poedit pour produire les fichiers .po
#F d’interprétation dans des langues différentes. Les fichiers .pot
#F et .po sont enregistrés dans le répertoire ‘resource’ avant d’être
#F copiés dans le système unix ou linux.
#F Les manuels sont écrits en anglais et français et peuvent être
#F traduits dans d'autres langues.
#F
#w Les variables Site et SitePrefix se rapportent à "fr.wikibooks.org"
#w des anciennes versions sont supprimés depuis les versions 210501
#w
#F Le programme est prévu pour fonctionner à l'initiative du
#F contributeur qui doit copier la page "Contenus" ou la page de
#F compilation dans le répertoire des livres :
#F "~/Add_appendix/books/<nom du livre>"
#F Depuis les versions de mai 2021 ce travail peut être facilité avec
#F le programme pre-annex.sh
#F Les modules de programmes sont indépendants pour les tests,
#F cependant ils doivent être exécutés dans l'ordre
#F ./header.sh <livre>, ./lister <livre>, ./télécharger <livre>
#F ./ajouter_sclt <livre>, ./ajouter_sclic, ./ajouter_sclip <livre>
#F La liste des pages à analyser est indispensable dans tous les
#F modules en tests. Ils auront été copiés par vous-même ou par
#F le programme pre-annex et seront listés par le module lister.
#F L'absence de la liste des pages à analyser est signalée à chaque
#F lancement des modules du programme principal "Annexer" par
#F l'exécution de header.inc inclus dans chaque module en test.
#F
 
#E English documentation
#E
 
#O . gettext for message internationalization
. gettext.sh
 
VERSION=210519
Version=210421
#O Clean messages on terminal
Bookname=
clear
Suffix=
#O Create the function tests_header_syntax
Site=
function tests_header_syntax
 
{
clear
echo
 
echo "$(gettext ' Syntax of the header.inc command in test mode if the book is already created : '$0' <bookname> --t ')"
#P Initializes Bindir
echo "$(gettext ' Example : '$O' TestBook --t ')"
if [ "$0" = "./header.inc" ];
echo "$(gettext ' Second syntax command in test mode if the book is not created : '$0' - --t ')"
echo "$(gettext ' Example : '$0' --v or '$0' ? --t ')"
echo
exit 0
}
#O If first parmeter is empty, print the error and suggest the help syntax
if test -z $1; then echo "$(gettext ' Error: input parameter is empty, see the command : header.inc ? ')"; exit -1; fi
#O If help is requested
if [ "$1" = '?' ]; then
if [ "$2" = "--t" ]; then tests_header_syntax; else echo "Syntax : header.inc bookame"; exit 0; fi
fi
#O Display header.inc version on demand with option -v for first parameter
if [ "$1" = "--v" ]; then echo " header.inc version $VERSION"; exit 0; echo; fi
#O If the test mode is validated, initialize the Testmode variable to true
if [ "$2" = "--t" ]; then echo -n "Tests mode $2; "; Testsmode="true"; echo "Testmode = $Testsmode"; fi
#O Directories initialization, install, working, binaries, tests directories
#O If the file 'installdir.var exist in vars dir, include installdir.var
if [ -e ~/Add_appendix/vars/installdir.var ]
then
source ~/Add_appendix/vars/installdir.var
pwd > pwd.txt
#O Else, print the problem and offer to reinstall Add_appendix directories
else
echo $0 | awk -F"/" '{ for (i=1;i<NF;i++) print $i}' | tr '\n' '/' | sed "s/bin\//bin/g" > pwd.txt
fi
read Bindir < pwd.txt
rm pwd.txt
 
#P Tests suite in header.inc
echo "$(gettext ' Directories in header.inc test environment ')"
#P Test and print $Bindir if exist or if it's not found
if test -d $Bindir
then echo "$(gettext ' The commands directory : ')"$Bindir"$(gettext ' is OK ! ')"
else echo "$(gettext ' The commands directory ')"$Bindir"$(gettext ' is not found ! ')"
fi
 
echo $Bindir | awk -F"/" '{ for (i=1;i<=NF;i++) print $i}' | tr '\n' '/' | sed s/"\/bin\/"//g > tmp.txt
read Installdir < tmp.txt
rm tmp.txt
 
if test -d $Installdir
then echo "$(gettext ' The Installdir : ')"$Installdir"$(gettext ' is OK ! ')"
else echo "$(gettext ' The Installdir : ')"$Installdir"$(gettext ' is not found ! ')"
fi
 
Libdir="$Installdir/lib"
if test -d "$Libdir"
then echo "$(gettext ' The lib directory : ')"$Libdir"$(gettext ' is OK ! ')"
else echo "$(gettext ' The lib directory ')"$Libdir"$(gettext ' is not OK ! ')"
fi
 
Sharedir="$Installdir/share"
if test -d "$Sharedir"
then echo "$(gettext ' The Share directory ')"$Sharedir"$(gettext ' is OK ! ')"
else
echo -n "Current directory in $0 "; pwd
mkdir $Sharedir
echo "~/Add_appendix/vars/installdir.var not found in $0 command"
echo "$(gettext ' The Share directory ')"$Sharedir"$(gettext ' is created ! ')";
echo -e "\033[47m\033[1;30m Re-install Add_Appendix ? \033[0m"
read -s -n1 -p "inkey y/n: " Inkey; echo "$Inkey"
if [ "$Inkey" = 'y' ]; then
~/Add_appendix/sbin/users_install_annex.sh Add_appendix
#xdg-open https://fr.wikibooks.org/wiki/Auto-éditer_un_wikilivre/Annexer_\(Version_internationalisée\)/users_install_annex.sh
exit 0
else exit -1
fi
fi
#O Initialize all global directories
 
#P In Install directories :
echo $Bindir > $Sharedir/bindir.txt
echo Bindir=$Installdir > $Sharedir/installdir.txtbin
echo $Libdir > =$SharedirInstalldir/libdir.txtlib
Mandir=$Installdir/man
 
Sharedir=$Installdir/share
 
Sbindir=$Installdir/sbin
Workdir=~/Add_appendix
#P In working directory :
 
if test -d Booksdir=$Workdir/books
Datasdir=$Workdir/datas
then
Resources=$Workdir/resources
echo "$(gettext ' The working directory : ')"$Workdir"$(gettext ' is OK ! ')"
echo Testsdir=$Workdir > $Sharedir/workdir.txttests
Varsdir=$Workdir/vars
#O If testmode is true (--t), print global directories variables
if [ "$Testsmode" = "true" ]; then
echo "* Install directory = $Installdir"
echo -e "\033[47m\033[1;30m In install directories \033[0m"
echo " Executables directory = $Bindir"
echo "- Library directory = $Libdir"
echo "- Manuals directory = $Mandir"
echo "- Share directory = $Sharedir"
echo "- Admin directory = $Sbindir"
echo
echo "* Working directory = $Workdir"
echo -e "\033[47m\033[1;30m In Working directory \033[0m"
echo "- Books directory = $Booksdir"
echo "- Datas directoy = $Datasdir"
echo "- Resources directory = $Resources"
echo "- Tests directory = $Testsdir"
echo "- Variables directory = $Varsdir"
fi
#O Check for the presence of the annex.lib file
if test -e $Libdir/annex.lib
then if [ "$Testsdmode" = "true" ]; then echo; echo " $Libdir/annex.lib is found"; fi
else
echo "To continue, copy annex.lib to $Libdir"
mkdir $Workdir
exit -1
echo "$(gettext ' The working directory ')"$Workdir"$(gettext ' is created ! ')";
fi
#O Initialize $Conversions variable
echo $Sharedir > $Workdir/sharedir.txt
Conversions="$Datasdir/conversions.dat"
 
#O If the command is local for tests, clean the old file and create a new conversions file
#P Initialyze $Conversions file
if [ "$Testsdmode" = "true" ]
Conversions="$Workdir/datas/conversions.dat"
#P If the command is local for tests, clean the old file and create a new conversions file
if [ $0 = "./header.inc" ]
then
echo; echo "$(gettext ' When the command tests is ./header.inc$Testsdir, then conversion file is recreated to ')"$Conversions
rm $Workdir/conversions.dat $Conversions
fi
#O Create or recreate the conversions.dat file in datas directory
 
if test -e "$Conversions"
then echo "$(gettext ' The conversion file is present ')"
Ligne 122 ⟶ 185 :
echo "s/%C2%B7/-/g;s/%C2%AB/«/g;s/%C2%BB/»/g" >> $Conversions
}
echo "$(gettext ' The file conversions.dat is created or updated in file : ')" $Conversions
fi
#O If the first parameter is a project in the books directory
 
if test -d $Booksdir/$1; then
#P Include the header syntax function for individual test
#O Initialize Books variables
function header_syntax
if test -e "$Varsdir/$1.var"; then source $Varsdir/$1.var;
{
else
echo "$(gettext ' Syntax of the header.inc command in test mode: ./header.inc <book> ')"
echo -e "$(gettext ' Example\033[31m'$1' :variables "./header.incnot LivreTest"found orin "./header'$Varsdir'.inc ?"\033[0m ')";
echo -n "Site=" > $Varsdir/$1.var; ls $Booksdir/$1 | grep wikibooks >> $Varsdir/$1.var
echo ""
echo "Bookname=$1" >> $Varsdir/$1.var
exit 0
#O Get suffix in Project directory and intialize Suffix
}
echo "$(gettext ' Get the suffix in project directory and put it in vars/$1.var : ')"
 
if test -e "$Booksdir/$1/$1.contents"; then cat $Booksdir/$1/$1.contents; echo "Suffix=contents" >> $Varsdir/$1.var
#P For a first parameter empty print syntax else print Bookname
elif test -e "$Booksdir/$1/$1.summary"; then cat $Booksdir/$1/$1.summary; echo "Suffix=summary" >> $Varsdir/$1.var
if test -z "$1" #S1
elif test -e "$Booksdir/$1/$1.compiled"; then cat $Booksdir/$1/$1.compiled; echo "Suffix=compiled" >> $Varsdir/$1.var
then
else echo "$(gettext ' List of links to articles to be analyzed : not found ')"
echo "-----"
fi
echo "$(gettext ' Error: no input parameter, see the command : annex ? ')"
if [ $0 = "./header.inc" ]; #S2
then header_syntax;
else echo "$(gettext ' Syntax : $0 [ <book-name> | ? ] [ -v ] [ -pb | -pc ] ')";
exit 1
fi #S-2
else
if [ "$1" != "?" ] #S3
then
echo $1 | awk -F "/" '{for (i=0;i<=NF;i++) print $i}' > bookname.txt
cat bookname.txt | grep wikibooks > sitename.txt
read Site < sitename.txt
if test $Site #S4
#w ATTENTION: These are strings, perhaps, without content.
then
if test -e "$Libdir/annex.lib"; #S5
then findbook $1;
echo $Site > "$Workdir/site.txt"; #T echo "Site name :"$Site
mkdir $Bookname; echo "Bookname : "$Bookname
#T Test if variable $Suffix is OK echo "Suffix : "$Suffix
echo "Bookname.Suffix" > $Workdir/$Bookname; #T echo "Bookname.Suffix = "$Bookname.$Suffix
else echo "$Libdir/annex not fount";
fi #S-5
fi #S-4
fi #S-3
fi #S-1
#T Breakpoint exit 0
 
#P If first parameter is the char '?'
if test $1;
then
if [ "$1" = "?" ]
then
clear
header_syntax
fi
fi
#O Print articles to be analyzed and if list-file exist
 
echo
#P Initialize the cariable Project directory
echo -n "$(gettext ' Test if the file $Project/$1.list, exists : ')"; echo "$Booksdir/$1/$1.list"
Project=$Workdir/$1
if test -e "$Booksdir/$1/$1.list"
echo "$(gettext ' Project directory : ')"$Project
echo "----------"
 
#P Test if the project diretory exist
if test -d $Project
then
echo "$(gettext ' The project directory : ')"$Project"$(gettext ' is found ! ')"
echo $Project > $Sharedir/project.txt
echo "$(gettext ' Default Site name is fr.wikibooks.org, when your book is in another site please modify site.txt ')" > $Workdir/README
fi
 
#P Test the existing file site.txt exist in working directory
if test -a $Workdir/site.txt
then
read Site < "$Workdir/site.txt"
SitePrefix="https:\/\/$Site\/wiki\/"
else
Site="fr.wikibooks.org"
SitePrefix="https:\/\/fr.wikibooks.org\/wiki\/"
fi
echo "----------"
 
#P Print Project name, site url, SitePrefix variable
echo "$(gettext ' Project name: ')"$1
echo "$(gettext ' Site: ')"$Site
echo "$(gettext ' SitePrefix for the shell command sed. ')"$SitePrefix
echo "----------"
 
#P Print the contents variable if exist
echo "$(gettext ' File contents presence test in header.inc')"
if test -e $Project/$1.contenu; then echo "$(gettext ' Found : ') $Project/$1.contenu";
elif test -e $Project/$1.Contenus; then echo "$(gettext ' Found : ') $Project/$1.Contenus";
elif test -e $Project/$1.compilé; then echo "$(gettext ' Found : ') $Project/$1.compilé";
elif test -e $Project/$1.Contents; then echo "$(gettext ' Found : ') $Project/$1.Contents";
elif test -e $Project/$1.compile; then echo "$(gettext ' Found : ') $Project/$1.compile";
elif test -e $Project/$1.compiled; then echo "$(gettext ' Found : ') $Project/$1.compiled";
elif test -e $Project/$1.Sommaire; then echo "$(gettext ' Found : ') $Project/$1.Sommaire";
elif test -e $Project/$1.Summary; then echo "$(gettext ' Found : ') $Project/$1.Summary";
elif test -e $Project/$1.Sommaire_du_livre; then echo "$(gettext ' Found : ') $Project/$1.Sommaire_du_livre";
else echo -e "$(gettext ' \033[31mContents file not found : ')$Project/$1.Contents\033[0m"; echo -e "\033[0m"; exit 2;
fi
echo "----------"
#P Print articles to be analyzed and if list-file exist
echo "$(gettext ' List of links to articles to be analyzed bye header.inc: ')"
echo "$(gettext ' Test if the file $Project/$1.list, exists: ')$Project/$1.list"
if test -e $Project/$1.list
then
echo "$(gettext ' Yes, the file exists. ')"
else
echo -e "$(gettext ' \033[31mNo, ')"$ProjectBooksdir/$1/$1.list"$(gettext ' is not yet created.\033[0m ')"
fi
#O Test if the .prj file exist and print the result
echo "----------"
echo -n "$(gettext ' Test if the file $Project/$1.prj, exists: ')"; echo "$Booksdir/$1/$1.prj"
 
if test -e "$Booksdir/$1/$1.prj"
#P Test if the .prj file exist and print the result
then echo "$(gettext ' Test ifYes, the file $Project/$1exists.prj, exists: ')$Project/$1.prj"
else echo -e "$(gettext ' \033[31mNo, '$Booksdir/$1/$1.prj' is not yet created.\033[0m ')"
if test -e $Project/$1.prj;
then echo "$(gettext ' Yes, the file exists. ')";
else echo -e "$(gettext ' \033[31mNo, ')"$Project/$1.prj"$(gettext ' is not yet created.\033[0m ')";
fi
#O If the first parameter is not empty, print the file list in the project diretory
echo "----------"
if test -n $1
 
then echo; echo -e -n "$(gettext ' \033[33mContents in the project directory : ')"; echo -e "$Booksdir/$1 \033[0m"
#P If the first parameter is not empty, print the file list in the project diretory
if [ ls -nal $Booksdir/$1 ];
then echo "$(gettext ' Contents in the project directory: ')$Project";
ls -al $Project
fi
#O cat Book variables
echo "----------"
echo; echo -n -e "$(gettext ' \033[33mVariables in the project directory : ')"; echo -e "$Booksdir/$1 \033[0m"
source $Varsdir/$1.var
cat $Varsdir/$1.var
#O list the content of local book
echo; echo -n -e "$(gettext ' \033[33mLink to the local html book : ')"; echo -e "$Booksdir/$1 \033[0m"
ls -1R $Booksdir/$1/$Site | tail -n 2 | sed "s/:/\//g" | tr -d "\n"; echo; echo
# End header.inc.sh
</syntaxhighlight>