Aller au contenu

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

Un livre de Wikilivres.
Documentation
#D pre-annex program documentation
#D This program use findbook to analyse the link to the book
#D If the result is correct, create the project directory, pre-create the
#D   file project.suffix and create a local html book in project directory
#D   if the suffix is 'content' or 'summary', else for 'collection' link create
#D   a local collection file
#D If everything is correct, edit the local book contents file or the local
#D   collection file to check and possibly update the file manually. Then copy
#D   the content to the project.suffix file if necessary
#D
#F Documentation du programme pre-annex 
#F Ce programme pré-annexe la construction de l'annexe du livre
#F Si ce programme s'est bien exécuté, le répertoire de projet aura été créé
#F - le fichier projet.suffixe et un fichier html local auront été créés dans
#F - le répertoire du projet
#F Il est proposé d'éditer la page 'Contenu' ou 'Sommaire' ou de 'Compilation'
#F   afin d'être éventuellement recopiée manuellement dans le fichier 
#F   projet.suffixe
#F
Program pre-annex code
#!/bin/bash
#H pre-annex header documentation
#H ------------------------------
#H Workdir is ~/Add_appendix or ~/Annexer
#H Source file     : Workdir/src/pre-annex.sh
#H Executable file : Workdir/bin/pre-annex
#H Syntax : pre-annex [ Full-url-of-book || --v ]
#H Example : ./pre-annex.sh https://fr.wikibooks.org/wiki/Wikilivres:Compilations/Faire_sa_fleur_de_sel
#P  https://fr.wikibooks.org/wiki/Wikilivres:Compilations/Faire_sa_fleur_de_sel
#P  https://fr.wikibooks.org/wiki/Wikilivres:Compilations/Faire_fleurir_le_sel
#H Created : 12/05/2021 by GC
#H Last Update on fr:Wikibooks :
#H Tested  : by GC 12/05/2021
#H Updated :  by user GC For layout
#H
#D pre-annex program documentation
#D -------------------------------
#D This program use findbook to analyse the link to the book
#D If the result is correct, create the project directory, pre-create the
#D   file project.suffix and create a local html book in project directory.
#D If everything is correct, edit a collection file or a book contents file
#D   to manually copy the content to the project/project.suffix file
#D
#w pre-annex wanings documentation
#w -------------------------------
#w Install Add_appendix or Annexer with the program executable users_install_annex.sh
#W https://fr.wikibooks.org/wiki/Auto-éditer_un_wikilivre/Annexer_(Version_internationalisée)/users_install_annex.sh
#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
#w
#O pre-annex organizational chart documentation
#O --------------------------------------------
#O pre-annex programmers documentation
#O ------------------------------------

VERSION=211005

#O If first parameter is "--v", print pre-annex version
    if [ "$1" = "--v" ]; then echo " pre-annex version $VERSION"; exit 0; fi
#O If first parameter is empty, print the syntax
    if test -z $1
    then 
      echo "Syntax : pre-annex [ --v | Full-url-of-book ] [ --t ] (with bin directory in environment variables)"
      echo "  Example in tests directory tests/pre-annex.sh/ :"
      echo "  ./pre-annex.sh https://fr.wikibooks.org/wiki/Wikilivres:Compilations/Faire_fleurir_le_sel --t"
      exit -1
    fi
#O Else, the first parameter is not empty
#P 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
    source $Workdir/vars/installdir.var
    if [ "$1" = "--t" ] || [ "$2" = "--t" ] ; then echo; echo "Global install variables :"; cat $Workdir/vars/installdir.var; fi
#T Test break exit 0
#O Find function findbook 
    Libdir=$Workdir/lib
    Booksdir=$Workdir/books
    if test -e $Libdir/annex.lib; then source $Libdir/annex.lib; elif test -e $Libdir/src/findbook.sh; \
    then source $Libdir/src/findbook.sh; else echo "Function findbook not found"; exit -1; fi
#O Find book parameter
    findbook $1
