254 lines
5.1 KiB
Plaintext
254 lines
5.1 KiB
Plaintext
use moi/util/format
|
||
use moi/util/pdf
|
||
use re
|
||
use str
|
||
|
||
fn -must-pdf {|file|
|
||
if (not (pdf:is-pdf $file)) {
|
||
fail (printf '%s n’est pas un fichier pdf' $file)
|
||
}
|
||
}
|
||
|
||
fn -must-exist {|file|
|
||
if (pdf:not-exist $file) {
|
||
fail (printf '%s n’existe pas' $file)
|
||
}
|
||
}
|
||
|
||
fn -must-not-exist {|file|
|
||
if (pdf:exist $file) {
|
||
fail (printf '%s existe déjà' $file)
|
||
}
|
||
}
|
||
|
||
fn -must-pdf-exist {|file|
|
||
-must-pdf $file
|
||
-must-exist $file
|
||
}
|
||
|
||
fn -must-pdf-not-exist {|file|
|
||
-must-pdf $file
|
||
-must-not-exist $file
|
||
}
|
||
|
||
fn -must-valid {|v cond|
|
||
if (not ($cond $v)) {
|
||
fail (printf '%s: paramètre invalide' $v)
|
||
}
|
||
}
|
||
|
||
fn -out {|in suffix|
|
||
var out = (str:trim-suffix $in .pdf)
|
||
str:join '' [ $out $suffix ]
|
||
}
|
||
|
||
fn help {|@args|
|
||
cat $E:HOME/.config/elvish/aliases/pdf.help
|
||
}
|
||
|
||
fn decrypt {|@args|
|
||
var l = (count $args)
|
||
if (or (< $l 1) (> $l 3)) {
|
||
fail 'decrypt doit contenir entre 1 et 3 paramètres'
|
||
}
|
||
var in = $args[0]
|
||
var out = (-out $in _decrypted.pdf)
|
||
var passwd = []
|
||
-must-pdf-exist $in
|
||
if (> $l 1) {
|
||
set @passwd = $args[1]
|
||
if (> $l 2) {
|
||
set out = $args[2]
|
||
}
|
||
}
|
||
-must-pdf-not-exist $out
|
||
pdf:decrypt &out=$out $in $@passwd
|
||
}
|
||
|
||
fn rotate {|@args|
|
||
var l = (- (count $args) 1)
|
||
if (< $l 1) {
|
||
fail 'rotate doit contenir au moins 1 paramètres'
|
||
}
|
||
var in = $args[0]
|
||
var out = (-out $in _rotated.pdf)
|
||
if (pdf:is-pdf $args[$l]) {
|
||
set @args out = (all $args[1..])
|
||
}
|
||
-must-pdf-exist $in
|
||
-must-pdf-not-exist $out
|
||
each {|e|
|
||
-must-valid $e $pdf:is-rotate-selection~
|
||
} $args
|
||
pdf:rotate &keep=$true &out=$out $in $@args
|
||
}
|
||
|
||
fn merge {|@args|
|
||
var l = (- (count $args) 1)
|
||
if (< $l 1) {
|
||
fail 'merge doit contenir au moins 2 paramètres'
|
||
}
|
||
var @selection out = $@args
|
||
-must-pdf-not-exist $out
|
||
var has-in = $false
|
||
each {|e|
|
||
if (pdf:is-pdf $e) {
|
||
-must-exist $e
|
||
set has-in = $true
|
||
} else {
|
||
if (not $has-in) {
|
||
fail 'Une sélection doit être précédée d’un fichier d’entrée'
|
||
}
|
||
-must-valid $e $pdf:is-selection~
|
||
}
|
||
} $selection
|
||
pdf:cat &out=$out $@selection
|
||
}
|
||
|
||
fn unmerge {|@args|
|
||
var l = (- (count $args) 1)
|
||
if (< $l 1) {
|
||
fail 'unmerge doit contenir au moins 2 paramètres'
|
||
}
|
||
var in = $args[0]
|
||
var out = (-out $in _unmerged)
|
||
if (not (pdf:is-selection $args[$l])) {
|
||
set @args out = (all $args[1..])
|
||
}
|
||
-must-pdf-exist $in
|
||
-must-not-exist $out
|
||
each {|e|
|
||
-must-valid $e $pdf:is-selection~
|
||
} $args
|
||
pdf:uncat $in $out $@args
|
||
}
|
||
|
||
fn split {|@args|
|
||
var l = (- (count $args) 1)
|
||
if (< $l 1) {
|
||
fail 'split doit contenir au moins 2 paramètres'
|
||
}
|
||
var out = $args[$l]
|
||
set args = $args[..$l]
|
||
-must-not-exist $out
|
||
var s = 1
|
||
if (pdf:is-number $args[0]) {
|
||
set s @args = $@args
|
||
}
|
||
var t = (pdf:-t)
|
||
try {
|
||
pdf:cat &out=$t $@args
|
||
pdf:split &size=$s $t $out
|
||
} finally {
|
||
rm -f $t
|
||
}
|
||
}
|
||
|
||
fn split-at {|@args|
|
||
var l = (count $args)
|
||
if (!= $l 3) {
|
||
fail 'split-at doit contenir 3 paramètres'
|
||
}
|
||
var rg in out = $@args
|
||
-must-pdf-exist $in
|
||
-must-not-exist $out
|
||
-must-valid $rg {|e| re:match '^\d+(,\d+)*$' $e }
|
||
|
||
var b sel = 1 []
|
||
str:split ',' $rg | order | each {|s|
|
||
var e = (- $s 1)
|
||
if (== $b $e) {
|
||
set @sel = $@sel $b
|
||
set b = $s
|
||
} elif (< $b $e) {
|
||
set @sel = $@sel (printf '%d-%d' $b $e)
|
||
set b = $s
|
||
}
|
||
}
|
||
set @sel = $@sel (printf '%d-z' $b)
|
||
unmerge $in $@sel $out
|
||
}
|
||
|
||
fn zip {|@args|
|
||
var l = (- (count $args) 1)
|
||
if (< $l 1) {
|
||
fail 'zip doit contenir au moins 2 paramètres'
|
||
}
|
||
var @selection out = $@args
|
||
-must-pdf-not-exist $out
|
||
var has-in = $false
|
||
each {|e|
|
||
if (pdf:is-pdf $e) {
|
||
-must-exist $e
|
||
set has-in = $true
|
||
} else {
|
||
if (not $has-in) {
|
||
fail 'Une sélection doit être précédée d’un fichier d’entrée'
|
||
}
|
||
-must-valid $e $pdf:is-selection~
|
||
}
|
||
} $selection
|
||
pdf:cat &collate=$true &out=$out $@selection
|
||
}
|
||
|
||
fn unzip {|@args|
|
||
var l = (- (count $args) 1)
|
||
if (< $l 1) {
|
||
fail 'unzip doit contenir au moins 2 paramètres'
|
||
}
|
||
var in @sel out = $@args
|
||
var mod = 2
|
||
if (pdf:is-number $in) {
|
||
set mod = $in
|
||
if (eq (count $sel) 0) {
|
||
fail 'unzip doit contenir au moins 3 paramètres'
|
||
}
|
||
set in @sel = $@sel
|
||
}
|
||
-must-not-exist $out
|
||
var t = (pdf:-t)
|
||
try {
|
||
pdf:cat &out=$t $in $@sel
|
||
var n = (pdf:nb-pages $t)
|
||
mkdir $out
|
||
range 1 (+ 1 $mod) | each {|m|
|
||
var sel = (range 1 (+ $n 1) | each {|i|
|
||
var s = (% $i $mod)
|
||
if (== $s 0) {
|
||
set s = $mod
|
||
}
|
||
if (== $s $m) {
|
||
put $i
|
||
}
|
||
} | each $to-string~ | str:join ',')
|
||
pdf:cat &out=(printf '%s/%d.pdf' $out $m) $t $sel
|
||
}
|
||
} finally {
|
||
rm -f $t
|
||
}
|
||
}
|
||
|
||
edit:add-var pdf~ {|action @args|
|
||
var actions = [
|
||
&help=$help~
|
||
&decrypt=$decrypt~
|
||
&rotate=$rotate~
|
||
&merge=$merge~
|
||
&cat=$merge~
|
||
&unmerge=$unmerge~
|
||
&uncat=$unmerge~
|
||
&split=$split~
|
||
&split-at=$split-at~
|
||
&cut=$split-at~
|
||
&zip=$zip~
|
||
&unzip=$unzip~
|
||
#&info=$info~
|
||
#&list=$list~
|
||
#&extract=$extract~
|
||
]
|
||
if (not (has-key $actions $action)) {
|
||
fail (printf '%s: action inconnue' $action)
|
||
}
|
||
$actions[$action] $@args
|
||
}
|