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

Un livre de Wikilivres.

add-sclic.sh[modifier | modifier le wikicode]

Cette commande appelle en_scli.inc ou fr_scli.inc selon l'origine de wikibooks

#! /bin/bash
#HF Fichier : add-sclic.sh
#HF Nom du fichier de commandes : add-sclic
#HF Syntaxe : "add-sclic <nom du livre> [ --t ]"
#HF Exemple : "./add-sclic LivreTest --t" à la console.
#HF
#DF La commande ./add_sclic <wikilivre> commence par créer la page
#DF   $Projet/$1.sclic comme elle apparaîtra dans la première partie de la page
#DF   "Annexe" du livre avec les titre en wikitexte et les sections :
#DF   Références: toutes celles qui n'ont pas été affichées dans les pages.
#DF   Contenus: liens établis vers la page du livre et les articles (sous-pages)
#DF   Source de l'édition: lien vers la page qui a permis de créer l' "Annexe".
#DF   Sources licences et contributeurs des articles: ne concerne que les
#DF   articles, les sous-pages.
#DF
#DE the command ./add_sclic <Wiki-book>[--t], first create the page 
#DE   $ Project / $ 1.sclic as it will appear in the "Appendix" page with the 
#DE   titles and Wikitext sections:
#DE   References : all references not referenced in articles
#DE   Contents : links to book articles
#DE   Sources of the edition : link to the page that allows you to create the
#DE     annex page
#DE   Licenses sources and article contributors : only articles and sub-pages
#DE
TEXTDOMAIN=add_sclic
TEXTDOMAINDIR="/usr/share/locale"
VERSION=211008
#O . gettext.sh for translation
   . gettext.sh
#O If first parameter is './add-sclic' print the add_sclic version 
    if [ "$0" = "./add-sclic" ]; then echo "add-sclic : Version "$VERSION; fi
#O Include the file header.inc
    if test -e ~/Add_appendix/bin/header.inc; then source ~/Add_appendix/bin/header.inc
    else echo $"$Bindir/header.inc not found. Execute sbin/cp-src2bin.sh"; exit -1
    fi
#O Create variable PageSclic
    Projectdir=$Workdir/books/$1
    PageSclic="$Projectdir/$1.sclic"
    echo $PageSclic
#O Inclure la commande scli.inc
    source $Bindir/scli.inc
#O Page scli classique initialization with the title 'Images sources, etc.
    cat $Projectdir/$1.scli > $PageSclic
#O Include the command corresponding to the origin of the book, depending on execution in test mode
    if [ "$2" = "--t" ]
    then 
#T pwd > pwd.txt; read Localdir < pwd.txt; rm pwd.txt; else Localdir=$Bindir; fi
    {
      pwd > pwd.txt; read Localdir < pwd.txt; rm pwd.txt
      if [ "$Site" = "fr.wikibooks.org" ]; then source $Localdir/fr_sclic.inc.sh
      else source $Localdir/en_sclic.inc.sh
      fi
    }
    else
      if [ "$Site" = "fr.wikibooks.org" ]; then source $Bindir/fr_sclic.inc
      else source $Bindir/en_sclic.inc
      fi
    fi
# end file add_sclic.sh

en_sclic.inc[modifier | modifier le wikicode]

#! /bin/bash
#H File : en_scli.inc include in add at add_scli.sh
#P . gettext for translation
. gettext.sh
TEXTDOMAIN=en_sclic.inc
TEXTDOMAINDIR="/usr/share/locale"
VERSION=211008
    cd $Projectdir
    if [ "$2" = "--t" ]
    then 
      echo "Projectdir = $Projectdir"
      echo "en_sclic version = $VERSION"
    fi   
#O Select lines containing 'fichier:', 'file', image and create bookname.files
    cat $Projectdir/$1/$1.str | grep -n -i -e fichier: -e file: -e image: > $Projectdir/$1/$1.files
#O Select lines containing 'fichier:', '.jpg', '.png', '.gif' and create bookname.pict
    cat $Projectdir/$1/$1.str | grep -n -i -e fichier: -e .jpg -e .png -e .gif > $Projectdir/$1/$1.picts
#O Sélect in bookname.files, the lines containing 'title', remove the tag <div> cut ">" and select the last champ to create bookname.illustrations
    cat $Projectdir/$1/$1.files | grep title |sed "s/<\/div>//g" | awk -F">" '{print $NF}' > $Projectdir/$1/$1.illustrations
