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