Auto-éditer un wikilivre/Annexer (Version internationalisée)/src/ecfpb.sh

Un livre de Wikilivres.

File for development of the function ecfpb ecfpb.bash, and test-ecfpb.bash to tests the function included in annex.lib

Fichier de mise au point de la fonction ecfpb ecfpb.bash, et test-ecfpb.bash pour éprouver la fonction en librairie annex.lib

ecfcb.bash[modifier | modifier le wikicode]

Development of the function

#!/bin/bash
#H VERSION 210409
#H File : ecfpb.bash function ecfpb 'extract contents from printable book'
#H This function is included in $Installdir/$Libdir/annex.lib
#H This function ecfpb product 'bookname.contents' in the project 'bookname'
#www function ecfcb () 'extract contents from compiled book'
#H
#H extract the documentation from this file :
#H cat test_ecfpc.bash | grep -e '#' > doc-test_ecfpc.bash
#H or cat test_ecfpb.bash | grep -e '#E' -e '#P' | sed -e "s/#E/ /g" -e "s/#P/ /g" |sed '1d' > docuEN-test_ecfpb
#H ou cat test_ecfpc.bash | grep -e '#F' -e '#P' | sed -e "s/#F/ /g" -e "s/#P/ /g" |sed '1d' > docuFR-test_ecfpc
#H
#E English Documentation
#E 

#F Doc en français
#F Le livre peut être annexé par article ou globalement.
#F Dans le cas d'annexes séparées d'un livre il sera nécessaire de concaténer
#F   les annexes manuellement.
#F Dans le cas de l'annexation du livre en entier il faut contrôler le
#F   résultat et, éventuellement, le corriger à l'aide d'un éditeur de texte.
#F

#P Doc programmeurs
#P L'objet de cette commande de tests est d'étudier le comportement que doit
#P   avoir la fonction ecfpc pour générer l'annexe d'un livre, avec un
#P   lien vers une page de contenu.
#P La fonction ecfpc de la librairie annex.lib analyse le paramètre entré
#P   par la ligne de commande à l'aide de la fonction findbook.
#P
#P ** Cas où le projet n'existe pas encore dans le répertoire de travail **
#P Ce paramètre doit être, l'url, l'adresse du livre ou de la page à annexer.
#P Exemples :
#P Version impimable :
#P  https://fr.wikibooks.org/wiki/Utilisateur:Goelette_Cardabela/Sandbox/LivreTest/Contenu
#P 
#P Dans ce cas la fonction findboox doit retrouver les articles du livre,
#P   trouver le nom du site, le nom du livre, le suffixe qui sera donné sous la
#P   forme : 'nom_du_livre.suffixe', afin d'être analysé par le programme list
#P   pour finalement générer la page'annexe' du livre.
#P
#P ** Cas où le projet existe **
#P Le paramètre doit être simplement le nom_dulivre. 
#P Le projet ~/Add_appendix/nomdulivre doit contenir le fichier
#P   nom_du_livre.suffixe qui contient la liste des pages à analyser.
#P La liste des pages peut être de la forme :
#P   * [[ nom_du_livre/article1 ]]
#P   * [[ nom_du_livre/article2|article2 ]] 
#P ou sous forme d'url :
#P   * https://fr.wikibooks.org/wiki/nom_du_livre/article1
#P   * [https://fr.wikibooks.org/wiki/nom_du_livre/article2 article 2]
#P

#P Print availables suffix in test mode
    if test -z $Installdir; then if test -d ~/Add_appendix/bin; then Installdir=~/Add_appendix;fi;fi
#T echo "Installdir = $Installdir"; exit 0;
    Workdir=~/Add_appendix
    Libdir=$Installdir/lib

#O Include the lib annex.lib which contains funtion findbook()
    source $Libdir/annex.lib
	
#O Print syntax if the first parameter is no entry in test mode
	if test -z $1; then echo "Syntax : ecfpb <book url> or ./ecfpb <book url>"; exit 1; fi

#O Test url with printable version
    #echo "Test for 'findbook https://fr.wikibooks.org/wiki/Utilisateur:Goelette_Cardabela/Sandbox/LivreTest/Contenu' "
    #findbook https://fr.wikibooks.org/wiki/Utilisateur:Goelette_Cardabela/Sandbox/LivreTest/Contenu
	echo "Test for $1"
	findbook $1
	echo ""
	echo "* Bookfullurl: "$Bookfullurl
	echo "* MW book url: "$MWbookurl
    echo "* Site name  : "$Site
    echo "* Last field : "$Lastfield
    echo "* Bookname   : "$Bookname
    echo "* Suffix     : "$Suffix
    echo "* Bookname.Suffix : "$Bookname.$Suffix	  

#function ecfpb ()
#{
#O Affect the variable $Projectdir
	Projectdir=$Workdir/$Bookname
#T Print the project directory echo "Project directory = $Projectdir"; exit 0

#O Test if the file is a printable version
    if [ -n $Site ] && [ "$Suffix" = "contents" ]; 
    then 
      echo "url is a printable version."; 	 
#O   Download script from the file $Lastfield
	  #wget https://fr.wikibooks.org/wiki/Utilisateur:Goelette_Cardabela/Sandbox/LivreTest/Contenu
	  #wget $1
#O 	 Find the contents of file named '$Lastfield'
	 #cat Contenu | grep /wiki/Utilisateur:Goelette_Cardabela/Sandbox/LivreTest/ | grep -v -i Contenu > $Lastfield.txt
	 cat $Lastfield | grep /$MWbookurl/ | grep -v -i $Lastfield > beforelastfield.txt
