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

Un livre de Wikilivres.
Aller à la navigation Aller à la recherche
  1. Cette fonction est à mettre à jour dans :
      Auto-éditer_un_wikilivre/Annexer_(Version_internationalisée)/src/annex.lib
    et/ou
    ~/Annexer/lib/findbook; annexer.lib
    et/ou
    ~/Add_appendix/lib/annex.lib
    et/ou
    /usr/local/lib/annex.lib
  2. Le programme de test est en deuxième section Programme des tests de la fonction findbook

Fonction findbook[modifier | modifier le wikicode]

#!bin/bash
#H File    : findbook.sh, source for test the findbook function to insert in
#H           annex.lib file. Copy this content in file $Workdir/lib/src/findbook.sh
#P Version : 210310 initial version
#P Usage   : The source findbook.sh will be processed and automatically inserted into $ Workdir/lib/annex.lib when the install_annex.sh program is executed.
#P Syntax  : 'findbook <url-book-name>' or 'findbook --v' to print version
#P Example : findbook https://fr.wikibooks.org/wiki/Utilisateur:Goelette_Cardabela/Sandbox/LivreTest/Contenu
#P Creation: 2020-06-16 by wikibook user Goelette Cardabela
#P Updated on                  : DATE by User
#P Updated on fr:Wikibooks     : 2020-06-16 by GC
#H Last Update on fr:Wikibooks : 2021-05-14 by GC
#H Tested  : by GC 2021-05-14
#H Updated : 2021-05-14 by user GC For layout
#H
#D function findbook documentation
#D Function findbook find site name, bookname, category (Collection, Printable)
#D   and proposes a file name for the project directory, containing the list of
#D   links to the articles in the book
#D
#w Attention, the character ';' at the end of the line is equal to '\' before NL
#w   if the following line is a comment, it will concatenate it at the end of
#w   the previous line and the command with grep -v deletes the whole line

VERSION=210514

#P . gettext for translation
. gettext.sh
                 
function findbook ()
{
#P To test version of function findbook with option --v
   if [ "$1" = "--v" ]; then echo "findbook version : $VERSION";return 0;fi

#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
    unset Suffix
    if [ "$Lastfield" = "Contenus" ] || [ "$Lastfield" = "Contenu" ] || [ "$Lastfield" = "Contents" ] || [ "$Lastfield" = "Content" ] 
    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" ] \
      || [ "$Penultimatefield" = "Wikilivres:Compilations" ] \
      || [ "$Penultimatefield" = "Wikibooks:Collections" ]
    then 
      Suffix=compiled 
    else Bookname=$Penultimatefield 
    fi

#P Special treatment for compilations
    if [ "$Suffix" = "compiled" ]
    then 
      MWcompilurl=$MWbookurl/$Bookname
      Compilfullurl=$1     
    fi
   
    rm bookname.txt sitename.txt lastfield.txt penultimatefield.txt N.txt
}


Programme des tests de la fonction findbook[modifier | modifier le wikicode]

  1. Ce programme de tests test_findbook.bash est à mettre à jour dans le répertoire ~/Add_appendix/tests/1-annex.lib/findboox.sh/.
    Add this codes to ~/Add_appendix/tests/1-annex.lib/findboox.sh/test_findbook.bash
  2. Copier également le code findbook dans ce même répertoire ~/Add_appendix/tests/1-annex.lib/findboox.sh/.
    Also add the findbook code in this same directory ~/Add_appendix/tests/1-annex.lib/findboox.sh/findbook
#!/bin/bash
#H File    : test_findbook.bash executable for testing the function findbook ()
#H Syntax  : ./test_findbook.bash <url-book-name>  (on the terminal for testing)
#H Example : ./test_findbook.bash https://fr.wikibooks.org/wiki/Utilisateur:Goelette_Cardabela/Sandbox/LivreTest/Contenus
#H         : ./test_findbook.bash [ -v | global ] ( global 'source annex.lib', else 'source ./findbook' )

VERSION=210514

clear
#P print prog, version and Why
    if [ "$1" = "--v" ]; 
    then 
      echo "Command 'test_findbook.bash' is $VERSION for testing findbook function";
      echo " Options : [ --v | global | <url-book> ]"
      exit 0;
    fi
    
#P Control if the local finbook.sh file is clean
    echo -e "\033[1;33m Control findbook.sh in the local test directory \033[0m" > text-control.txt
    echo -e "\033[1;33m Please check that the comment line does not contain an command \033[0m" >> text-control.txt
    grep -n -e "#T\|#O\|#P" findbook.sh >> text-control.txt
    more text-control.txt
#T    echo -e "\033[31m ROUGE \033[0m"
#T    echo -e "\033[1;33m Please check that the comment line does not contain an command \033[0m"
    echo; echo "Continue ? y/n"
    read -s -e -n 1 -t 60 -p "y/n ? : " Inkey;echo
