From dcceb30788af8433e8184ab15fe8833f5539145b Mon Sep 17 00:00:00 2001 From: Andinus Date: Sat, 11 Jun 2022 21:06:46 +0530 Subject: Gallery: Document code, add title fallback --- lib/Crater/Gallery.rakumod | 56 ++++++++++++++++++++++----------------- lib/Crater/Routes/Gallery.rakumod | 2 +- 2 files changed, 32 insertions(+), 26 deletions(-) diff --git a/lib/Crater/Gallery.rakumod b/lib/Crater/Gallery.rakumod index 4fdfac8..fdb8284 100644 --- a/lib/Crater/Gallery.rakumod +++ b/lib/Crater/Gallery.rakumod @@ -3,6 +3,7 @@ class Crater::Gallery { has Str $!title; submethod TWEAK() { + # Get title from file if exists. my $title-file = $!directory.add(".crater/title"); $!title = $title-file.slurp.chomp if $title-file.f; } @@ -13,43 +14,48 @@ class Crater::Gallery { method list(:@sub-dir) { # This will be considered an attempt to attack. There is no # reason to check '.' I belive. - if @sub-dir.grep('.'|'..').elems { - die "[!!!] @sub-dir contains '..'/'.'"; - } + die "[!!!] @sub-dir contains '..'/'.'" if @sub-dir.grep('.'|'..'); - my @gallery; + # Serve the subdirectory if passed. my @paths = @sub-dir ?? $!directory.add(@sub-dir.join("/")).dir !! $!directory.dir; - with $!title { - push @gallery, %( :type, :text($_) ); - } + # Gallery holds all the elements that will be shown. + my @gallery; + @gallery.push(%( :type, :text($_) )) with $!title; # Add directories on top. for @paths.grep(*.d).sort { next if .ends-with(".crater"); - push @gallery, %( :type, - :text($_.relative($!directory)) ); + push @gallery, %(:type, :text($_.relative($!directory))); } - for @paths.grep(*.f).sort(*.modified) { - my Str $ext = .extension.lc; - # For images get the original if thumbnail doesn't exist, - # otherwise use the thumbnail. - if $ext eq "jpg"|"png" { - my $rel = $_.relative($!directory); - my $alt = $rel; - unless $!directory.add(".crater/thumbnails").add($rel).f { - $rel ~= "?original"; + # Adding supported file types. + for @paths.grep(*.f).sort(*.modified) -> $f { + my $rel = $f.relative($!directory); + + given $f.extension.lc { + when 'jpg'|'png' { + my $thumb = $!directory.add(".crater/thumbnails").add($rel); + + # For images get the original if thumbnail doesn't + # exist, otherwise use the thumbnail. + push @gallery, %( + :type, + :src($thumb.f ?? $rel !! "{$rel}?original"), + alt => $rel + ); + } + when '0' { + push @gallery, %(:type, :text($f.slurp.chomp)); + } + when 'txt' { + push @gallery, %(:type, :text($f.slurp.chomp)); + } + default { + note "Unhandled file: $f"; } - push @gallery, %( :type, :src($rel), :$alt ); - } elsif $ext eq "0" { - push @gallery, %( :type, :text($_.slurp.chomp) ); - } elsif $ext eq "txt" { - push @gallery, %( :type, :text($_.slurp.chomp) ); - } else { - note "Unhandled file: $_"; } } return @gallery; diff --git a/lib/Crater/Routes/Gallery.rakumod b/lib/Crater/Routes/Gallery.rakumod index ede5010..ebf40b4 100644 --- a/lib/Crater/Routes/Gallery.rakumod +++ b/lib/Crater/Routes/Gallery.rakumod @@ -30,7 +30,7 @@ sub gallery-routes( template 'gallery.crotmp', { gallery => $gallery.list(sub-dir => @path), - title => $gallery.title(), + title => $gallery.title() // "Gallery", nav => @nav, show-nav => @path.elems ?? True !! False }; -- cgit 1.4.1-2-gfad0