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

Un livre de Wikilivres.
Aller à la navigation Aller à la recherche
Remarquelink={{{link}}}

Ce fichier d'entête doit être inclus dans tous les modules par la commande 'source' :
source $Bindir/header.inc
Dans ses dernières versions :
- header.inc.sh est un fichier source et header.inc est un bichier exécutable
- 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.
- Les messages en anglais sont introduits pour l'internalisation avec gettext.
En deuxième partie: exemple de fichier de commande d'un test tests/header.inc/test_header.inc.sh

src/header.inc.sh[modifier | modifier le wikicode]

  1. #!/bin/bash
    #H File    : bin/header.inc or src/header.inc.sh
    #H Version : 2020-05-24
    #H Syntax  : header.inc [ <book-name> | ? ] [ -v ] [ -pb | -pc ]
    #H Example : ./header.inc LivreTest (on the terminal for unit tests)
    #H Creation: 2020-03-24 
    #H           see https://fr.wikibooks.org/wiki/Auto-éditer_un_wikilivre/Auto-référencer#Entête_des_modules
    #H Updated : 2020-05-15 by GC
    #H Updated : 2020-05-24 By GC to translate
    #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-04-01
    #H
    
    . gettext.sh
    
    Version=210421
    Bookname=
    Suffix=
    Site=
    
    clear
    
    #P Initializes Bindir
        if [ "$0" = "./header.inc" ]; 
        then 
          pwd > pwd.txt
        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
          mkdir $Sharedir
          echo "$(gettext ' The Share directory  ')"$Sharedir"$(gettext '  is created ! ')";
        fi
    
        echo $Bindir > $Sharedir/bindir.txt
        echo $Installdir > $Sharedir/installdir.txt
        echo $Libdir > $Sharedir/libdir.txt
    
    
        Workdir=~/Add_appendix
    
        if test -d $Workdir
        then 
          echo "$(gettext ' The working directory :  ')"$Workdir"$(gettext '  is OK ! ')"
          echo $Workdir > $Sharedir/workdir.txt
        else
          mkdir $Workdir
          echo "$(gettext ' The working directory  ')"$Workdir"$(gettext '  is created ! ')";
        fi
        echo $Sharedir > $Workdir/sharedir.txt
    
    #P Initialyze $Conversions file
        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 "$(gettext ' When the command is ./header.inc, then conversion file is recreated to ')"$Conversions
          rm $Workdir/conversions.dat $Conversions
        fi
    
        if test -e "$Conversions"
        then echo "$(gettext ' The conversion file is present ')"
        else 
        {
          echo "s/%24/\$/g;s/%21/\!/g;s/%25/%/g;s/%2B/+/g;s/%3C/\\\&lt\;/g;s/%3E/\&gt\;/g;" >> $Conversions
          echo "s/%5C/\\\/g;s/%B0/°/g" >> $Conversions
          echo "s/%E0/à/g;s/%E2/â/g" >> $Conversions
          echo "s/%E8/è/g;s/%E9/é/g;s/%EA/ê/g;s/%EB/ë/g" >> $Conversions
          echo "s/%EE/î/g;s/%EF/ï/g" >> $Conversions
          echo "s/%F4/ô/g" >> $Conversions
          echo "s/%F9/ù/g;s/%FB/û/g" >> $Conversions
          echo "s/%25/%/g;" >> $Conversions
          echo "s/%28/(/g;s/%29/)/g;" >> $Conversions
          echo "s/%2B/+/g;" >> $Conversions
          echo "s/%3A/:/g;" >> $Conversions
          echo "s/%3D/=/g;" >> $Conversions
          echo "s/%5B/[/g;" >> $Conversions
          echo "s/%5D/]/g" >> $Conversions
          echo "s/%3B/;/g;s/%2F/\//g;s/%27/''/g" >> $Conversions
          echo 's/%22/"/g' >> $Conversions
          echo "s/%C3%A7/ç/g;s/%E7/ç/g" >> $Conversions
          echo "s/%C3%A0/à/g;s/%C3%A2/â/g" >> $Conversions
          echo "s/%C3%A9/é/g;s/%C3%A8/è/g;s/%C3%AA/ê/g" >> $Conversions
          echo "s/%C3%AE/î/g;s/%C3%AF/ï/g" >> $Conversions
          echo "s/%C3%B4/ô/g" >> $Conversions
          echo "s/%C3%B9/ù/g;s/%C3%BB/û/g" >> $Conversions
          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
    
    #P Include the header syntax function for individual test
        function header_syntax
        {
          echo "$(gettext ' Syntax of the header.inc command in test mode: ./header.inc <book> ')"
          echo "$(gettext ' Example : "./header.inc LivreTest" or "./header.inc ?" ')"
    	    echo ""
          exit 0
        }
    
    #P For a first parameter empty print syntax else print Bookname
        if test -z "$1" #S1
        then 
          echo "-----"
          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
    
    #P Initialize the cariable Project directory
        Project=$Workdir/$1
        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, ')"$Project/$1.list"$(gettext ' is not yet created.\033[0m ')"
        fi
    echo "----------"
    
    #P Test if the .prj file exist and print the result
        echo "$(gettext ' Test if the file $Project/$1.prj, exists: ')$Project/$1.prj"
        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
        echo "----------"
    
    #P If the first parameter is not empty, print the file list in the project diretory
        if [ -n $1 ]; 
        then echo "$(gettext ' Contents in the project directory: ')$Project";
          ls -al $Project 
        fi
        echo "----------"
        
    # End header.inc.sh
    

tests/header.inc/test_header.inc.sh[modifier | modifier le wikicode]

Exemple de fichier de commande d'un test dans le répertoire ~/Add_appendix/tests/header.inc/.
  1. #!/bin/bash
    # File test_header.inc.sh in ~/Add_appendix/tests/header.inc/.
    #D 
       Version=210401
    
    #O nettoyer l'écran
        clear
    
    #O Initialisation de la variable Workdir
        Workdir=~/Add_appendix
        echo "Espace de travail = $Workdir"
        
    #O Vérifier que la syntaxe est respectée :
        echo "Syntax  : header.inc [ <book-name> | ? ] [ -v ] [ -pb | -pc ]"
        echo "----"
        
        $Workdir/bin/header.inc
        echo "Test sans paramètre. Code de retour = $?"
        sleep 5
        echo "----"
        
        $Workdir/bin/header.inc ?
        echo "Test avec le paramètre '?'. Code de retour = $?"
        sleep 5
        echo "----"
      
        $Workdir/bin/header.inc LivreTestInexistant
        echo "Test avec paramètre avec paramètre 'LivreTestInexistant'. Code de retour = $?" 
        sleep 5
        echo "----"
        
        if [ -d "$Workdir/LivreTest" ]
        then
          $Workdir/bin/header.inc LivreTest
          echo "Test avec le paramètre 'LivreTest'. Code de retour = $?" 
        else 
          echo "Test avec le paramètre 'LivreTest' :"
          echo "Le répertoire de projet LivreTest n'est pas trouvé"
          echo "La commande n'a pas été exécutée."
        fi