#T    echo "Inkey = $Inkey"
    if [ "$Inkey" != 'y' ]; then echo "Inkey is not yes, or time out after 60 sec, exit from $0"; exit 0;fi

#P Initialization of environment variables for global execution
    if [ "$1" = "global" ] 
    then
    {
      echo "Global test whis annex.lib"; echo
      
#P   Include install variables
      source $HOME/Add_appendix/datas/installdir.var
      
#P   Print install variables
      echo "Working environnement variables :"
      echo "---------------------------------"
      echo "  Install directory : Installdir=$Installdir"
      echo "  Working directory : Workdir=$Workdir"
      echo "  Executables directory : Bindir=$Bindir"
      echo "  Tests directory : Testsdir=$Testsdir"
      Libdir=$Installdir/lib; Sharedir=$Installdir/share
      echo "  Library directory : Libdir=$Libdir"
      echo "  Share directory: Sharedir=$Sharedir"
      echo "----"
#T   Test break exit 0

#    Clear screen
#      clear
    
#P   Include the lib annex.lib which contains funtion findboo
      source $Libdir/annex.lib
   }
   #else source ./findbook.sh 
   #else source ~/Add_appendix/lib/src/findbook.sh
   #else source ~/Add_appendix/lib/src/annex.lib.sh
   else source ~/Add_appendix/lib/annex.lib
   fi
   
