summaryrefslogtreecommitdiff
path: root/katana.raku
blob: c7e563082183140dc893cd378edce56b37b28c98 (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
72
#!/usr/bin/env raku

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

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/2, '-blur', $bg-blur, $!source, $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 make-mr-proper(:$dist-dir) {
  ❱ |<rm -r -f>, $dist-dir if $dist-dir.IO.d;
}

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

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: { Image.new: source => $_, :$dist-dir };
  @images = @images.pick: * if $randomize;

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

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