summary refs log tree commit diff stats
path: root/lib/Crater/Gallery.rakumod
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Crater/Gallery.rakumod')
-rw-r--r--lib/Crater/Gallery.rakumod51
1 files changed, 36 insertions, 15 deletions
diff --git a/lib/Crater/Gallery.rakumod b/lib/Crater/Gallery.rakumod
index 19b0d69..448593f 100644
--- a/lib/Crater/Gallery.rakumod
+++ b/lib/Crater/Gallery.rakumod
@@ -1,25 +1,46 @@
 class Crater::Gallery {
     has IO $.directory is required;
+    has Str $!title;
+
+    submethod TWEAK() {
+        my $title-file = $!directory.add(".crater/title");
+        $!title = $title-file.slurp.chomp if $title-file.f;
+    }
+
+    #| Accessor for $!title.
+    method title() { $!title }
 
     method list() {
         my @gallery;
-        for $!directory.dir.sort(*.modified) {
-            if .IO.d {
+        my @paths = $!directory.dir;
+
+        with $!title {
+            push @gallery, %( :type<heading>, :text($_) );
+        }
+
+        # Add directories on top.
+        for @paths.grep(*.d) {
+            push @gallery, %( :type<directory>,
+                              :text($_.relative($!directory)) );
+        }
 
-            } elsif .IO.f {
-                my Str $ext = .extension.lc;
-                if $ext eq "jpg"|"png" {
-                    push @gallery, %(
-                        :type<img>, :src($_.relative($!directory)),
-                        :alt($_)
-                    );
-                } elsif $ext eq "0" {
-                    push @gallery, %( :type<heading>, :text($_.slurp) );
-                } elsif $ext eq "txt" {
-                    push @gallery, %( :type<text>, :text($_.slurp) );
-                } else {
-                    warn "Unhandled file :$_";
+        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";
                 }
+                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;