Pdfreorder rewrite pha
Rewrite pdfreorder to take "key [page_start [page_end]]" as keys.
Merge request reports
Activity
requested review from @nrichard
1 1 #!/usr/bin/env bash 2 2 3 # Usage: $0 fichier.pdf <liste_matricules 4 5 # chaque ligne de liste_matricules est le matricule d'une page du pdf 3 err() { 4 echo -e "$@" >&2 printf '%s\n' "$@" est généralement considéré plus safe.
voir p.ex. https://unix.stackexchange.com/questions/65803/why-is-printf-better-than-echo
8 err "$@" 9 exit 1 10 10 } 11 warn() { 12 printf >&2 "%s\n" "$*" 11 12 badusage() { 13 usage 14 die "\nBad usage:\n$*" 13 15 } 14 16 15 say() { 16 printf "%s\n" "$*" 17 on_exit() { 18 # Cleanup temp files 19 if [ -n "$tmp_pdf_file" ] && [ -e "$tmp_pdf_file" ] 19 if [ -n "$tmp_pdf_file" ] && [ -e "$tmp_pdf_file" ] 20 then 21 rm "$tmp_pdf_file" 22 fi 23 if [ -n "$tmp_toc" ] && [ -e "$tmp_toc" ] 24 then 25 rm "$tmp_toc" 26 fi 17 27 } 18 export -f say warn die 28 29 trap on_exit EXIT 30 31 # pdfjam is a strong dependency. 32 hash pdfjam 2>/dev/null || 33 # pdftk is a soft dependency. 25 selon cette clé, du pdf d'entrée. Le tri est stable. 38 cat >&2 <<EOF 39 usage: $(basename "$0") [options...] <PDF-FILE> <KEYS-FILE> 40 41 Reorder the given PDF-FILE in the sorted order of the keys in KEYS-FILE and 42 write the resulting pdf to another PDF file. 43 44 KEYS-FILE is a list of "key page_start page_end" or "key page_start" or "key". 45 46 The sort on the keys is numeric and stable. 47 48 -h Display this message 49 -o Set the output file. Defaults to PDF-FILE with the ".reordered.pdf" 50 extension. 51 -1 Set the input PDF to be a one-sided scan (one key per page). 52 -2 Set the input PDF to be a two-sided scan (one key for two pages) (this is the default). 97 [[ -e "$pdf_file" ]] || die "$pdf_file does not exist." 98 [[ -f "$pdf_file" ]] || die "$pdf_file is not a file." 99 [[ -r "$pdf_file" ]] || die "$pdf_file is not readable." 100 101 [[ $(mimetype -L -b -M "$pdf_file") == "application/pdf" ]] || 102 badusage "$pdf_file does not seem to be a PDF file." 103 104 [[ -e "$keys_file" ]] || die "$keys_file does not exist." 105 [[ -r "$keys_file" ]] || die "$keys_file is not readable." 106 [[ -f "$keys_file" || -c "$keys_file" ]] || 107 die "$keys_file is not a file nor a character device." 108 109 [[ -e "$output_file" ]] && die "error: $output_file already exists" 110 111 # Find the number of pages in the PDF file using multiple fallbacks. 112 if hash pdftk 2>/dev/null; then 34 pdffile=$1 35 numPage=1 125 [[ -z "$num_pages" ]] && \ 126 die "error: failed to read the number of pages of $pdf_file." 127 128 129 # Reorder the PDF. 130 131 declare -A keys 132 declare -A keys_page_count 133 134 key="" 135 page_start=$((-pages_per_sheet)) 136 page_end=0 137 138 while read -r new_key new_page_start new_page_end 31 32 KEYS-FILE is a list of keys, one line per pdf page (or two pdf pages for 33 two-sided scan). The sort is numeric and stable. 44 KEYS-FILE is a list of "key page_start page_end" or "key page_start" or "key". 34 45 35 If KEYS-FILE is undefined, keys a read from stdin. 46 The sort on the keys is numeric and stable. 36 47 37 48 -h Display this message 38 49 -o Set the output file. Defaults to PDF-FILE with the ".reordered.pdf" 39 50 extension. 40 51 -1 Set the input PDF to be a one-sided scan (one key per page). 41 -2 Set the input PDF to be a two-sided scan (one key for two pages). 42 If neither -1 nor -2 is set, it will be guessed from the number of pages 43 and keys. 52 -2 Set the input PDF to be a two-sided scan (one key for two pages) (this is the default). 117 readonly numKeys 118 128 119 if ((pagesPerKey == 0)); then 120 if ((numKeys * 2 == numPages)); then 121 err "Assuming a two-sided scan, one key per sheet (two pages)."\ 122 "Use -1 option to force a one-sided scan." 123 pagesPerKey=2 124 else 125 pagesPerKey=1 129 # Reorder the PDF. 130 131 declare -A keys 132 declare -A keys_page_count 133 134 key="" 135 page_start=$((-pages_per_sheet)) J'avais commencé à lire le code il y a longtemps. J'ai voulu m'y remettre aujourd'hui mais certains de mes commentaires n'apparaissaient plus dans la review. En "publiant" la review ils sont apparus ici... mais je suis un peu paumé avec cet outil de review que je n'avais jamais utilisé avant.
Bref, pour en revenir sur le fond du code : si j'ai tant tarder avant de faire le moindre commentaire, c'est que la grosse boucle me fait peur. Je me demande si ça vaut la peine de faire une même boucle pour supporter les trois syntaxes "key[ page_start[ page_end]" ?
removed review request for @nrichard