#O If option --t, print the book parameter
    if [ "$2" = "--t" ]
    then
      echo
      echo -n "--t : "; findbook --v
      echo "--t : Global variables after findbook execution : " 
      echo "--t : Site name :"$Site
      echo "--t : Bookname : "$Bookname 
      echo "--t : Suffix : "$Suffix 
      echo "--t : Bookname.Suffix = "$Bookname.$Suffix
      if [ "$Suffix" = "compiled" ]
      then 
        if test -n $MWcompilurl; then
        echo "Compil mw url : "--t : $MWcompilurl
        echo -n "--t :   Compil mw syntax = [[ "$MWcompilurl; echo " ]] on $Site"
        echo "--t : Compil full url : "$Compilfullurl
        fi
      else
        if test -n $MWbookurl; then
        echo "--t : mw book url = "$MWbookurl
        echo -n "--t :   mw book syntax = [[ "$MWbookurl/$Bookname; echo " ]] on $Site"
        echo "--t : Book full url = "$Bookfullurl
        fi
      fi 
      echo " ----"
    fi
#O If all parameter are found carry continue otherwise print the error
    if ! test $Site || ! test $Bookname || ! test $Suffix
    then
      if ! test $Site; then echo "Site not found";fi
      if ! test $Bookname; then echo "Bookname not found";fi
      if ! test $Suffix; then echo "Suffix not found";fi
    fi
#O Create the project directory
    Projectdir=$Booksdir/$Bookname
    if ! test -d "$Projectdir"; then mkdir $Projectdir; fi 
#O Create the file bookname.suffix
    if test -e $Projectdir/$Bookname.$Suffix
    then
      echo "$Bookname.$Suffix : "
      cat $Projectdir/$Bookname.$Suffix
      echo -e "\033[47m\033[1;30m  Want you update $Projectdir/$Bookname.$Suffix ? \033[0m"
      read -s -n1 -p "inkey y/n : " Inkey; echo $Inkey
      if [ "$Inkey" = "y" ]
      then 
        echo; chmod 755 $Projectdir/$Bookname.$Suffix
        if [ "$Suffix" = "compiled" ]
        then 
          if test -n $MWcompilurl; then echo "[[$MWcompilurl]]" > $Projectdir/$Bookname.$Suffix; fi 
        else
          if test -n $MWbookurl; then echo "[[$MWbookurl]]" > $Projectdir/$Bookname.$Suffix; fi
        fi
      fi
    else 
      if [ "$Suffix" = "compiled" ]
      then 
        if test -n $MWcompilurl; then echo "[[$MWcompilurl]]" > $Projectdir/$Bookname.$Suffix; fi
      else
        if test -n $MWbookurl; then echo "[[$MWbookurl]]" > $Projectdir/$Bookname.$Suffix; fi 
      fi 
    fi
#O Download the book in html form
#O Télécharger le site récursivement avec une profondeur infinie ( -linf ), \
#O convertit les liens pour une consultation en local ( -k ), \
#O rapatrie tous les fichiers nécessaires à l'affichage convenable d'une page HTML ( -p ) \
#O et renomme toutes les pages HTML avec l'extension .html ( -E ) 
   if ! test -d "$Workdir/temp"; then mkdir $Workdir/temp; fi
   cd $Projectdir
   if test -n "$Bookfullurl"; then echo "Dowload $Bookfullurl"; wget -r -linf -k -p -E "$Bookfullurl" -o $Workdir/temp/wget-log-télécharger.txt
   elif test -n "$Compilfullurl"; then echo "Dowload $Compilfullurl"; wget -r -linf -k -p -E "$Compilfullurl" -o $Workdir/temp/wget-log-télécharger.txt
   else echo " book full url not found to download"
   fi
