mason/dhandler (.contact): Nudge index-scanning loop to focus on top level.
[tgal] / mason / dhandler
CommitLineData
6ac5dde2
MW
1%### -*-html-*-
2%###
3%### Main output for Trivial Gallery.
4%###
5%### (c) 2021 Mark Wooding
6%###
7%
8%###----- Licensing notice --------------------------------------------------
9%###
10%### This file is part of Trivial Gallery.
11%###
12%### Trivial Gallery is free software: you can redistribute it and/or modify
13%### it under the terms of the GNU Affero General Public License as
14%### published by the Free Software Foundation; either version 3 of the
15%### License, or (at your option) any later version.
16%###
17%### Trivial Gallery is distributed in the hope that it will be useful, but
18%### WITHOUT ANY WARRANTY; without even the implied warranty of
19%### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20%### Affero General Public License for more details.
21%###
22%### You should have received a copy of the GNU Affero General Public
23%### License along with Trivial Gallery. If not, see
24%### <https://www.gnu.org/licenses/>.
25%
26%###-------------------------------------------------------------------------
27<%def .html>\
28% $r->content_type("text/html; charset=\"utf-8\"");
29<!DOCTYPE html>
30<!--
31Trivial Gallery, copyright © 2021 Mark Wooding.
32Free software: you can redistribute it and/or modify it under the terms
33of the GNU Affero General Public License.
34-->
35<html>
36<head>
37 <meta name=viewport content="width=device-width initial-scale=1.0">
38 <script type="text/javascript" src="<% "$STATICURL/tgal.js" |u %>" defer></script>
39 <link rel=stylesheet type=text/css href="<% "$STATICURL/tgal.css" |u %>">
40<% $head %>\
41 <title><% $title %></title>
42</head>
43<body>
44<% $m->content %>
45</body>
46</html>\
47%
48<%args>
49 $title
50 $head => ""
51</%args>
52</%def>
53%
54%###-------------------------------------------------------------------------
55<%def .not-found>\
56<&| .html, title => "Not found" &>
57<h1>Not found</h1>
58Failed to find &lsquo;<% $path |h %>&rsquo;.
59</&>
60% return 404;
61%
62<%args>
63 $path
64</%args>
65</%def>
66%
67%###-------------------------------------------------------------------------
68<%def .contact>\
69<%perl>
458a80b2
MW
70 unless ($r->path_info =~ m!/$!)
71 { $m->redirect(join_paths($SCRIPTURL, $path) . "/"); }
72
6ac5dde2
MW
73 my $real = join_paths $IMGROOT, $path;
74 my $url = join_paths $SCRIPTURL, $path;
75 my ($dd, $ff, $ii) = listdir $real;
76 my $links = "";
77 my $uplink;
78 if ($path eq "" || $path eq "/") { $uplink = undef; }
79 else {
80 ($uplink = $path) =~ s![^/]*/$!!;
81 $links .= sprintf " <link rel=up href=\"%s\">\n",
82 urlencode "$SCRIPTURL/$uplink";
83 }
84 (my $nosl = $path) =~ s!/$!!;
6e749fab 85
f4b90914 86 my $size = "medthumb";
6e749fab
MW
87 my %tn;
88 for my $f (@$ff)
ccaab4e8 89 { $tn{$f} = TrivGal::Image->new($path . $f->name)->scale($size); }
6e749fab 90 for my $d (@$dd) {
2fe5383f 91 my $p = join_paths $path, $d->name;
b0e94d8a
MW
92 my ($ddd, $fff, $iii);
93 ($ddd, $fff, $iii) = listdir join_paths $IMGROOT, $p;
2fe5383f 94 DIR: for (;;) {
2fe5383f
MW
95 if (defined $iii) {
96 my $index = join_paths $p, $iii->name;
ccaab4e8 97 $tn{$d} = TrivGal::Image->new($index)->scale($size);
2fe5383f
MW
98 last DIR;
99 }
100 if (!@$ddd) { $tn{$d} = undef; last DIR; }
101 $p = join_paths $p, $ddd->[0]->name;
b0e94d8a 102 ($ddd, $fff, $iii) = listdir join_paths $IMGROOT, $p;
2fe5383f 103 }
6e749fab 104 }
6ac5dde2
MW
105</%perl>
106%
3d7d20f8
MW
107<&| .html, title =>
108 "Folder " . $m->interp->apply_escapes($nosl || "[top]", "h"),
6ac5dde2
MW
109 head => $links &>
110<& .breadcrumbs, what => "Folder", path => $path &>
111%
112% my $note = contents "$IMGROOT/$path/.tgal-note.html";
113% if (defined $note) {
114<div class=note>
115<% $note %>
116</div>
117% }
118%
119% if (@$dd) {
120<h2>Subfolders</h2>
6e749fab 121<div class="gallery <% $size %>">
f4b90914 122% for my $d (@$dd) {
24f4eac3 123 <& .thumbnail, target => $d->name, comment => $d->comment,
ccaab4e8 124 tn => $tn{$d}, size => $size,
dfdd1964 125 caption => $m->interp->apply_escapes($d->name, "h") &>\
6e749fab 126% }
f4b90914 127</div>
6ac5dde2
MW
128% }
129%
130% if (@$ff) {
131<h2>Images</h2>
6e749fab 132<div class="gallery <% $size %>">
f4b90914 133% for my $f (@$ff) {
dfdd1964 134 <& .thumbnail, target => $f->name, comment => $f->comment,
ccaab4e8 135 tn => $tn{$f}, size => $size,
dfdd1964 136 caption => $m->interp->apply_escapes($f->name, "h") &>\
6e749fab 137% }
f4b90914 138</div>
6ac5dde2
MW
139% }
140%
141<div class=fill></div>
142<& .footer, path => $path &>
143</&>
144%
145<%args>
146 $path
147</%args>
148</%def>
149%
150%###-------------------------------------------------------------------------
151<%def .image>\
152<%perl>
153 my ($dir, $base, $ext) = split_path $path;
9601c4bb 154
bfc5bfe6
MW
155 if (defined $scale) {
156 my $img = TrivGal::Image->new($path);
784bdf8f 157 $m->redirect($img->scale($scale, 1));
bfc5bfe6 158 }
9601c4bb 159
bfc5bfe6 160 my $real = join_paths $IMGROOT, $path;
6ac5dde2
MW
161 my $url = join_paths $IMGURL, $path;
162 my $realdir = join_paths $IMGROOT, $dir;
163 my $urldir = join_paths $SCRIPTURL, $dir;
164 my ($dd, $ff, $ii) = listdir $realdir;
bfc5bfe6
MW
165 my @thumbsz = qw{smallthumb medthumb bigthumb};
166 my %tn;
167 my $vw;
6ac5dde2
MW
168
169 my $fi = undef;
bfc5bfe6
MW
170 FILE: for (my $i = 0; $i < @$ff; $i++) {
171 my $f = $ff->[$i];
172 my $img = TrivGal::Image->new(join_paths $dir, $f->name);
173 for my $sz (@thumbsz) { $tn{$f->name}{$sz} = $img->scale($sz); }
174 if ($ff->[$i]->name eq "$base$ext")
175 { $fi = $i; $vw = $img->scale("view"); }
176 }
6ac5dde2
MW
177 defined $fi or die "image not found in its folder?";
178 my $this = $ff->[$fi];
179
180 my %link;
181 $link{up} = "";
182 if ($fi != 0) {
183 $link{first} = $ff->[0]->name;
184 $link{prev} = $ff->[$fi - 1]->name;
185 }
186 if ($fi != @$ff - 1) {
187 $link{last} = $ff->[-1]->name;
188 $link{next} = $ff->[$fi + 1]->name;
189 }
190
191 my $links = "";
192 my $pre =
193 urlencode join_paths $SCRIPTURL, $dir;
194 for my $rel (qw{up first prev next last}) {
195 exists $link{$rel} and
196 $links .= sprintf " <link rel=%s href=\"%s\">\n",
197 $rel, urlencode "$pre/$link{$rel}";
198 }
199</%perl>
200%
201<&| .html, title => "Image " . $m->interp->apply_escapes($path, "h"),
202 head => $links &>
203<& .breadcrumbs, what => "Image", path => $path &>
204% if ($this->comment) {
205 <div class=comment>
206 <p><% $this->comment %>
207 </div>
208% }
209%
210<div class=viewnav>
211% if ($link{prev}) {
212 <div class=prev><a class=prev href="<% "$pre/$link{prev}" |u %>">&lsaquo;</a></div>
213% }
214 <a class=view href="<% $url |h %>">
215 <img src="<% $vw |h %>">
216 </a>
217% if ($link{next}) {
218 <div class=next><a class=next href="<% "$pre/$link{next}" |u %>">&rsaquo;</a></div>
219% }
220</div>
221%
1408b7a2 222% for my $size (qw{smallthumb medthumb bigthumb}) {
6e749fab
MW
223<div class="thumbstrip <% $size %>">
224% for my $f (@$ff) {
ccaab4e8 225 <& .thumbnail, target => $f->name,
bfc5bfe6 226 tn => $tn{$f->name}{$size}, size => $size,
6ac5dde2 227 caption => $m->interp->apply_escapes($f->name, "h"),
62ee1c09 228 focus => $f eq $this &>\
6e749fab 229% }
6ac5dde2 230</div>
6e749fab 231% }
6ac5dde2
MW
232<& .footer, path => $dir &>
233</&>
234%
235<%args>
236 $path
9601c4bb 237 $scale => undef
6ac5dde2
MW
238</%args>
239</%def>
240%
241%###-------------------------------------------------------------------------
242<%def .breadcrumbs>\
243% $path =~ s!/$!!;
244% my @p = split m!/!, $path;
245% my $pp = "";
246% my $prev = undef;
247<h1><% $what %> \
248% if (!@p) {
249[top]
250% } else {
251<a href="<% $SCRIPTURL |u %>/">[top]</a>&thinsp;/&thinsp;\
252% STEP: for my $p (@p) {
253% if (defined $prev) {
254% $pp .= "$prev/";
255<a href="<% join_paths($SCRIPTURL, $pp) |u %>/">\
256<% $prev %></a>&thinsp;/&thinsp;\
257% }
258% $prev = $p;
259% }
260<% $prev %>\
261% }
262</h1>
263<%args>
264 $what
265 $path
266</%args>
267</%def>
268%
269%###-------------------------------------------------------------------------
270<%def .thumbnail>\
ccaab4e8 271% $tn //= "$STATICURL/folder.svg";
6ac5dde2 272% if ($focus) {
1408b7a2 273 <figure class="thumb focusthumb <% $size %>">
1b3d6791 274 <img class="thumb <% $size %>" load=lazy src="<% $tn |h %>">
83bfcbc7 275 <figcaption><span class=name><% $caption %></span></figcaption>
6ac5dde2 276% } else {
dfdd1964 277 <figure class="thumb <% $size %>">
56340849 278 <a class=thumb href="<% $target |u %>">
1b3d6791 279 <img class="thumb <% $size %>" load=lazy src="<% $tn |h %>">
83bfcbc7 280 <figcaption>
6ac5dde2
MW
281 <span class=name><% $caption %></span>
282% if (defined $comment) {
283 <span class=comment><% $comment %></span>
284% }
83bfcbc7 285 </figcaption>
6ac5dde2
MW
286 </a>
287% }
83bfcbc7 288 </figure>
6ac5dde2
MW
289%
290<%args>
291 $target
ccaab4e8 292 $tn
dfdd1964 293 $size
6ac5dde2
MW
294 $caption
295 $comment => undef
296 $focus => 0
297</%args>
298</%def>
299%
300%###-------------------------------------------------------------------------
301<%def .footer>\
302<%perl>
303</%perl>
304<div class=footer>
305 <div class=footitem>
306 <a href="https://www.gnu.org/licenses/agpl-3.0.en.html"><img class=licence src="<% "$STATICURL/agpl.png" |u %>"></a>
307 Trivial Gallery, copyright &copy; 2021 Mark Wooding.
308 Free software: you can modify it and/or redistribute it under the
309 terms of the
a54108de 310 <a rel=license href="https://www.gnu.org/licenses/agpl-3.0.en.html">GNU Affero
6ac5dde2 311 General Public License version 3</a>.
a38a867d 312 Browse or download the <a href="<% $SRCURL %>">source code</a>.
6ac5dde2
MW
313 </div>
314% my $user =
315% find_covering_file $IMGROOT, $path, ".tgal-footer.html";
316% if (defined $user) {
317 <div class=footitem>
318<% $user %>
319 </div>
320% }
321</div>
322<%args>
323 $path
324</%args>
325</%def>
326%
327%###-------------------------------------------------------------------------
328<%once>
329 use autodie;
6ac5dde2
MW
330 use File::stat;
331
332 use TrivGal;
333</%once>
334%
335<%init>
336 TrivGal->init;
337
338 my $path = $m->dhandler_arg;
339 my $st = stat "$IMGROOT/$path";
340 my $comp;
341 if (!$st) { $comp = ".not-found"; }
342 elsif (-d $st) { $comp = ".contact"; }
343 elsif (-f $st) { $comp = ".image"; }
344 else { $comp = ".not-found"; }
ecf3c7a8 345 $r->header_out("X-AGPL-Source" => $SRCURL);
6abe8e59 346 $m->comp($comp, path => $path, %ARGS);
6ac5dde2
MW
347</%init>
348%
349%###----- That's all, folks -------------------------------------------------