mason/dhandler: Use the correct attribute to encourage lazy loading.
[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">
988a1d43
MW
38 <script type="text/javascript" src="<% "$STATICURL/tgal.js" |hu %>" defer></script>
39 <link rel=stylesheet type=text/css href="<% "$STATICURL/tgal.css" |hu %>">
6ac5dde2
MW
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 87 my %tn;
27ad11a2 88 my (%nd, %nf);
6e749fab 89 for my $f (@$ff)
ccaab4e8 90 { $tn{$f} = TrivGal::Image->new($path . $f->name)->scale($size); }
6e749fab 91 for my $d (@$dd) {
2fe5383f 92 my $p = join_paths $path, $d->name;
b0e94d8a
MW
93 my ($ddd, $fff, $iii);
94 ($ddd, $fff, $iii) = listdir join_paths $IMGROOT, $p;
27ad11a2 95 $nd{$d} = @$ddd; $nf{$d} = @$fff;
2fe5383f 96 DIR: for (;;) {
2fe5383f
MW
97 if (defined $iii) {
98 my $index = join_paths $p, $iii->name;
ccaab4e8 99 $tn{$d} = TrivGal::Image->new($index)->scale($size);
2fe5383f
MW
100 last DIR;
101 }
102 if (!@$ddd) { $tn{$d} = undef; last DIR; }
103 $p = join_paths $p, $ddd->[0]->name;
b0e94d8a 104 ($ddd, $fff, $iii) = listdir join_paths $IMGROOT, $p;
2fe5383f 105 }
6e749fab 106 }
6ac5dde2
MW
107</%perl>
108%
3d7d20f8
MW
109<&| .html, title =>
110 "Folder " . $m->interp->apply_escapes($nosl || "[top]", "h"),
6ac5dde2
MW
111 head => $links &>
112<& .breadcrumbs, what => "Folder", path => $path &>
113%
114% my $note = contents "$IMGROOT/$path/.tgal-note.html";
115% if (defined $note) {
116<div class=note>
117<% $note %>
118</div>
119% }
120%
121% if (@$dd) {
122<h2>Subfolders</h2>
6e749fab 123<div class="gallery <% $size %>">
f4b90914 124% for my $d (@$dd) {
27ad11a2
MW
125% my $count = "";
126% $count .= "$nd{$d}/" if $nd{$d};
127% $count .= "$nf{$d}" if $nf{$d};
24f4eac3 128 <& .thumbnail, target => $d->name, comment => $d->comment,
ccaab4e8 129 tn => $tn{$d}, size => $size,
27ad11a2
MW
130 caption =>
131 $m->interp->apply_escapes($d->name, "h") . " [$count]" &>\
6e749fab 132% }
f4b90914 133</div>
6ac5dde2
MW
134% }
135%
136% if (@$ff) {
137<h2>Images</h2>
6e749fab 138<div class="gallery <% $size %>">
f4b90914 139% for my $f (@$ff) {
dfdd1964 140 <& .thumbnail, target => $f->name, comment => $f->comment,
ccaab4e8 141 tn => $tn{$f}, size => $size,
dfdd1964 142 caption => $m->interp->apply_escapes($f->name, "h") &>\
6e749fab 143% }
f4b90914 144</div>
6ac5dde2
MW
145% }
146%
147<div class=fill></div>
148<& .footer, path => $path &>
149</&>
150%
151<%args>
152 $path
153</%args>
154</%def>
155%
156%###-------------------------------------------------------------------------
157<%def .image>\
158<%perl>
159 my ($dir, $base, $ext) = split_path $path;
9601c4bb 160
bfc5bfe6
MW
161 if (defined $scale) {
162 my $img = TrivGal::Image->new($path);
784bdf8f 163 $m->redirect($img->scale($scale, 1));
bfc5bfe6 164 }
9601c4bb 165
bfc5bfe6 166 my $real = join_paths $IMGROOT, $path;
6ac5dde2
MW
167 my $url = join_paths $IMGURL, $path;
168 my $realdir = join_paths $IMGROOT, $dir;
169 my $urldir = join_paths $SCRIPTURL, $dir;
170 my ($dd, $ff, $ii) = listdir $realdir;
bfc5bfe6
MW
171 my @thumbsz = qw{smallthumb medthumb bigthumb};
172 my %tn;
173 my $vw;
6ac5dde2
MW
174
175 my $fi = undef;
bfc5bfe6
MW
176 FILE: for (my $i = 0; $i < @$ff; $i++) {
177 my $f = $ff->[$i];
178 my $img = TrivGal::Image->new(join_paths $dir, $f->name);
179 for my $sz (@thumbsz) { $tn{$f->name}{$sz} = $img->scale($sz); }
180 if ($ff->[$i]->name eq "$base$ext")
181 { $fi = $i; $vw = $img->scale("view"); }
182 }
6ac5dde2
MW
183 defined $fi or die "image not found in its folder?";
184 my $this = $ff->[$fi];
185
186 my %link;
187 $link{up} = "";
188 if ($fi != 0) {
189 $link{first} = $ff->[0]->name;
190 $link{prev} = $ff->[$fi - 1]->name;
191 }
192 if ($fi != @$ff - 1) {
193 $link{last} = $ff->[-1]->name;
194 $link{next} = $ff->[$fi + 1]->name;
195 }
196
197 my $links = "";
198 my $pre =
199 urlencode join_paths $SCRIPTURL, $dir;
200 for my $rel (qw{up first prev next last}) {
201 exists $link{$rel} and
202 $links .= sprintf " <link rel=%s href=\"%s\">\n",
203 $rel, urlencode "$pre/$link{$rel}";
204 }
205</%perl>
206%
207<&| .html, title => "Image " . $m->interp->apply_escapes($path, "h"),
208 head => $links &>
209<& .breadcrumbs, what => "Image", path => $path &>
210% if ($this->comment) {
211 <div class=comment>
212 <p><% $this->comment %>
213 </div>
214% }
215%
216<div class=viewnav>
217% if ($link{prev}) {
988a1d43 218 <div class=prev><a class=prev href="<% "$pre/$link{prev}" |hu %>">&lsaquo;</a></div>
6ac5dde2
MW
219% }
220 <a class=view href="<% $url |h %>">
221 <img src="<% $vw |h %>">
222 </a>
223% if ($link{next}) {
988a1d43 224 <div class=next><a class=next href="<% "$pre/$link{next}" |hu %>">&rsaquo;</a></div>
6ac5dde2
MW
225% }
226</div>
227%
1408b7a2 228% for my $size (qw{smallthumb medthumb bigthumb}) {
6e749fab
MW
229<div class="thumbstrip <% $size %>">
230% for my $f (@$ff) {
ccaab4e8 231 <& .thumbnail, target => $f->name,
bfc5bfe6 232 tn => $tn{$f->name}{$size}, size => $size,
6ac5dde2 233 caption => $m->interp->apply_escapes($f->name, "h"),
62ee1c09 234 focus => $f eq $this &>\
6e749fab 235% }
6ac5dde2 236</div>
6e749fab 237% }
6ac5dde2
MW
238<& .footer, path => $dir &>
239</&>
240%
241<%args>
242 $path
9601c4bb 243 $scale => undef
6ac5dde2
MW
244</%args>
245</%def>
246%
247%###-------------------------------------------------------------------------
248<%def .breadcrumbs>\
249% $path =~ s!/$!!;
250% my @p = split m!/!, $path;
251% my $pp = "";
252% my $prev = undef;
253<h1><% $what %> \
254% if (!@p) {
255[top]
256% } else {
988a1d43 257<a href="<% $SCRIPTURL |hu %>/">[top]</a>&thinsp;/&thinsp;\
6ac5dde2
MW
258% STEP: for my $p (@p) {
259% if (defined $prev) {
260% $pp .= "$prev/";
988a1d43 261<a href="<% join_paths($SCRIPTURL, $pp) |hu %>/">\
6ac5dde2
MW
262<% $prev %></a>&thinsp;/&thinsp;\
263% }
264% $prev = $p;
265% }
266<% $prev %>\
267% }
268</h1>
269<%args>
270 $what
271 $path
272</%args>
273</%def>
274%
275%###-------------------------------------------------------------------------
276<%def .thumbnail>\
ccaab4e8 277% $tn //= "$STATICURL/folder.svg";
6ac5dde2 278% if ($focus) {
1408b7a2 279 <figure class="thumb focusthumb <% $size %>">
1126ec40 280 <img class="thumb <% $size %>" loading=lazy src="<% $tn |h %>">
83bfcbc7 281 <figcaption><span class=name><% $caption %></span></figcaption>
6ac5dde2 282% } else {
dfdd1964 283 <figure class="thumb <% $size %>">
1126ec40
MW
284 <a class=thumb href="<% $target |hu %>">
285 <img class="thumb <% $size %>" loading=lazy src="<% $tn |h %>">
83bfcbc7 286 <figcaption>
6ac5dde2
MW
287 <span class=name><% $caption %></span>
288% if (defined $comment) {
289 <span class=comment><% $comment %></span>
290% }
83bfcbc7 291 </figcaption>
6ac5dde2
MW
292 </a>
293% }
83bfcbc7 294 </figure>
6ac5dde2
MW
295%
296<%args>
297 $target
ccaab4e8 298 $tn
dfdd1964 299 $size
6ac5dde2
MW
300 $caption
301 $comment => undef
302 $focus => 0
303</%args>
304</%def>
305%
306%###-------------------------------------------------------------------------
307<%def .footer>\
308<%perl>
309</%perl>
310<div class=footer>
311 <div class=footitem>
988a1d43 312 <a href="https://www.gnu.org/licenses/agpl-3.0.en.html"><img class=licence src="<% "$STATICURL/agpl.png" |hu %>"></a>
6ac5dde2
MW
313 Trivial Gallery, copyright &copy; 2021 Mark Wooding.
314 Free software: you can modify it and/or redistribute it under the
315 terms of the
a54108de 316 <a rel=license href="https://www.gnu.org/licenses/agpl-3.0.en.html">GNU Affero
6ac5dde2 317 General Public License version 3</a>.
a38a867d 318 Browse or download the <a href="<% $SRCURL %>">source code</a>.
6ac5dde2
MW
319 </div>
320% my $user =
321% find_covering_file $IMGROOT, $path, ".tgal-footer.html";
322% if (defined $user) {
323 <div class=footitem>
324<% $user %>
325 </div>
326% }
327</div>
328<%args>
329 $path
330</%args>
331</%def>
332%
333%###-------------------------------------------------------------------------
334<%once>
335 use autodie;
6ac5dde2
MW
336 use File::stat;
337
338 use TrivGal;
339</%once>
340%
341<%init>
342 TrivGal->init;
343
344 my $path = $m->dhandler_arg;
345 my $st = stat "$IMGROOT/$path";
346 my $comp;
347 if (!$st) { $comp = ".not-found"; }
348 elsif (-d $st) { $comp = ".contact"; }
349 elsif (-f $st) { $comp = ".image"; }
350 else { $comp = ".not-found"; }
ecf3c7a8 351 $r->header_out("X-AGPL-Source" => $SRCURL);
6abe8e59 352 $m->comp($comp, path => $path, %ARGS);
6ac5dde2
MW
353</%init>
354%
355%###----- That's all, folks -------------------------------------------------