83 lines
1.8 KiB
Plaintext
83 lines
1.8 KiB
Plaintext
edit:add-var pdf2grey~ {|@argv|
|
||
use re
|
||
use str
|
||
var m = [
|
||
&nb=$false
|
||
&split=50
|
||
]
|
||
|
||
fn _help {||
|
||
echo 'Usage: pdf2gray [-hn (<n>)] <input>.pdf <output>.pdf'
|
||
echo ''
|
||
echo 'Options:'
|
||
echo ' -h Affiche cette aide'
|
||
echo ' -n [0-100] Convertit en noir et blanc. Si un nombre est spécifié, indique le pourcentage de coupure (par défaut: 50)'
|
||
}
|
||
|
||
fn is_pdf {|e|
|
||
re:match '.*\.pdf$' $e
|
||
}
|
||
|
||
fn is_int {|e|
|
||
re:match '^\d+$' $e
|
||
}
|
||
|
||
fn is_split {|e|
|
||
and (>= $e 0) (<= $e 100)
|
||
}
|
||
|
||
while (> (count $argv) 0) {
|
||
var e @argv = $@argv
|
||
if (or (eq $e '-h') (eq $e '--help')) {
|
||
_help
|
||
return
|
||
} elif (eq $e -n) {
|
||
set m[nb] = $true
|
||
if (== (count $e) 0) {
|
||
echo 'Il manque des arguments après -s'
|
||
_help
|
||
return
|
||
} elif (is_int $argv[0]) {
|
||
set e @argv = $@argv
|
||
if (is_split $e) {
|
||
set m[split] = $e
|
||
} else {
|
||
echo 'L’argument (optionnel) après -s doit être compris entre 0 et 100'
|
||
_help
|
||
return
|
||
}
|
||
}
|
||
} elif (not (is_pdf $e)) {
|
||
echo 'Argument non reconnu: '$e
|
||
_help
|
||
return
|
||
} elif (not (has-key $m input)) {
|
||
set m[input] = $e
|
||
} else {
|
||
set m[output] = $e
|
||
if (> (count $argv) 0) {
|
||
echo 'Trop d’arguments: '$@argv
|
||
_help
|
||
return
|
||
}
|
||
}
|
||
}
|
||
if (not (has-key $m input)) {
|
||
echo 'Manque le input'
|
||
_help
|
||
return
|
||
} elif (not (has-key $m output)) {
|
||
echo 'Manque le output'
|
||
_help
|
||
return
|
||
}
|
||
|
||
var params = [-colorspace gray]
|
||
if $m[nb] {
|
||
set @params = (all $params) -threshold $m[split]'%'
|
||
}
|
||
set @params = $@params $m[input] $m[output]
|
||
echo 'Commande: convert '(str:join ' ' $params)
|
||
convert $@params
|
||
}
|