summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorAndinus <andinus@nand.sh>2022-06-11 21:06:46 +0530
committerAndinus <andinus@nand.sh>2022-06-11 21:06:46 +0530
commitdcceb30788af8433e8184ab15fe8833f5539145b (patch)
tree2bcd3ed110096ab0cb06c981064b45f52a3c469c /lib
parent02f8885f05f09dcb57a358fcd3440eaf679c8c02 (diff)
downloadcrater-dcceb30788af8433e8184ab15fe8833f5539145b.tar.gz
Gallery: Document code, add title fallback
Diffstat (limited to 'lib')
-rw-r--r--lib/Crater/Gallery.rakumod56
-rw-r--r--lib/Crater/Routes/Gallery.rakumod2
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<heading>, :text($_) );
-        }
+        # Gallery holds all the elements that will be shown.
+        my @gallery;
+        @gallery.push(%( :type<heading>, :text($_) )) with $!title;
 
         # Add directories on top.
         for @paths.grep(*.d).sort {
             next if .ends-with(".crater");
-            push @gallery, %( :type<directory>,
-                              :text($_.relative($!directory)) );
+            push @gallery, %(:type<directory>, :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<img>,
+                        :src($thumb.f ?? $rel !! "{$rel}?original"),
+                        alt => $rel
+                    );
+                }
+                when '0' {
+                    push @gallery, %(:type<heading>, :text($f.slurp.chomp));
+                }
+                when 'txt' {
+                    push @gallery, %(:type<text>, :text($f.slurp.chomp));
+                }
+                default {
+                    note "Unhandled file: $f";
                 }
-                push @gallery, %( :type<img>, :src($rel), :$alt );
-            } elsif $ext eq "0" {
-                push @gallery, %( :type<heading>, :text($_.slurp.chomp) );
-            } elsif $ext eq "txt" {
-                push @gallery, %( :type<text>, :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
             };