word edit: null priority to inherit from parent
[sheet.git] / tools / mkimgthumb
index d7cfa15926915936bf99292ef1b99c8db475a866..e2b974dcf09e35ae7fdd27c3bdc3fbf7631880ee 100755 (executable)
@@ -2,12 +2,30 @@
 use 5.014;
 use warnings;
 
+my $failcount = 0;
+
 for my $src (@ARGV) {
-       my ($name, @cmds) = split /:/, $src =~ s/\.jpg$//r;
+       my ($name, @cmds) = split /:(?<!\\:)/, $src =~ s/\.(\w+)\z//r;
+       my $ext = $1 // '*';
+       next if $name =~ m/\./;
+       unless (-e $src) {
+               ($src) = grep {-e} glob qq<"$name"{,:*}.$ext> or next;
+       }
+       s/\\(.)/$1/g for @cmds;
        say $name;
-       unshift @cmds, -gravity => 'northwest';
+
+       if (@cmds and $cmds[0] =~ /^\d/) {
+               # crop shorthand from initial dimension argument
+               my @crop = split /\D/, shift @cmds;
+               unshift @cmds, -gravity => 'southeast', -chop => "$crop[2]%x$crop[3]%"
+                       if @crop > 2;
+               unshift @cmds, -chop => "$crop[0]%x$crop[1]%";
+       }
+       unshift @cmds, -gravity => 'northwest' if @cmds;
        push @cmds, -resize => '300x200^', -gravity => 'north', -extent => '300x200';
        push @cmds, '-strip', -quality => '60%';
-       system convert => @cmds, $src => "../$name.jpg";
+       system(convert => @cmds, $src => "../$name.jpg") == 0
+               or $failcount += warn "error creating $name.jpg from $src\n";
 }
 
+exit $failcount;