#O If everything is correct, edit a collection file or a book contents file
#O   to manually copy the content to the project/project.suffix file
   if test $Site || test $Bookname || test $Suffix
   then
     echo -e "\033[31mAttention : See and possibly complete the $Bookname.$Suffix "; echo -e "Edit and copy the wiki-code in $Workdir/$Bookname/$Bookname.$Suffix :\033[0m"
     echo -e "\033[47m\033[1;30m  Go forward to edit the book and copy wiki-code to $Bookname.$Suffix ? \033[0m"
     read -s -n1 -p "inkey y/n" Inkey; echo "$Inkey"
     if [ "$Inkey" = 'y' ] 
     then
       gedit $Workdir/$Book/$Bookname/$Bookname.$Suffix&
       xdg-open $1
     fi
   else 
     echo -e "\033[31m Non-compliant result \033[0m"
   fi
   #T xdg-open file:///home/jpl/Annexer/Faire_fleurir_le_sel/fr.wikibooks.org/wiki/Faire_fleurir_le_sel.html
   #T xdg-open https://fr.wikibooks.org/w/index.php?title=Auto-%C3%A9diter_un_wikilivre/Auto-r%C3%A9f%C3%A9rencer/LivreTest/LivreTest.compil%C3%A9&action=edit
   echo -n "$Projectdir/" > localbook.txt
   echo "$Bookfullurl" | cut -d '/' -f3-  >> localbook.txt
#T cat localbook.txt
   read Localbook < localbook.txt
   Htmlbook=$Localbook.html
   echo "Htmlbook = $Htmlbook"
#T Test break exit 0
   if test -e $Htmlbook 
   then 
     echo -e "\033[47m\033[1;30m  Do you want to see the local html book ? \033[0m"
     read -s -n1 -p "inkey y/n :" Inkey; echo "$Inkey"; echo
     if [ "$Inkey" = 'y' ]; then xdg-open file://$Htmlbook ; fi
   else echo "$Htmlbook not found" 
   fi
   Varsdir=$Workdir/vars
#O Copy the variables from the book to 'vars/$Bookname.var'
   if test -n "$Site"; then echo "Site=$Site" > $Varsdir/$Bookname.var; fi
   if test -n "$Bookname"; then echo "Bookname=$Bookname" >> $Varsdir/$Bookname.var; fi
   if test -n "$Suffix"; then echo "Suffix=$Suffix" >> $Varsdir/$Bookname.var; fi
   if test -n "$Projectdir"; then echo "Projectdir=$Projectdir" >> $Varsdir/$Bookname.var; fi
   if test -n "$Bookfullurl"; then echo "Bookfullurl=$Bookfullurl" >> $Varsdir/$Bookname.var; fi
   if test -n "$Compilfullurl"; then echo "Compilfullurl=$Compilfullurl" >> $Varsdir/$Bookname.var; fi
   if test -n "$Htmlbook"; then echo "Htmlbook=$Htmlbook" >> $Varsdir/$Bookname.var; fi
   exit 0
#O End pre-annex.sh

unrecognized_links.dat

[modifier | modifier le wikicode]

Data file in $Datasdir to remove unwanted links

title
Spécial
Accueil
Vitrine
Tous_les_livres
Rechercher_un_livre
Wikijunior
Portail_communautaire
Le_Bistro/2021
Special:
Aide:Accueil
propos_de
https://fr.wikibooks.org/wiki/Wikilivres
Wikilivres:
Catégorie:
Category
Main_Page
Help:Contents
Card_Catalog_Office
Cookbook:Table_of_Contents
Featured_books
Using_Wikibooks
Reading_room
Community_Portal
Reading_room/Bulletin_Board
Maintenance
Policies_and_guidelines
Contact_us
Welcome
General_disclaimer

test_pre-annex.bash

[modifier | modifier le wikicode]

Install this code in ~/Add_appendix/tests/prannex.sh to test preannex.sh

