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