#O Dans le fichier .files avec le séparateur "=" imprimer dans le champ 'i' le retour chariot, sélectionner le slignes conteneant 'https', remplacer le caractère '"' par '!'
#O   et sélectionner le trosième champ, puis relélectionner la ligne contenant https, remplacer le caractère '>' par !, supprimer </a, puis supprimer le caractère '!'
#O   et créer le fichier.links
       cat $Projectdir/$1/$1.files | awk -F"=" '{for (i=1;i<=NF;i++) print $i "\n"}' | grep https | sed "s/\"/!/g" | cut -d '!' -f3 \
       | grep https | tr '>' ! | sed "s/<\/a//g" |sed "s/!//g" > $Projectdir/$1/$1.links
#OF Télécharger les fichiers contenus dans la liste du fichier bookname.links
#O Download the files contained in the list of the bookname.links file
    wget -P $Projectdir/$1 -r -linf -k -p -E -i $Projectdir/$1/$1.links       
#O Copy html liles from ./commons.wikimedia.org/wiki in the curreny directory
    cd $1
    if test -e commons.wikimedia.org; then cp -R commons.wikimedia.org/wiki/*.html . ; fi
#O html.list initialization
    echo -n "" > html.list
    if test -s $Projectdir/$1/$1.links
    then 
    { 
      echo $"$Projectdir/$1/$1.links is not empty" 
#OF  Tant qu'on lit des lignes dans le fichier .links, lire les images et les lister dans html.list  
#O   As long as there is a line in file html.links, read the line and copy it to html.list
      while read line
      do
      echo $line | awk -F"/" '{print $NF}' | cut -d '%' -f1 | cut -d '.' -f1 > tmp
      read Image < tmp
      ls $Image*.html  >> html.list
      echo "Image : "$Image.html  
      done < $Projectdir/$1/$1.links
    }
    elif test -s $Projectdir/html.list; then cp $Projectdir/html.list $Projectdir/$1/html.list
    else echo $"No images found in $Projectdir/$1"; exit 0
    fi
    
    echo " *** References : image, src, lic, contrib."
#O As long as there is a line in file html.list extract illustrations, sources, licenses, authors(s)	   
    while read line
    do
       echo
       echo
       echo "$(gettext '**** line = ')$line ****"
       echo
	   
       mkd -pw '**' $line $line.tmp
       cat $line.tmp | tr ',' '\n' > $line.str 
     
       echo -n "'''$(gettext 'Illustration : ')'''" > $line.title
       cat $line.str |grep wgTitle | cut -d '"' -f4 >> $line.title
       cat $line.title >> $PageSclic 
       cat $line.title

       echo -n ", ''$(gettext ' source : ')''https://"$Site"/w/index.php?title= " > $line.source
       echo $line | sed "s/.html//g" >> $line.source
       cat $line.source >> $PageSclic
       cat $line.source

       echo -n ", ''$(gettext ' license : ')''" > $line.license
       cat $line.str | grep licensetpl_short | sed "s/<td>//g" | sed "s/<span class//g" | sed "s/<\/span>//g" | sed "s/style=\"display:none;\"//g" | tr '=' '\n' | grep licensetpl_short | awk -F">" '{print $NF}' >> $line.license
       cat $line.license >> $PageSclic
       cat $line.license
	   
       echo -n ", ''$(gettext ' author(s) : ')''" > $line.authors
       rm tmp
       cat $line.str | grep -i -n -m1 -A 1 -e Author | grep -i -e user -e utilisteur -e auteur | tr '/' '\n' | grep -i -e user -e utilisteur -e auteur | cut -d '"' -f1 > tmp
       if test -s tmp 
       then cat tmp >> $line.authors
       else echo "-" >> $line.authors
       fi
       cat $line.authors >> $PageSclic
       cat $line.authors	   
       echo >> $PageSclic
    done < html.list
	
    echo "</div>" >> $PageSclic
    echo "$(gettext '{{Newpage}} ')" >> $PageSclic
exit 0 
#end of file en_sclic.inc.sh

fr_sclic.inc[modifier | modifier le wikicode]

#! /bin/bash
#H File : fr_sclic.inc include in add at add_sclic.sh
Version=210819
    cd $Projectdir
    if [ "$2" = "--t" ]
    then 
      echo "Projectdir = $Projectdir"
      echo "fr_sclic version = $Version"
    fi    
#O Sélectionner les lignes contenant 'fichier:', 'file', image et créer le fichier .files
    cat $Projectdir/$1/$1.str | grep -n -i -e fichier: -e file: -e image: > $Projectdir/$1/$1.files
#O Sélectionner les lignes contenant 'fichier:', '.jpg', '.png', '.gif' et créer le fichier .pict
    cat $Projectdir/$1/$1.str | grep -n -i -e fichier: -e .jpg -e .png -e .gif > $Projectdir/$1/$1.picts
#O Sélectionner dans le fichier .files, les lignes contenant 'title', supprimer le tag <div> couper ">" et sélectionner le dernier champ pour créer le fichier des illustrations
    cat $Projectdir/$1/$1.files | grep title |sed "s/<\/div>//g" | awk -F">" '{print $NF}' > $Projectdir/$1/$1.illustrations
#O Dans le fichier .files avec le séparateur "=" imprimer dans le champ 'i' le retour chariot, sélectionner le slignes conteneant 'https', remplacer le caractère '"' par '!'
#O   et sélectionner le troième champ, puis relélectionner la ligne contenant https, remplacer le caractère '>' par !, supprimer </a, puis supprimer le caractère '!'
#O   et créer le fichier.links
       cat $Projectdir/$1/$1.files | awk -F"=" '{for (i=1;i<=NF;i++) print $i "\n"}' | grep https | sed "s/\"/!/g" | cut -d '!' -f3 \
       | grep https | tr '>' ! | sed "s/<\/a//g" |sed "s/!//g" > $Projectdir/$1/$1.links
#O Télécharger les fichiers contenus dans la liste du fichier bookname.links
    wget -P $Projectdir/$1 -r -linf -k -p -E  -i $Projectdir/$1/$1.links
#O Copy html files from commons.wikimedia.org/wiki if this file exist, in current directory 
    cd $1
    if test -e commons.wikimedia.org; then cp -R commons.wikimedia.org/wiki/*.html . ; fi
#O Copy html files from $Site if this file exist, in current directory
    if test -e $Site; then cp -R $Site/wiki/*.html . ; fi
#O Initialiser le fichier html.list
    echo -n "" > html.list
    if test -s $Projectdir/$1/$1.links
    then {
#O Tant qu'on lit des lignes dans le fichier .links, lire les images et les lister dans html.list  
    while read line
    do
    echo $line | awk -F"/" '{print $NF}' | cut -d '%' -f1 | cut -d '.' -f1 > tmp
    read Image < tmp
    ls $Image*.html  >> html.list
    echo "Image : "$Image.html  
    done < $Projectdir/$1/$1.links
    }
    elif test -s $Projectdir/html.list; then cp $Projectdir/html.list $Projectdir/$1/html.list
    else echo "Aucune images trouvées dans $Projectdir/$1"; exit 0
    fi

    echo "*** Références : image, src, lic, contrib. "
#O tant que l'on lit des lignes dans le fichier html.list extraire les illustrations, sources, licences, auteurs(s)	   
    while read line
    do
       echo ""
       echo ""
       echo "**** ligne lue = $line ****"
       echo ""
	   
       mkd -pw '**' $line $line.tmp
       cat $line.tmp | tr ',' '\n' > $line.str 
     
       echo -n "'''Illustration : '''" > $line.title
       cat $line.str |grep wgTitle | cut -d '"' -f4 >> $line.title
       cat $line.title >> $PageSclic 
       cat $line.title

       echo -n ", ''source : ''https://"$Site"/w/index.php?title=" > $line.source
       echo $line | sed "s/.html//g" >> $line.source
       cat $line.source >> $PageSclic
       cat $line.source

       echo -n ", ''licence : ''" > $line.license
       cat $line.str | grep licensetpl_short | sed "s/<td>//g" | sed "s/<span class//g" | sed "s/<\/span>//g" | sed "s/style=\"display:none;\"//g" | tr '=' '\n' | grep licensetpl_short | awk -F">" '{print $NF}' >> $line.license
       cat $line.license >> $PageSclic
       cat $line.license
	   
       echo -n ", ''auteur(s) : ''" > $line.auteur
       rm tmp
       cat $line.str | grep -i -n -m1 -A 1 -e Author | grep -i -e user -e utilisteur -e auteur | tr '/' '\n' | grep -i -e user -e utilisteur -e auteur | cut -d '"' -f1 > tmp
       if test -s tmp 
       then cat tmp >> $line.auteur
       else echo "-" >> $line.auteur
       fi
       cat $line.auteur >> $PageSclic
       cat $line.auteur 	   
       echo >> $PageSclic
    done < html.list
	
    echo "</div>" >> $PageSclic
    echo "{{Nouvelle page imprimée}}" >> $PageSclic
exit 0
# fin de fichier fr_sclic.sh

tests_add_sclic.bash[modifier | modifier le wikicode]

#! /bin/bash
#P file : tests_add_sclic.bash
#P Syntax ./tests_add_sclic.bash <short-bookname> [ --t ]
VERSION=210618
#O Clean screen
    clear
#O Include install variables
    pwd > pwd.txt
    if cat pwd.txt | grep ~/Annexer; then Workdir=~/Annexer; elif cat pwd.txt | grep ~/Add_appendix; \
    then Workdir=~/Add_appendix; else echo "Workind directory not found"; exit -1;fi
    rm pwd.txt    
#O add_sclic global variables
    source $Workdir/vars/installdir.var
#O Print tests_add_sclic.bash version
    echo " tests_add_sclic.bash version = $VERSION"
    sleep 2
#T    echo "----"
#O Control if the test add_sclic.sh file is clean
    echo -e "\033[1;33m Control add_sclic.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
    echo >> text-control.txt
    
    echo -e "\033[1;33m### add_sclic.sh ###\033[0m" >> text-control.txt
    grep -n -e "#T\|#O\|#P" add_sclic.sh >> text-control.txt
    echo >> text-control.txt
    
    echo -e "\033[1;33m###### en_sclic.inc.sh ###\033[0m" >> text-control.txt
    grep -n -e "#T\|#O\|#P" en_sclic.inc.sh >> text-control.txt 
    echo >> text-control.txt
    
    echo -e "\033[1;33m###### fr_sclic.inc.sh ###\033[0m" >> text-control.txt
    grep -n -e "#T\|#O\|#P" fr_sclic.inc.sh >> text-control.txt 
    echo -e "\033[1;33m ----  \033[0m" >> text-control.txt
    more text-control.txt
    echo; echo -e "\033[47m\033[1;30m Continue ? \033[0m"
    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
    #O Test with first param empty
    echo "Command ./add_sclic.sh without parameter"
    ./add_sclic.sh
    echo;echo " wait for 2 sec"
    sleep 2; echo "----"
#O Test, all with option --t 
   echo " All tests whith parameter --t"   
#O add_sclic local books for tests
    Inkey=1
    for i in 'seq 1 9'
    do 
      cat $Workdir/tests/sitelist.txt; echo
      echo -e "\033[47m\033[1;30m Choose the site number to test \033[0m"
      read -s -n1 -p "inkey number 1 to 9 : " Inkey; echo;
      if [ "$Inkey" = "1" ]; then ./add_sclic.sh LivreTest --t
      elif [ "$Inkey" = "2" ]; then ./add_sclic.sh Hélices_de_navires_à_déplacement --t
      elif [ "$Inkey" = "3" ]; then ./add_sclic.sh TestBook --t
      elif [ "$Inkey" = "4" ]; then ./add_sclic.sh Mkd_\(Extracteur_de_documents\) --t
      elif [ "$Inkey" = "5" ]; then ./add_sclic.sh Faire_fleurir_le_sel --t
      elif [ "$Inkey" = "6" ]; then ./add_sclic.sh Faire_sa_fleur_de_sel --t      
      elif [ "$Inkey" = "7" ]; then ./add_sclic.sh Guide_to_Unix --t
      elif [ "$Inkey" = "8" ]; then ./add_sclic.sh Tests_de_la_capacité_des_batteries_d%27accumulateurs --t
      elif [ "$Inkey" = "9" ]; then ./add_sclic.sh LivreTest --t
      else echo "$inkey local url of site not found"; exit 0
      fi
    done
#O End tests_add_sclic.bash