1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
|
#!/usr/bin/perl
# Copyright © 2009-2011 Simon McVittie <http://smcv.pseudorandom.co.uk/>
# Copyright © 2013 Lukas Lipavsky <lukas@lipavsky.cz>
#
# Licensed under the GNU GPL, version 2, or any later version published by the
# Free Software Foundation
package IkiWiki::Plugin::album;
use warnings;
use strict;
use IkiWiki 3.00;
sub import {
hook(type => "getsetup", id => "album", call => \&getsetup);
hook(type => "needsbuild", id => "album", call => \&needsbuild);
hook(type => "preprocess", id => "album",
call => \&preprocess_album, scan => 1);
hook(type => "preprocess", id => "albumsection",
call => \&preprocess_albumsection, scan => 1);
hook(type => "preprocess", id => "albumimage",
call => \&preprocess_albumimage, scan => 1);
hook(type => "pagetemplate", id => "album", call => \&pagetemplate);
# We need these plugins. Additionally, meta is recommended.
IkiWiki::loadplugin("filecheck");
IkiWiki::loadplugin("img");
IkiWiki::loadplugin("inline");
IkiWiki::loadplugin("trail");
IkiWiki::loadplugin("transient");
IkiWiki::loadplugin("tag");
}
sub getsetup () {
return
plugin => {
safe => 1,
rebuild => undef,
section => "widget",
},
# FIXME: unimplemented
album_copydir => {
type => "string",
example => "$ENV{HOME}/photos-to-upload",
description => "if set, copy photos to this directory at reduced size",
advanced => 1,
safe => 0,
rebuild => 1,
},
}
# Terminology:
#
# album - main page for an album/gallery, contains a list of inlined viewers
# viewer - page generated to contain/display/represent one image
# section - a subset of the viewers in an album
#
# Page state for albums:
#
# size - default size for viewers in this album
# thumbnailsize - size to resize thumbnails to
# viewertemplate - template to use for viewers
# nexttemplate - template for "next image", as embedded in viewers
# prevtemplate - template for "previous image", as embedded in viewers
# sort - as for inline
# sections - ref to array of pagespecs representing sections
# viewers - list of viewers' names
# tags - list of tags that should be added to viewers
#
# Page state for image viewers:
#
# album - full name of associated album
# image - full name of image file
# caption - caption if any
# size, thumbnailsize, viewertemplate, nexttemplate, prevtemplate -
# override the corresponding option for the album
sub isalbumableimage ($) {
my $file=shift;
return $file =~ /\.(png|gif|jpg|jpeg|mov)$/i;
}
sub needsbuild {
my $needsbuild = shift;
my $deleted = shift;
foreach my $page (@$needsbuild, @$deleted) {
# it's neither an album nor a viewer, unless it later says
# it is
delete $pagestate{$page}{album};
}
return $needsbuild;
}
sub thumbnail {
my $viewer = shift;
my $destpage = shift;
my $thumbnailsize = shift;
my $image = $pagestate{$viewer}{album}{image};
# img requires that each file is generated by one page, so nominate
# the viewer as the page that makes the thumbnail
my $img = "";
my $title = IkiWiki::Plugin::trail::title_of($viewer);
if (IkiWiki::isinlinableimage($image)) {
$img = IkiWiki::Plugin::img::preprocess(
"$image" => undef,
link => "/$viewer",
size => ($thumbnailsize or '96x96'),
alt => $title,
title => $title,
page => $viewer,
destpage => $destpage);
}
return $img;
}
sub show_in_album {
my $viewers = shift;
my %params = @_;
return IkiWiki::preprocess_inline(
pagenames => join(" ", @$viewers),
actions => "no",
feeds => "no",
template => "albumitem",
show => 0,
%params);
}
# album => [ list of viewers ]
my %scanned;
sub create_viewer {
my ($viewer, $image, $album) = @_;
my $vfile = newpagefile($viewer, $config{default_pageext});
# FIXME: try to read creation date, copyright etc. from EXIF tags
add_autofile($vfile, "album", sub {
my $message = sprintf(gettext("creating album page %s"), $viewer);
debug($message);
my $content = <<"END";
[[!albumimage
title=""
caption=""
date=""
updated=""
author=""
authorurl=""
copyright=""
license=""
description=""
]]
END
# size, thumbnailsize, viewertemplate, prevtemplate,
# nexttemplate aren't in the generated page because
# they're not expected to be commonly used - setting
# them for an entire album is likely to be more useful
writefile($vfile, $IkiWiki::Plugin::transient::transientdir,
$content);
});
}
sub scan_images {
my $album = $_[0];
# There are two purposes to this, optimization (don't bother
# re-scanning images) and correctness (when we're preprocessing
# after the scan stage, we don't want to re-run scan_binary because
# it would override the metadata from [[!albumimage]]).
return @{$scanned{$album}} if exists $scanned{$album};
# All the images that are attached to an album or its subpages count as
# (potential) members of it. For each one, we synthesize a page if no
# page exists.
my $regexp = qr{^\Q$album\E/}i;
my @viewers;
# collect the images
foreach my $candidate (keys %pagesources) {
if ($candidate =~ $regexp &&
isalbumableimage($candidate)) {
my $viewer = $candidate;
$viewer =~ s/\.[^.]+$//;
if (exists $IkiWiki::pagecase{lc $viewer}) {
$viewer = $IkiWiki::pagecase{lc $viewer};
}
else {
create_viewer($viewer, $candidate, $album);
}
push @viewers, $viewer;
# FIXME: what if albums are nested? Current resolution
# is that the image goes in a randomly selected album.
# Because pages are scanned in arbitrary order, I
# don't think we can do better.
$pagestate{$viewer}{album}{album} = $album;
# FIXME: what if there's more than one image file
# with the same basename? Current resolution is that
# a random one "wins".
$pagestate{$viewer}{album}{image} = $candidate;
}
}
$scanned{$album} = \@viewers;
return @viewers;
}
# These hashes are populated by collect_images whenever an album, or any
# image in that album, has changed.
# album => { filter => array of viewers in that section }
# filter "" is the catch-all for images in no other section
my %albumsections;
# linked list of viewers in each album
my (%before, %after);
# viewer => index number in its album
my %albumorder;
sub get_adjacent {
my ($trail, $member) = @_;
IkiWiki::Plugin::trail::prerender();
my $adjacent = $pagestate{$member}{trail}{item}{$trail};
return (undef, undef) unless $adjacent;
return @$adjacent;
}
sub collect_images {
my $album = shift;
return if $albumsections{$album};
my $sort = $pagestate{$album}{album}{sort};
if (!defined $sort) {
$sort = '-age';
}
my %sections;
local $_; # localize iterator variable
my @remaining = @{$pagestate{$album}{album}{viewers}};
foreach my $filter (@{$pagestate{$album}{album}{sections}}) {
next if $filter eq '';
my @section = pagespec_match_list($album,
$filter, sort => $sort,
list => [@remaining]);
my %set = map { $_ => 1 } @section;
@remaining = grep { ! exists $set{$_} } @remaining;
$sections{$filter} = \@section;
}
$sections{""} = [IkiWiki::sort_pages($sort, \@remaining)];
my @ordered;
foreach my $filter (@{$pagestate{$album}{album}{sections}}) {
my $pages;
$pages = $sections{$filter};
push @ordered, @$pages;
$albumsections{$album}{$filter} = $pages;
}
for (my $i = 0; $i <= $#ordered; $i++) {
my $viewer = $ordered[$i];
$albumorder{$viewer} = $i;
# We don't need to track what's before or after:
# trail has API for that.
}
}
sub preprocess_album {
# [[!album]]
my %params=@_;
my $album = $params{page};
my @viewers = scan_images($album);
my $tags = defined($params{tag}) ? $params{tag} : "";
# placeholder for the "remaining images" section
push @{$pagestate{$album}{album}{sections}}, ""
unless grep { $_ eq "" }
@{$pagestate{$album}{album}{sections}};
$pagestate{$album}{album} = {
sort => $params{sort},
size => $params{size},
thumbnailsize => $params{thumbnailsize},
viewertemplate => $params{viewertemplate},
nexttemplate => $params{nexttemplate},
prevtemplate => $params{prevtemplate},
viewers => [@viewers],
tags => [split(' ', $tags)],
# in the render phase, we want to keep the sections that we
# accumulated during the scan phase, if any
sections => $pagestate{$album}{album}{sections},
};
# The sort order depends on matching pagespecs for each
# section, so we can't define it yet - delegate it to a
# sortspec defined by this plugin, which can collect the
# images lazily.
IkiWiki::Plugin::trail::preprocess_trailoptions(
sort => 'albumorder',
page => $album,
destpage => $params{destpage},
);
my %trailparams = (
pagenames => join(' ', @viewers),
page => $album,
destpage => $params{destpage},
);
if (defined wantarray) {
collect_images($album) unless $albumsections{$album};
scalar IkiWiki::Plugin::trail::preprocess_trailitems(%trailparams);
return show_in_album($albumsections{$album}{""},
page => $album,
destpage => $params{destpage});
}
else {
IkiWiki::Plugin::trail::preprocess_trailitems(%trailparams);
}
}
sub preprocess_albumsection {
# [[!albumsection filter="friday/*"]]
my %params=@_;
my $album = $params{page};
my $filter = $params{filter};
local $_;
# remember the filter for this section so the "remaining images" section
# won't include these images (this needs to be run in the scan stage
# so the info will be there for the album directive)
push @{$pagestate{$album}{album}{sections}}, $filter
unless grep { $_ eq $filter }
@{$pagestate{$album}{album}{sections}};
# If we're just scanning, don't bother producing output
return unless defined wantarray;
collect_images($album) unless $albumsections{$album};
return show_in_album($albumsections{$album}{$filter},
page => $album,
destpage => $params{destpage});
}
sub preprocess_albumimage {
my %params=@_;
my $viewer = $params{page};
my $album = $pagestate{$viewer}{album}{album};
my $image = $pagestate{$viewer}{album}{image};
if (! defined $album || ! defined $image) {
error(sprintf(gettext("%s is not in any album"), $viewer));
}
$pagestate{$viewer}{album} = {
album => $album,
image => $image,
};
# [[!albumimage title=foo copyright=bar]] is a shortcut for a couple
# of [[!meta]] invocations. Zero-length metadata gets ignored, so we
# can put all the available metadata in the template album page,
# with zero-length values.
if (IkiWiki::Plugin::meta->can('preprocess')) {
foreach my $meta (qw(title date updated author authorurl
copyright license description)) {
if (defined $params{$meta} && length $params{$meta}) {
IkiWiki::Plugin::meta::preprocess(
$meta => $params{$meta},
page => $viewer);
}
}
}
# The thumbnail has to have exactly one source, so thumbnail() always
# claims that it is this page. To avoid ikiwiki deleting the
# thumbnail, we need to make sure will_render() gets called, and this
# seems the easiest way to do that.
if (defined wantarray) {
my $foo = thumbnail($viewer, $viewer,
$pagestate{$album}{album}{thumbnailsize});
}
else {
thumbnail($viewer, $viewer,
$pagestate{$album}{album}{thumbnailsize});
}
add_depends($viewer, $album);
# If we're just scanning, don't bother producing output
return unless defined wantarray;
# Copy various settings from the album, unless overridden
foreach my $k (qw(viewertemplate nexttemplate prevtemplate size)) {
$params{$k} = $pagestate{$album}{album}{$k}
unless defined $params{$k} && length $params{$k};
}
# Because we're no longer in the scan phase, we know that
# preprocess_album has already run, so we know:
# - which album this photo appears in
# - what order the album is in
# (either because the scan phase has run for modified pages, or
# because the pagestate was loaded from last time for unmodified
# pages.)
collect_images($album) unless $albumsections{$album};
my ($prevpage, $nextpage) = get_adjacent($album, $viewer);
my $prev = '';
my $next = '';
if (defined $nextpage) {
add_depends($album, $nextpage);
$next = show_prevnext($nextpage, $viewer, "next");
}
if (defined $prevpage) {
add_depends($album, $prevpage);
$prev = show_prevnext($prevpage, $viewer, "prev");
}
my $img;
if (IkiWiki::isinlinableimage($image)) {
my $title = IkiWiki::Plugin::trail::title_of($viewer);
$img = IkiWiki::Plugin::img::preprocess("$image" => undef,
title => $title,
alt => $title,
size => ($params{size} or 'full'),
page => $viewer,
destpage => $params{destpage});
}
else {
$img = htmllink($viewer, $params{destpage}, "/$image");
}
# Tags (always return "")
IkiWiki::Plugin::tag::preprocess_tag(
page => $viewer,
destpage => $params{destpage},
map { ($_ => 1) } @{$pagestate{$album}{album}{tags}},
);
my $viewertemplate = template(
$pagestate{$album}{album}{viewertemplate} or
'albumviewer.tmpl');
$viewertemplate->param(page => $viewer,
img => $img,
next => $next,
prev => $prev);
IkiWiki::run_hooks(pagetemplate => sub {
shift->(page => $viewer, destpage => $params{destpage},
template => $viewertemplate);
});
return $viewertemplate->output;
}
sub pagetemplate (@) {
my %params=@_;
my $template = $params{template};
eval q{use Image::Magick};
return if $@;
if (exists $pagestate{$params{page}}{album}{image}) {
# the page is a viewer, maybe treat it specially
my $viewer = $params{page};
my $album = $pagestate{$viewer}{album}{album};
my $image = $pagestate{$viewer}{album}{image};
my $thumbnailsize = $pagestate{$album}{album}{thumbnailsize};
return unless defined $album;
return unless defined $image;
my $title = IkiWiki::Plugin::trail::title_of($viewer);
$template->param(album => $album);
$template->param(albumurl => urlto($album, $params{destpage}));
$template->param(albumtitle => $title);
if ($template->query(name => 'thumbnail')) {
$template->param(thumbnail =>
thumbnail($viewer, $params{destpage}, $thumbnailsize));
}
if (IkiWiki::isinlinableimage($image)
&& ($template->query(name => 'imagewidth') ||
$template->query(name => 'imageheight') ||
$template->query(name => 'imagefilesize') ||
$template->query(name => 'imageformat'))) {
my $im = Image::Magick->new;
my ($w, $h, $s, $f) = $im->Ping(srcfile($image, 1));
$s = IkiWiki::Plugin::filecheck::humansize($s);
$template->param(imagewidth => $w, imageheight => $h,
imagefilesize => $s, imageformat => $f);
}
if ($template->query(name => 'caption')) {
$template->param(caption =>
$pagestate{$viewer}{album}{caption});
}
}
}
sub show_prevnext {
my ($page, $destpage, $which) = @_;
my $template = template("album$which.tmpl", blind_cache => 1);
my $title = IkiWiki::Plugin::trail::title_of($page);
$template->param(
pageurl => urlto($page, $destpage),
title => $title,
ctime => displaytime($IkiWiki::pagectime{$page}),
mtime => displaytime($IkiWiki::pagemtime{$page}),
);
IkiWiki::run_hooks(pagetemplate => sub {
shift->(page => $page, destpage => $destpage,
template => $template);
});
return $template->output;
}
package IkiWiki::SortSpec;
sub cmp_albumorder {
# Firstly, are they even in the same album? If not, order is
# indeterminate - so let's just compare the paths.
my $album = $IkiWiki::pagestate{$a}{album}{album};
my $album2 = $IkiWiki::pagestate{$b}{album}{album};
if (! defined $album || ! defined $album2 || $album ne $album2) {
return $a cmp $b;
}
# OK, now we need to work out where they are in the album.
# We do this lazily because until the scan stage has finished,
# it could change.
IkiWiki::Plugin::album::collect_images($album) unless $albumsections{$album};
return $albumorder{$a} <=> $albumorder{$b};
}
1;
|