#T	 cat beforelastfield.txt and select 
	 cat beforelastfield.txt | awk -F "/" '{for (i=1;i<=NF;i++) print $i}' > fieldlist.txt
	 cat fieldlist.txt | sed  -e "s/\"/\n/g" | sed -e "s/ /\n/g" > fieldlist2.txt
	 wc -l fieldlist.txt | cut -d" " -f1 > N.txt; read N < N.txt; #T echo " N Fields = "$N   
     let  n=N; i=0 ; j=0; article=1; flag0=0; #flag1=1;
	 rm article.*.txt
	 while read line  
     do 
	   let i+=1;
	   if [ "$line" = "wiki" ]; then echo -n "[[$MWbookurl/" > article.$article.txt; let flag0=1; fi
	   if [ $flag0 = "1" ] && [ "$line" = "$Bookname" ]; then let j=i+1; fi
	   if [ $flag0 = "1" ] && [ "$i" = "$j" ]; then echo "$line]]" >> article.$article.txt; let article+=1; let flag0=0; fi
     done < fieldlist2.txt
	 fi
#O Concatenation of files obtained, to file bookname.suffix
    let i=1; rm $Bookname.$Suffix
	echo "[[$MWbookurl]]" > $Bookname.$Suffix
	while (( i < 10 ))
	do
	  #echo "article.$i.txt ="; cat article.$i.txt
      if [ -r article.$i.txt ]; then cat article.$i.txt >> $Bookname.$Suffix; let i+=1 ; else break; fi
	done
	
#O If project '$Bookname' exist
    if test -d $Projectdir 
    then
	  echo "Project directory = $Projectdir"
      if [ -r "$Projectdir/$Bookname.$Suffix" ] 
      then 
        echo -n -e "$(gettext ' \033[31mUpdate $Bookname.$Suffix ? y/n :\033[0m ')"; read -s -n1 -p "inkey" Inkey; echo;
        if [ "$Inkey" = "y" ] 
        then  chmod 755 "$Projectdir/$Bookname.$Suffix"; cp "$Bookname.$Suffix" $Projectdir/.; chmod 555 "$Projectdir/$Bookname.$Suffix";
        else "$(gettext ' file is not updated ! ... Continue ... ')"
        fi
      else cp "$Bookname.$Suffix" $Projectdir/.
      fi
    fi
#}
#T Affichage de bookname.suffix en mode test
    if [ $0="./ecfpb.bash" ] || [ $0="$Testsdir/findbook/ecfpb.bash" ]; then echo "$Bookname.$Suffix :"; cat $Bookname.$Suffix; fi

test-ecfpb.bash with function in annex.lib[modifier | modifier le wikicode]

#!/bin/bash
#H File : ecfpb.bash function ecfpb 'extract contents from printable book'
#H Syntax for tests ecfpb <book url>, example in tests directory :
#H ./ecfpb.bash https://fr.wikibooks.org/wiki/Utilisateur:Goelette_Cardabela/Sandbox/LivreTest/Contenu

#P Print availables suffix in test mode
#    if test -z $Installdir; then if test -d ~/Add_appendix/bin; then Installdir=~/Add_appendix;fi;fi
#T echo "Installdir = $Installdir"; exit 0;
#    Workdir=~/Add_appendix
#    Libdir=$Installdir/lib
    
Version=210414

clear
#P print prog, version and Why
echo "ecfpb.sh $Version for testing ecfpb 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
     echo -n "/" > workdir.txt
     cat pwd.txt | head -n 4 | cut -d '/' -f 2-4 >> workdir.txt
     read Workdir < workdir.txt
    # rm pwd.txt homedir.txt workdir.txt
     
#P print current, home, working directories
    echo "Current directory = $Currentdir"
    echo ""
    echo "Home directory = $Homedir"
    echo ""
    echo "Working directory = $Workdir"
#T Test break exit 0

#P Find lib directory
    if test -a /usr/local/lib/annex.lib; then Libdir=/usr/local/lib; Bindir=/usr/local/bin; Sharedir=/usr/local/share
    elif test -a $Workdir/lib/annex.lib; then Libdir=$Workdir/lib; Bindir=Workdir/bin; Sharedir=$Workdir/share
    elif test -a $Workdir/tests/1-annex.lib/annex.lib; then Libdir=$Workdir/tests/1-annex.lib/
    else echo "Lib directory is not found, exit -1"; exit -1
    fi

# print directories and if OK
   echo -n "Working directory = $Workdir"
   if [ -d $Workdir ]; 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 Testbreak exit 0
    clear   
    
#O Include the lib annex.lib which contains funtion ecfpb()
    source $Libdir/annex.lib
	
#O Print syntax if the first parameter is no entry in test mode
	#if test -z $1; then echo "Syntax : ecfpb <book url> or ./ecfpb <book url>"; exit 1; fi

#O Test url with printable version
    #echo "Test for 'ecfpb https://fr.wikibooks.org/wiki/Utilisateur:Goelette_Cardabela/Sandbox/LivreTest/Contenu' "
    #ecfpb https://fr.wikibooks.org/wiki/Utilisateur:Goelette_Cardabela/Sandbox/LivreTest/Contenu
	echo "Test for $1"
	findbook $1
	echo ""
	echo "* Bookfullurl: "$Bookfullurl
	echo "* MW book url: "$MWbookurl
    echo "* Site name  : "$Site
    echo "* Last field : "$Lastfield
    echo "* Bookname   : "$Bookname
    echo "* Suffix     : "$Suffix
    echo "* Bookname.Suffix : "$Bookname.$Suffix
    
#O Test the function ecfpb 
    ecfpb $1

#T Print bookname.suffix in test mode
    if [ $0="./ecfpb.sh" ] || [ $0="$Testsdir/1-annex.lib/12-ecfpb/ecfpb.sh" ]; then echo "$Bookname.$Suffix :"; cat $Bookname.$Suffix; fi