#P Beginnig test
#P First, test whith a existing parameter else whith urls
    if [ -n "$1" ] && [ "$1" != "global" ]; then
    {
      echo "Test for input "$1; 
      findbook $1; 
      echo "* Site name :"$Site;
      echo "* Bookname : "$Bookname; 
      echo "* Suffix : "$Suffix; 
      echo "* Bookname.Suffix = "$Bookname.$Suffix;
      if [ "$Suffix" = "compiled" ]
      then 
        if test -n $MWcompilurl; then
        echo "Compil mw url : "$MWcompilurl
        echo -n "  Compil mw syntax = [[ "$MWcompilurl; echo " ]] on $Site"
        echo "Compil full url : "$Compilfullurl
        fi
      else
        if test -n $MWbookurl; then
        echo "mw book url = "$MWbookurl
        echo -n "  mw book syntax = [[ "$MWbookurl/$Bookname; echo " ]] on $Site"
        echo "Book full url = "$Bookfullurl
        fi
      fi 
      echo "----";
      exit 0;
    } 
    else
    { 
      echo "test_findbook.bash version $VERSION"
      findbook --v
      sleep 4
      echo "----"
      
      echo "Tests with printables versions"
      echo "------------------------------"
    
      echo "1. Test for 'https://en.wikibooks.org/wiki/User:Goelette_Cardabela/Sandbox/Testbook/Contents' "
      findbook https://en.wikibooks.org/wiki/User:Goelette_Cardabela/Sandbox/Testbook/Contents
      echo "Global variables after findbook execution : " 
      echo "Site name :"$Site
      echo "Bookname : "$Bookname 
      echo "Suffix : "$Suffix 
      echo "Bookname.Suffix = "$Bookname.$Suffix
      if [ "$Suffix" = "compiled" ]
      then 
        if test -n $MWcompilurl; then
        echo "Compil mw url : "$MWcompilurl
        echo -n "  Compil mw syntax = [[ "$MWcompilurl; echo " ]] on $Site"
        echo "Compil full url : "$Compilfullurl
        fi
      else
        if test -n $MWbookurl; then
        echo "mw book url = "$MWbookurl
        echo -n "  mw book syntax = [[ "$MWbookurl/$Bookname; echo " ]] on $Site"
        echo "Book full url = "$Bookfullurl
        fi
      fi 
      echo " ----"; echo
      
      echo "2. 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 "Global variables after findbook execution : " 
      echo "Site name :"$Site
      echo "Bookname : "$Bookname 
      echo "Suffix : "$Suffix 
      echo "Bookname.Suffix = "$Bookname.$Suffix
      if [ "$Suffix" = "compiled" ]
      then 
        if test -n $MWcompilurl; then
        echo "Compil mw url : "$MWcompilurl
        echo -n "  Compil mw syntax = [[ "$MWcompilurl; echo " ]] on $Site"
        echo "Compil full url : "$Compilfullurl
        fi
      else
        if test -n $MWbookurl; then
        echo "mw book url = "$MWbookurl
        echo -n "  mw book syntax = [[ "$MWbookurl/$Bookname; echo " ]] on $Site"
        echo "Book full url = "$Bookfullurl
        fi
      fi 

      echo " ----"; echo

      echo "3. Test for 'findbook LivreTest/Contenu' "
      findbook LivreTest/Contenu
      echo "Global variables after findbook execution : " 
      echo "Site name :"$Site
      echo "Bookname : "$Bookname 
      echo "Suffix : "$Suffix 
      echo "Bookname.Suffix = "$Bookname.$Suffix
      if [ "$Suffix" = "compiled" ]
      then 
        if test -n $MWcompilurl; then
        echo "Compil mw url : "$MWcompilurl
        echo -n "  Compil mw syntax = [[ "$MWcompilurl; echo " ]] on $Site"
        echo "Compil full url : "$Compilfullurl
        fi
      else
        if test -n $MWbookurl; then
        echo "mw book url = "$MWbookurl
        echo -n "  mw book syntax = [[ "$MWbookurl/$Bookname; echo " ]] on $Site"
        echo "Book full url = "$Bookfullurl
        fi
      fi 
 
      echo " ----"; echo

      echo "Tests with compiled versions"
      echo "----------------------------";echo

      echo "4. Test for 'findbook https://fr.wikibooks.org/wiki/Utilisateur:Goelette_Cardabela/Compilations/LivreTest' "
      findbook https://fr.wikibooks.org/wiki/Utilisateur:Goelette_Cardabela/Compilations/LivreTest
      echo "Global variables after findbook execution : " 
      echo "Site name :"$Site
      echo "Bookname : "$Bookname 
      echo "Suffix : "$Suffix 
      echo "Bookname.Suffix = "$Bookname.$Suffix
      if [ "$Suffix" = "compiled" ]
      then 
        if test -n $MWcompilurl; then
        echo "Compil mw url : "$MWcompilurl
        echo -n "  Compil mw syntax = [[ "$MWcompilurl; echo " ]] on $Site"
        echo "Compil full url : "$Compilfullurl
        fi
      else
        if test -n $MWbookurl; then
        echo "mw book url = "$MWbookurl
        echo -n "  mw book syntax = [[ "$MWbookurl/$Bookname; echo " ]] on $Site"
        echo "Book full url = "$Bookfullurl
        fi
      fi 
      echo " ----"; echo

      echo "5. Test for 'findbook https://fr.wikibooks.org/wiki/Wikilivres:Compilations/Faire_fleurir_le_sel' "
      findbook https://fr.wikibooks.org/wiki/Wikilivres:Compilations/Faire_fleurir_le_sel
      echo "Global variables after findbook execution : " 
      echo "Site name :"$Site
      echo "Bookname : "$Bookname 
      echo "Suffix : "$Suffix 
      echo "Bookname.Suffix = "$Bookname.$Suffix
      if [ "$Suffix" = "compiled" ]
      then 
        if test -n $MWcompilurl; then
        echo "Compil mw url : "$MWcompilurl
        echo -n "  Compil mw syntax = [[ "$MWcompilurl; echo " ]] on $Site"
        echo "Compil full url : "$Compilfullurl
        fi
      else
        if test -n $MWbookurl; then
        echo "mw book url = "$MWbookurl
        echo -n "  mw book syntax = [[ "$MWbookurl/$Bookname; echo " ]] on $Site"
        echo "Book full url = "$Bookfullurl
        fi
      fi 
      echo " ----"; echo
      
      echo "6. Test for 'https://en.wikibooks.org/wiki/Wikibooks:Collections/Guide_to_Unix' "
      findbook https://en.wikibooks.org/wiki/Wikibooks:Collections/Guide_to_Unix
      echo "Global variables after findbook execution : " 
      echo "Site name :"$Site
      echo "Bookname : "$Bookname 
      echo "Suffix : "$Suffix 
      echo "Bookname.Suffix = "$Bookname.$Suffix
      if [ "$Suffix" = "compiled" ]
      then 
        if test -n $MWcompilurl; then
        echo "Compil mw url : "$MWcompilurl
        echo -n "  Compil mw syntax = [[ "$MWcompilurl; echo " ]] on $Site"
        echo "Compil full url : "$Compilfullurl
        fi
      else
        if test -n $MWbookurl; then
        echo "mw book url = "$MWbookurl
        echo -n "  mw book syntax = [[ "$MWbookurl/$Bookname; echo " ]] on $Site"
        echo "Book full url = "$Bookfullurl
        fi
      fi 
      echo " ----"; echo
    }
    fi
    
    if [ "$1" = "global" ]; then
      echo; echo "  ************************************************************"
      echo "  * If errors are found with the global option :             *" 
      echo "  *   find the address of the library and update it.         *"
      echo "  * Si des erreurs sont trouvées avec l'option globale :     *"
      echo "  *   repérer l'adresse de la librairie et la mettre à jour. *"
      echo "  ************************************************************"
      echo "  Libdir is $Libdir";
    fi
    echo; echo "  syntax : ./test_findbook.bash [ --v | global | <book-url> ] "
    echo "  global with 'source annex.lib', else with 'source ./findbook' "

# End test function findbook()