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

  • Cette librairie est à mettre à jour dans les répertoires :
    ~/Annexer/lib/
    et/ou
    ~/Add_appendix/lib/
    et/ou
    /usr/local/lib/

Tester le code source de la fonction findbook avec test_findbook.bash
Tester le code source de la fonction ecfpb avec ecfpb.sh (extract contents from printable book)
Tester le code source de la fonction ecfpc avec ecfpc.sh (extract contents from compiled book)

#!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
}

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 Converting accented characters
    cat $Bookname.$Suffix | sed -f ~/Add_appendix/datas/conversions.dat > $Bookname.$Suffix.conv
    cat $Bookname.$Suffix.conv > $Bookname.$Suffix
	
#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 echo "$(gettext ' file is not updated ! ... Continue ... ')"
        fi
      else cp "$Bookname.$Suffix" $Projectdir/.
      fi
    fi
}

function ecfpc ()
{
#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 version Collections
    if [ -n $Site ] && [ "$Suffix" = "compiled" ]; 
    then 
      echo "url is a version Collections."; 	 
#O   Download script from the file $Lastfield
	  #wget https://fr.wikibooks.org/wiki/Wikilivres:Compilations/Faire_sa_fleur_de_sel
	  wget $1
#T Test break exit 0

#O 	 Find the contents of file named '$Lastfield' is the downloaded html code
     cat $Lastfield | grep /$Bookname/  > beforelastfield.txt

#T	 cat beforelastfield.txt and select 
	 cat beforelastfield.txt | awk -F " " '{for (i=1;i<=NF;i++) print $i}' | grep "href=" > fieldlist.txt
	 cat fieldlist.txt | sed -e "s/href=\"\/wiki\//[[/g" | sed -e "s/\"/]]/g" > fieldlist2.txt
     cat fieldlist2.txt | sed -f ~/Add_appendix/datas/conversions.dat > $Bookname.$Suffix
   fi
     	
#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 echo "$(gettext ' file is not updated ! ... Continue ... ')"
        fi
      else cp $Bookname.$Suffix $Projectdir/.
      fi
    fi
}