83 lines
1.7 KiB
Plaintext
83 lines
1.7 KiB
Plaintext
edit:add-var pdf2grey~ [@argv]{
|
||
use re
|
||
use str
|
||
local: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) {
|
||
local:e @argv = $@argv
|
||
if (or (eq $e '-h') (eq $e '--help')) {
|
||
_help
|
||
return
|
||
} elif (eq $e -n) {
|
||
m[nb] = $true
|
||
if (== (count $e) 0) {
|
||
echo 'Il manque des arguments après -s'
|
||
_help
|
||
return
|
||
} elif (is_int $argv[0]) {
|
||
e @argv = $@argv
|
||
if (is_split $e) {
|
||
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)) {
|
||
m[input] = $e
|
||
} else {
|
||
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
|
||
}
|
||
|
||
local:params = [-colorspace gray]
|
||
if $m[nb] {
|
||
@params = (all $params) -threshold $m[split]'%'
|
||
}
|
||
@params = $@params $m[input] $m[output]
|
||
echo 'Commande: convert '(str:join ' ' $params)
|
||
convert $@params
|
||
}
|