summaryrefslogtreecommitdiff
path: root/katana.raku
blob: 6d5b1047fe810e409fe1cc482c7196c615fb45e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env raku

sub prefix:<❱>(*@args) {
  say "❱ {@args}";
  my $proc = run @args, :out, :err;
  .say if .chars > 0 for $proc.out.slurp(:close), $proc.err.slurp(:close);
}

sub prefix:<⁉>(*@args) { ❱ @args unless @args[*-1].IO.f }

class Template {
  has Str %!vars;
  has Str $!frame = 'frame.html';
}

class Image {
  has Str $!filename;
  has Str $!source;
  has Str $!dist-dir;

  submethod BUILD(IO::Path :$source, Str :$dist-dir) {
    $!filename = $source.basename;
    $!source = $source.path;
    $!dist-dir = $dist-dir;
  }

  method generate(Int :$thumb-geometry, Str :$bg-blur) {
    my $thumb = "$!dist-dir/thumb/{$!filename}";
    ⁉ [|<convert -auto-orient -geometry>, $thumb-geometry, $!source, $thumb];

    my $blur = "$!dist-dir/blur/{$!filename}";
    ⁉ [|<convert -flip -geometry>, $thumb-geometry/4, '-blur', $bg-blur, $thumb, $blur];

    my $large = "$!dist-dir/large/{$!filename}";
    ⁉ ['cp', $!source, $large];
  }

  method thumb_tag { "<img class='thumb' src='./thumb/{$!filename}' />" }
  method large_tag { "<img class='large' src='./large/{$!filename}' />" }
}

sub ensure-directories(Str :$dist-dir) {
  mkdir $dist-dir unless $dist-dir.IO.d;
  mkdir $_ unless .IO.d for $dist-dir <<~>> </large /blur /thumb>;
}

sub make-mr-proper(Str :$dist-dir) {
  ❱  ['rm', '-rf', $dist-dir] if $dist-dir.IO.d;
}

multi MAIN(
  Bool :$mr-proper,           #= Clean output dir
  Str :$in-dir = './in',      #= Input dir
  Str :$dist-dir = './dist',  #= Output dir
  Int :$thumb-geometry = 800, #= Thumbnail geometry
  Str :$bg-blur = '0x8',      #= Background blur factor
  Bool :$randomize = True,    #= Randomize order of images
  Str :$title = 'Yay',        #= Album title
) {

  my @images = dir($in-dir, test => { "$in-dir/$_".IO.f })
              .map: { new Image: source => $_, :$dist-dir };
  @images = @images.pick: * if $randomize;
  say "Found {@images.elems} images";

  make-mr-proper :$dist-dir if $mr-proper;
  ensure-directories :$dist-dir;

  @images.hyper.map: { .generate: :$thumb-geometry, :$bg-blur };
}