#!/bin/bash
#H File    : test_pre-annex.bash  executable for testing the program pre-annex
#H Syntax  : ./test_pre-annex.bash <full-url-book-name>  (on the terminal for testing)
#H Example : ./test_pre-annex.bash https://fr.wikibooks.org/wiki/Utilisateur:Goelette_Cardabela/Sandbox/LivreTest/Contenus --t
#H         : ./test_pre-annex.bash [ --v | <full-url-book-name> ] --t
    
VERSION=210619

#O Clean screen
clear

#P 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 drectory not found"; exit -1;fi
    rm pwd.txt#O Download global variables
    source $Workdir/datas/installdir.var

#O Print prog, version and Why
    if [ "$1" = "--v" ]; 
    then 
      echo "Command 'test_pre-annex.bash' is $VERSION for testing pre-annex function";
      echo "  Options : [ --v | <full-url-book-name> ] --t"
      exit 0;
    fi
    
#O Control if the test pre-annex.sh file is clean
    echo -e "\033[1;33m Control pre-annex.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 -e "\033[1;33m ----  \033[0m"
    grep -n -e "#T\|#O\|#P" pre-annex.sh >> 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
    

   echo " Beginning tests without parameter"
   ./pre-annex.sh 
   echo "----"; echo
   sleep 2
   echo " Test whith parameter --v"
   ./pre-annex.sh --v     
   echo "----"; echo
   sleep 2
   
#T Test, all with option --t 
   echo " All tests whith parameter --t"
   
#P List fullurl 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 ./pre-annex.sh https://fr.wikibooks.org/wiki/Utilisateur:Goelette_Cardabela/Sandbox/LivreTest/Contenu --t
      elif [ "$Inkey" = "2" ]; then ./pre-annex.sh https://fr.wikibooks.org/wiki/Hélices_de_navires_à_déplacement/Sommaire --t
      elif [ "$Inkey" = "3" ]; then ./pre-annex.sh https://en.wikibooks.org/wiki/User:Goelette_Cardabela/Sandbox/TestBook/Content --t
      elif [ "$Inkey" = "4" ]; then ./pre-annex.sh https://fr.wikibooks.org/wiki/Mkd_\(Extracteur_de_documents\)/Sommaire --t
      elif [ "$Inkey" = "5" ]; then ./pre-annex.sh https://fr.wikibooks.org/wiki/Wikilivres:Compilations/Faire_fleurir_le_sel --t
      elif [ "$Inkey" = "6" ]; then ./pre-annex.sh https://fr.wikibooks.org/wiki/Wikilivres:Compilations/Faire_sa_fleur_de_sel --t      
      elif [ "$Inkey" = "7" ]; then ./pre-annex.sh https://en.wikibooks.org/wiki/Wikibooks:Collections/Guide_to_Unix --t
      elif [ "$Inkey" = "8" ]; then ./pre-annex.sh https://fr.wikibooks.org/wiki/Utilisateur:Goelette_Cardabela/Compilations/Tests_de_la_capacité_des_batteries_d%27accumulateurs
      elif [ "$Inkey" = "9" ]; then ./pre-annex.sh https://fr.wikibooks.org/wiki/Utilisateur:Goelette_Cardabela/Compilations/LivreTest --t
      else echo "$inkey full url of site not found"; exit 0
      fi
    done

Install this code in /Add_appendix/tests, to product sitelist.txt

#!/bin/bash
#H File sitelist to test with 'test_preannex.bash in preannex.sh tests directory
#H Application for source ~/Add_appendix/datas/sitelist in 'test_preannex.bash'
#H   or ./sitelist in datas directory in executable mode
#H Created 14/05/2021 by GC
#H Updated 19/06/2021 by GC
#P Make sitelist.txt : ./sitelist > sitelist.txt in datas directory
#
echo "sitelist to test annex programs, version 210516"
echo -e "\033[47m \033[1;32m   Books with Contents or Summary   \033[0m"
echo " 1 - https://fr.wikibooks.org/wiki/Utilisateur:Goelette_Cardabela/Sandbox/LivreTest/Contenu --t"
echo " 2 - https://fr.wikibooks.org/wiki/Hélices_de_navires_à_déplacement/Sommaire --t"
echo " 3 - https://en.wikibooks.org/wiki/User:Goelette_Cardabela/Sandbox/TestBook/Content --t"
echo " 4 - https://fr.wikibooks.org/wiki/Mkd_\(Extracteur_de_documents\)/Sommaire --t"
# echo -e "\033[47m \033[1;32m   Books with Contents or Summary   \033[0m"
echo -e "\033[47m \033[1;32m             Collections            \033[0m"
echo " 5 - https://fr.wikibooks.org/wiki/Wikilivres:Compilations/Faire_fleurir_le_sel --t"
echo -e "\033[1;31m  Same book second title \033[0m"
echo " 6 - https://fr.wikibooks.org/wiki/Wikilivres:Compilations/Faire_sa_fleur_de_sel --t"
echo " 7 - https://en.wikibooks.org/wiki/Wikibooks:Collections/Guide_to_Unix --t"
echo " 8 - https://fr.wikibooks.org/wiki/Utilisateur:Goelette_Cardabela/Compilations/Tests_de_la_capacit%C3%A9_des_batteries_d%27accumulateurs"
echo " 9 - https://fr.wikibooks.org/wiki/Utilisateur:Goelette_Cardabela/Compilations/LivreTest --t"
# echo -e "\033[47m \033[1;32m   Books with Contents or Summary   \033[0m"
echo -e "\033[47m \033[1;32m          Atypical variants         \033[0m"
echo -e "\033[1;31m  Copy the line (Ctrl-Shift-C) and execute in terminal (Ctrl-Shift-V) \033[0m"
echo " xdg-open https://fr.wikibooks.org/wiki/Auto-éditer_un_wikilivre/Auto-référencer/LivreTest/LivreTest.compilé"
echo " xdg-open https://fr.wikibooks.org/w/index.php?title=Auto-éditer_un_wikilivre/Auto-référencer/LivreTest/LivreTest.compilé&action=edit"
sitelist.txt to test program annex sub-programs
sitelist to test annex programs, version 210516
�[47m �[1;32m   Books with Contents or Summary   �[0m
 1 - https://fr.wikibooks.org/wiki/Utilisateur:Goelette_Cardabela/Sandbox/LivreTest/Contenu --t
 2 - https://fr.wikibooks.org/wiki/Hélices_de_navires_à_déplacement/Sommaire --t
 3 - https://en.wikibooks.org/wiki/User:Goelette_Cardabela/Sandbox/TestBook/Content --t
 4 - https://fr.wikibooks.org/wiki/Mkd_\(Extracteur_de_documents\)/Sommaire --t
�[47m �[1;32m             Collections            �[0m
 5 - https://fr.wikibooks.org/wiki/Wikilivres:Compilations/Faire_fleurir_le_sel --t
�[1;31m  Same book second title �[0m
 6 - https://fr.wikibooks.org/wiki/Wikilivres:Compilations/Faire_sa_fleur_de_sel --t
 7 - https://en.wikibooks.org/wiki/Wikibooks:Collections/Guide_to_Unix --t
 8 - https://fr.wikibooks.org/wiki/Utilisateur:Goelette_Cardabela/Compilations/Tests_de_la_capacit%C3%A9_des_batteries_d%27accumulateurs
 9 - https://fr.wikibooks.org/wiki/Utilisateur:Goelette_Cardabela/Compilations/LivreTest --t
�[47m �[1;32m          Atypical variants         �[0m
�[1;31m  Copy the line (Ctrl-Shift-C) and execute in terminal (Ctrl-Shift-V) �[0m
 xdg-open https://fr.wikibooks.org/wiki/Auto-éditer_un_wikilivre/Auto-référencer/LivreTest/LivreTest.compilé
 xdg-open https://fr.wikibooks.org/w/index.php?title=Auto-éditer_un_wikilivre/Auto-référencer/LivreTest/LivreTest.compilé&action=edit