chiark / gitweb /
mason/dhandler: Announce source code through `X-AGPL-Source' HTTP header.
[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 => "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>
110 %   for my $size (qw{medthumb}) {
111 <div class="gallery <% $size %>">
112 %     for my $d (@$dd) {
113   <& .thumbnail, target => $d->name . "/", comment => $d->comment,
114                  img => $tn{$d}, size => $size,
115                  caption => $m->interp->apply_escapes($d->name, "h") &>\
116 %     }
117 </div>
118 %   }
119 % }
120 %
121 % if (@$ff) {
122 <h2>Images</h2>
123 %   for my $size (qw{medthumb}) {
124 <div class="gallery <% $size %>">
125 %     for my $f (@$ff) {
126   <& .thumbnail, target => $f->name, comment => $f->comment,
127                  img => $tn{$f}, size => "bigthumb",
128                  caption => $m->interp->apply_escapes($f->name, "h") &>\
129 %     }
130 </div>
131 %   }
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;
152         my $vw = TrivGal::Image->new($path)->scale("view");
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 %
202 % my %img = map { $_ => TrivGal::Image->new($dir . "/" . $_->name) } @$ff;
203 % for my $size (qw{smallthumb medthumb bigthumb}) {
204 <div class="thumbstrip <% $size %>">
205 %   for my $f (@$ff) {
206   <& .thumbnail, target => $f->name, img => $img{$f}, size => $size,
207                  caption => $m->interp->apply_escapes($f->name, "h"),
208                  focus => $f->name eq "$base$ext" &>\
209 %   }
210 </div>
211 % }
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;
251 % if (defined $img) { $tn = $img->scale($size); }
252 % else { $tn = "$STATICURL/folder.svg"; }
253 % if ($focus) {
254   <figure class="thumb focusthumb <% $size %>">
255     <img class="thumb <% $size %>" load=lazy src="<% $tn |u %>">
256     <figcaption><span class=name><% $caption %></span></figcaption>
257 % } else {
258   <figure class="thumb <% $size %>">
259     <a class=thumb href="<% $target |u %>">
260       <img class="thumb <% $size %>" load=lazy src="<% $tn |u %>">
261       <figcaption>
262         <span class=name><% $caption %></span>
263 % if (defined $comment) {
264         <span class=comment><% $comment %></span>
265 % }
266       </figcaption>
267     </a>
268 % }
269   </figure>
270 %
271 <%args>
272         $target
273         $img
274         $size
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 rel=license href="https://www.gnu.org/licenses/agpl-3.0.en.html">GNU Affero
292     General Public License version 3</a>.
293     Browse or download the <a href="<% $SRCURL %>">source code</a>.
294   </div>
295 % my $user =
296 %   find_covering_file $IMGROOT, $path, ".tgal-footer.html";
297 % if (defined $user) {
298   <div class=footitem>
299 <% $user %>
300   </div>
301 % }
302 </div>
303 <%args>
304         $path
305 </%args>
306 </%def>
307 %
308 %###-------------------------------------------------------------------------
309 <%once>
310         use autodie;
311 use Data::Dumper;
312         use File::stat;
313
314         use TrivGal;
315 </%once>
316 %
317 <%init>
318         TrivGal->init;
319
320         my $path = $m->dhandler_arg;
321         my $st = stat "$IMGROOT/$path";
322         my $comp;
323         if (!$st) { $comp = ".not-found"; }
324         elsif (-d $st) { $comp = ".contact"; }
325         elsif (-f $st) { $comp = ".image"; }
326         else { $comp = ".not-found"; }
327         $r->header_out("X-AGPL-Source" => $SRCURL);
328         $m->comp($comp, path => $path);
329 </%init>
330 %
331 %###----- That's all, folks -------------------------------------------------