-
Notifications
You must be signed in to change notification settings - Fork 7
/
llgal.in
executable file
·2550 lines (2147 loc) · 84.7 KB
/
llgal.in
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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl -w
use strict ;
require Llgal::Config ;
require Llgal::Messages ;
require Llgal::Templates ;
require Llgal::Utils ;
use FileHandle ;
use Image::Size ;
use Cwd ;
use Locale::gettext ;
use POSIX qw (setlocale) ;
use URI::Escape ;
use Text::ParseWords ;
use File::Compare ;
Getopt::Long::Configure('noignorecase', 'noautoabbrev', 'bundling') ;
STDOUT->autoflush("1") ;
# constants and globals
my $self = {
version => "@VERSION@",
llgal_config_dir => "@SYSCONFDIR@/llgal",
llgal_share_dir => "@DATADIR@/llgal",
user_share_dir => $ENV{HOME}."/.llgal",
generic_configuration_filename => "llgalrc",
locale_dir => "@LOCALEDIR@",
# local llgal directory, may be modified during early configuration
local_llgal_dir => ".llgal",
# look in current directory "."
destination_dir => ".",
# no section by default
# section_dirs => (),
# some variable may only be modified when by system
# or user-wide configuration files only
early_configuration => 1,
# show version
version_asked => 0,
# displays brief usage message
help_asked => 0,
# clean up all generated files
clean_asked => 0,
# clean up all generated and user modified files
cleanall_asked => 0,
# generate (or update) captions
generate_captions => 0,
# give templates to the given directory
give_templates => undef,
# generate a config file
generate_config => undef,
# level of indentation, warnings, ...
messages => Llgal::Messages::new (),
} ;
# store command-line options to write in the index <head>
my $llgal_cmdline = join (" ", @ARGV) ;
# parse early command line options
Llgal::Config::early_parse_cmdline_options ($self) ;
# gettext initialization
Llgal::Config::init_llgal_gettext ($self) ;
######################################################################
# various temporary globals
my $messages = $self->{messages} ;
my $destination_string ;
my $local_llgal_url ;
my $opts ;
my $opts_without_defaults ;
######################################################################
# --clean and --cleanall option
sub clean_files {
my $cleanall = shift ;
my $notdeleted = 0 ;
if ($cleanall) {
$messages->print ("Cleaning all in $destination_string\n") ;
} else {
$messages->print ("Cleaning in $destination_string\n") ;
}
opendir DIR, "$self->{destination_dir}$self->{local_llgal_dir}"
or die "Can't open directory $self->{destination_dir}$self->{local_llgal_dir} ($!).\n" ;
while ($_ = readdir DIR) {
if (/^$opts->{thumbnail_image_filenameprefix}/ or /^$opts->{scaled_image_filenameprefix}/) {
unlink "$self->{destination_dir}$self->{local_llgal_dir}/$_" ;
} elsif (/^$opts->{filmtile_filename}$/ or /^$opts->{index_link_image_filename}$/
or /^$opts->{prev_slide_link_image_filename}$/ or /^$opts->{next_slide_link_image_filename}$/
or /^$opts->{indextemplate_filename}$/ or /^$opts->{slidetemplate_filename}$/
or /^$opts->{css_filename}$/) {
my $original = (Llgal::Templates::find_generic_template_file ($self, $opts, $_, 0))."/".$_ ;
my $diff = 0 ;
# default is cleanall, which removes everything, as if file were not changed
if (!$cleanall) {
$diff = compare ("$self->{destination_dir}$self->{local_llgal_dir}/$_", "$original") ;
die "Failed to check whether $self->{destination_dir}$self->{local_llgal_dir}/$_ has been modified ($!).\n"
if $diff == -1 ;
}
if ($diff) {
$messages->warning ("Preserved $self->{destination_dir}$self->{local_llgal_dir}/$_ since it seems to be modified.") ;
$notdeleted++ ;
} else {
if (!unlink "$self->{destination_dir}$self->{local_llgal_dir}/$_" and ! $!{ENOENT}) {
$messages->warning ("Failed to remove file $self->{destination_dir}$self->{local_llgal_dir}/$_ ($!).") ;
}
}
} elsif (/^$opts->{captions_filename}$/) {
my $grep = 0 ;
# default is cleanall, which removes everything, as if the removal line was here
if (!$cleanall) {
open CAP, "$self->{destination_dir}$self->{local_llgal_dir}/$_" ;
my @lines = <CAP> ;
close CAP ;
$grep = scalar (grep { $_ =~ $opts->{captions_removal_line} } @lines) ;
}
if (!$grep) {
$messages->warning ("Preserved $self->{destination_dir}$self->{local_llgal_dir}/$_ since it seems to be modified.") ;
$notdeleted++ ;
} else {
if (!unlink "$self->{destination_dir}$self->{local_llgal_dir}/$_" and ! $!{ENOENT}) {
$messages->warning ("Failed to remove file $self->{destination_dir}$self->{local_llgal_dir}/$_.") ;
}
}
} else {
$notdeleted++ ;
}
}
closedir DIR ;
if ($notdeleted <= 2 and $cleanall) {
if (!rmdir "$self->{destination_dir}$self->{local_llgal_dir}" and ! $!{ENOENT}) {
$messages->warning ("Failed to remove directory $self->{destination_dir}$self->{local_llgal_dir} ($!).") ;
}
}
opendir DIR, $self->{destination_dir} ? $self->{destination_dir} : "./" ; # destination is empty for './'
while ($_ = readdir DIR ) {
if (/^$opts->{index_filename}\.$opts->{www_extension}$/
or /^$opts->{slide_filenameprefix}.*\.$opts->{www_extension}$/) {
unlink "$self->{destination_dir}$_" ;
}
}
closedir DIR ;
}
#####################################################################
# Main variables
# The main gallery is composed of
# - @entries (the entry list)
# - @headers and @footers
sub init_gallery {
my $gallery = () ;
@{$gallery->{headers}} = () ;
@{$gallery->{footers}} = () ;
@{$gallery->{entries}} = () ;
%{$gallery->{user_fields}} = () ;
$gallery->{indextemplate} = undef ;
@{$gallery->{slidetemplate_text}} = () ;
@{$gallery->{template_exif_tags_list}} = () ;
return $gallery ;
}
# Entries are hashed composed of
# - type: see below
# - filename (original filename with extension)
# - url (url of the filename)
# - linktext (an associated text that may be used in a link, www-safe)
# - caption (the caption of the slide, www-safe)
# - title (title, www-safe)
# - no_thumb (1 or undef, means that the entry is listed instead of displayed as a thumbnail)
# - no_slide (1 or undef, means that the entry does not appear as any slide at all)
# - xdim, ydim, kbytes (details about the image or movie)
# - dimstring (string containing dimensions and/or size that were requested)
# - slide_filename (name of the HTML slide filename)
# - slide_url (url of the HTML slide filename)
# - counter_with_zeros (stringified counter used in slide's filename)
# - thumb_xdim, thumb_ydim (details about the thumbnail image)
# - thumb_dimstring (string containing dimensions)
# - thumb_filename (name of the thumbnail image file)
# - thumb_url (url of the thumbnail image file)
# - scaled_xdim, scaled_ydim, scaled_kbytes (details about the scaled image)
# - scaled_dimstring (string containing dimensions and/or size that were requested)
# - scaled_filename (name of the scaled image file)
# - scaled_url (url of the scaled image file)
# - gallery (the whole gallery in the subdirectory pointed by this entry)
# types
my $TYPE_TEXT = 0 ;
my $TYPE_LINK = 1 ;
my $TYPE_IMAGE = 2 ;
my $TYPE_MOVIE = 3 ;
my $TYPE_FILE = 4 ;
my $TYPE_DIR = 5 ;
my $TYPE_LINE = 6 ;
my $TYPE_BREAK = 7 ;
my $TYPE_LINKNOSLIDE = 8 ; # only used when calling create_nofile_entry, which changes it into $TYPE_LINK with no_slide set
###################################################################################
# checking files
sub check_file {
my $filename = shift ;
if (! -e $filename) {
$messages->warning ("Cannot find file '$filename', skipping it.") ;
return -1 ;
}
my $excluded = 0 ;
for(my $i = 0; $i < @{$opts->{excludes}}; $i++) {
my $entry = @{$opts->{excludes}}[$i] ;
if ($filename =~ m@^(.*/)*$entry->{filter}$@) {
$excluded = $entry->{excluded}
}
}
return -1
if $excluded ;
return 0 ;
}
###################################################################################
# returns a filename that the user provided in the llgal directory
# to be used as a thumbnail or scaled image
sub find_user_thumbnail {
my $entry = shift ;
my $type = $entry->{type} ;
my $filename = $entry->{filename} ;
my $user_thumbnail_filename = "$opts->{user_thumbnail_image_filenameprefix}$opts->{thumbnail_image_filenameprefix}$filename" ;
if ($type == $TYPE_IMAGE) {
if (-f "$self->{destination_dir}$self->{local_llgal_dir}/$user_thumbnail_filename") {
return $user_thumbnail_filename ;
}
} else {
foreach my $ext (split (/\|/, $opts->{image_extensions})) {
if (-f "$self->{destination_dir}$self->{local_llgal_dir}/$user_thumbnail_filename.$ext") {
return "$user_thumbnail_filename.$ext" ;
}
}
}
return "" ;
}
sub find_user_scaled {
my $entry = shift ;
my $type = $entry->{type} ;
my $filename = $entry->{filename} ;
my $user_scaled_filename = "$opts->{user_scaled_image_filenameprefix}$opts->{scaled_image_filenameprefix}$filename" ;
if ($type == $TYPE_IMAGE) {
if (-f "$self->{destination_dir}$self->{local_llgal_dir}/$user_scaled_filename") {
return $user_scaled_filename ;
}
} else {
foreach my $ext (split (/\|/, $opts->{image_extensions})) {
if (-f "$self->{destination_dir}$self->{local_llgal_dir}/$user_scaled_filename.$ext") {
return "$user_scaled_filename.$ext" ;
}
}
}
return "" ;
}
###################################################################################
# extract caption from image infos
# Image comment
sub generate_caption_from_image_comment {
my $exif_infos = shift ;
my @texts = () ;
# Loop until a non-empty comment list is found
foreach my $types (split (/,/, $opts->{make_caption_from_image_comment})) {
# types is a + separated string of types
foreach my $type (split (/\+/, $types)) {
if ($type =~ m/^std$/i) {
# Standard comment such as JFIF or GIF
my $comment = $exif_infos->{Comment} ;
push (@texts, $comment)
if $comment ;
} elsif ($type =~ m/^exif$/i) {
# Exif comment
my $comment = $exif_infos->{UserComment} ;
push (@texts, $comment)
if $comment ;
} elsif ($type =~ m/^exifdesc$/i) {
# Exif image description
my $comment = $exif_infos->{ImageDescription} ;
push (@texts, $comment)
if $comment ;
} else {
$messages->abort_percentage ;
die "Unrecognized image comment type '$type'.\n" ;
}
}
return @texts
if @texts ;
}
return () ;
}
# Image timestamp
sub generate_caption_from_image_timestamp {
my $exif_infos = shift ;
my $imagetime = $exif_infos->{DateTimeOriginal} ;
return $imagetime;
}
# Image infos
sub generate_caption_from_image_infos {
my $exif_infos = shift ;
my @texts = () ;
if ($opts->{make_caption_from_image_comment}) {
my @comments = generate_caption_from_image_comment $exif_infos ;
push (@texts, @comments) ;
}
if ($opts->{make_caption_from_image_timestamp}) {
my $timestamp = generate_caption_from_image_timestamp $exif_infos ;
push (@texts, $timestamp) ;
}
return @texts ;
}
###################################################################################
# generating safe url
sub make_safe_url_nowarn {
return join '/', map { uri_escape $_ } (split /\//, shift) ;
}
sub make_safe_url {
my $file = shift ;
my $safe = make_safe_url_nowarn ($file) ;
$messages->warning ("Non-ascii characters were escaped in filename '$file'.")
if $safe ne $file and $file =~ m/[\x80-\xFF]/ ;
return $safe ;
}
###################################################################################
# Generate entries
sub create_file_entry {
my $entry = () ;
my $gallery = shift ;
my $type = shift ;
my $filename = shift ;
my $linktext = shift ;
my $caption = shift ;
# cache real filename for later
my $real_filename = "$self->{destination_dir}$filename" ;
# check file
return undef
if check_file ($real_filename) < 0 ;
# set the type
$entry->{type} = $type ;
# set the filename
$entry->{filename} = $filename ;
# make a safe url
$entry->{url} = make_safe_url ($filename) ;
# the link text (will be web-safe after caption file generation)
if (not defined $linktext) {
$linktext = "" ;
if ($type == $TYPE_MOVIE) {
$linktext = $opts->{MVI_link_text}.$filename ;
} elsif ($type == $TYPE_FILE) {
$linktext = $opts->{FIL_link_text}.$filename ;
} elsif ($type == $TYPE_DIR) {
$linktext = $opts->{DIR_link_text}.$filename ;
}
$linktext =~ s/&/&/g ;
# $linktext =~ s/"/&\#34;/g ;
}
$entry->{linktext} = $linktext ;
if ($type == $TYPE_DIR) {
# subgallery specific fields
# add index.html so that it works without a web server
$entry->{url} = $entry->{url}."/".$opts->{index_filename}.".".$opts->{www_extension} ;
# generate a name for links from prev/next gallery
my @parts = split /\//, $filename ;
$entry->{gallery_name} = pop @parts ;
}
# for images, we need at least the dimensions (and check their validity) and possibly some Exif info
# if some Exif are needed, retrieve dimensions with ExifTool too
# if not, just retrieve dimensions with imgsize
if ($type == $TYPE_IMAGE) {
my ($x,$y) ;
# if showing some exif flags, or using some options that require Exif,
# use ExifTool for both these and image dimensions validity checking
if ($opts->{show_all_exif_tags}
or @{$opts->{show_exif_tags}}
or @{$gallery->{template_exif_tags_list}}
or $opts->{make_caption_from_image_timestamp}
or $opts->{make_caption_from_image_comment}) {
# only load Image::ExifTool when really required
if (not defined $opts->{exiftool}) {
# Try to load the module
if (not eval { require Image::ExifTool ; }) {
$messages->abort_percentage ();
die "Perl module Image::ExifTool not found, please install it if you wish to extract tags from images.\n" ;
}
# ExifTool initialization
$opts->{exiftool} = new Image::ExifTool;
# accept unknown tags, just in case...
$opts->{exiftool}->Options(Unknown => 1) ;
# set the GPS coodinate format **HACK**
$opts->{exiftool}->Options(CoordFormat => "%.6f") ;
# dateFormat should be initialized with whatever the user said to --ct
$opts->{exiftool}->Options(DateFormat => $opts->{timestamp_format_in_caption}) ;
# initialize the list of tags, empty for all
my @exif_tags = () ;
if (!$opts->{show_all_exif_tags}) {
# use the user provided Exif tags, and add ImageWidth/Height for ourself
@exif_tags = @{$gallery->{template_exif_tags_list}} ;
@exif_tags = (@exif_tags, 'ImageWidth', 'ImageHeight', @{$opts->{show_exif_tags}}) ;
@exif_tags = (@exif_tags, 'DateTimeOriginal')
if $opts->{make_caption_from_image_timestamp} ;
@exif_tags = (@exif_tags, 'UserComment', 'Comment', 'ImageDescription')
if $opts->{make_caption_from_image_comment} ;
}
@{$opts->{cached_exif_tags_list}} = @exif_tags ;
}
# do the actual exif tag retrieval
my $exif_infos = $opts->{exiftool}->ImageInfo ($real_filename, @{$opts->{cached_exif_tags_list}}) ;
$entry->{exif_infos} = $exif_infos ;
# get image dimensions
$x = $exif_infos->{ImageWidth} ;
$y = $exif_infos->{ImageHeight} ;
} else {
# if not using Exif at all, just retrieve dimensions for ourself
($x,$y) = imgsize ($real_filename) ;
}
# check dimensions validity
if (not defined $x or not defined $y) {
$messages->warning ("Bad image file '$filename', cannot retrieve its dimensions, skipping it.") ;
return undef ;
}
$entry->{xdim} = $x ;
$entry->{ydim} = $y ;
}
# the caption (will be web-safe after caption file generation)
if (not defined $caption) {
my @texts = () ;
if ($opts->{make_caption_from_extension}) {
push (@texts, $filename) ;
} elsif ($opts->{make_caption_from_filename}) {
my $basename = $filename ;
if ($type != $TYPE_DIR) {
# remove the extension
$basename =~ s/\.[^\.]+$// ;
}
push (@texts, $basename) ;
}
push (@texts, generate_caption_from_image_infos $entry->{exif_infos})
if $type == $TYPE_IMAGE ;
$caption = join (' - ', grep {$_} @texts) ;
$caption =~ s/&/&/g ;
# $caption =~ s/"/&\#34;/g ;
}
chomp $caption ;
$entry->{caption} = $caption ;
# title is based on the filename
my $title = $filename ;
$title =~ s/&/&/g ;
# $title =~ s/"/&\#34;/g ;
# add caption to the title if asked
$title .= ": ". $caption
if $opts->{make_slide_title_from_caption} ;
$entry->{title} = $title ;
# do not generate more if we are only generating captions
return $entry ;
}
sub fill_file_entry {
my $entry = shift ;
my $type = $entry->{type} ;
my $filename = $entry->{filename} ;
# cache real filename for later
my $real_filename = "$self->{destination_dir}$filename" ;
# xdim, ydim, kbytes of the image or movie
if ($type == $TYPE_IMAGE) {
# initialize here so that we can use it even when scaled are not used
$entry->{scaled_xdim} = $entry->{xdim} ;
$entry->{scaled_ydim} = $entry->{ydim} ;
}
if ($type == $TYPE_IMAGE or $type == $TYPE_MOVIE or $type == $TYPE_FILE) {
my $kbytes = (-s $real_filename) >> 10 ;
$entry->{kbytes} = $kbytes ;
# initialize here so that we can use it even when scaled are not used
$entry->{scaled_kbytes} = $kbytes ;
}
# dimstring
if ($type == $TYPE_IMAGE) {
if ($opts->{show_dimensions} and $opts->{show_size}) {
$entry->{dimstring} = "($entry->{xdim}x$entry->{ydim}, $entry->{kbytes}$opts->{show_size_unit})" ;
} elsif ($opts->{show_size}) {
$entry->{dimstring} = "($entry->{kbytes}$opts->{show_size_unit})" ;
} elsif ($opts->{show_dimensions}) {
$entry->{dimstring} = "($entry->{xdim}x$entry->{ydim})" ;
} else {
$entry->{dimstring} = "" ;
}
} elsif ($type == $TYPE_MOVIE or $type == $TYPE_FILE) {
if ($opts->{show_size}) {
$entry->{dimstring} = "($entry->{kbytes}$opts->{show_size_unit})" ;
} else {
$entry->{dimstring} = "" ;
}
} else {
$entry->{dimstring} = "" ;
}
####################################
# thumbnail, its url and dimensions
my $thumb_filename = find_user_thumbnail ($entry) ;
if ($thumb_filename) {
# there's a user provided thumbnail, just use it
# cache real thumb filename for later
my $real_thumb_filename = "$self->{destination_dir}$self->{local_llgal_dir}/$thumb_filename" ;
$entry->{thumb_filename} = "$self->{local_llgal_dir}/$thumb_filename" ;
$entry->{thumb_url} = "$local_llgal_url/". make_safe_url_nowarn ($thumb_filename) ;
# thumbnail dimensions
my ($tx, $ty) = imgsize ($real_thumb_filename) ;
# check thumbnail validity
if (not defined $tx or not defined $ty) {
$messages->warning ("Bad user-given thumbnail file '$real_thumb_filename', cannot retrieve its dimensions, ignoring it.") ;
goto NO_USER_THUMB ;
}
# resize in the gallery if necessary
if ($ty > $opts->{thumbnail_height_max}) {
$tx = $tx * ($opts->{thumbnail_height_max} / $ty) ;
$ty = $opts->{thumbnail_height_max} ;
}
if ($opts->{thumbnail_width_max} > 0 and $tx > $opts->{thumbnail_width_max}) {
$ty = $ty * ($opts->{thumbnail_width_max} / $tx) ;
$tx = $opts->{thumbnail_width_max} ;
}
# store values
($entry->{thumb_xdim},$entry->{thumb_ydim}) = ($tx, $ty) ;
# dimstring
if ($opts->{show_dimensions}) {
$entry->{thumb_dimstring} = "(${tx}x${ty})" ;
} else {
$entry->{thumb_dimstring} = "" ;
}
} elsif ($type == $TYPE_IMAGE) {
NO_USER_THUMB:
# no user provided thumbnail, generate one
# filename and url
$thumb_filename = "$opts->{thumbnail_image_filenameprefix}$filename" ;
$thumb_filename =~ s@/@$opts->{path_separator_replacement}@g ;
$entry->{thumb_filename} = "$self->{local_llgal_dir}/$thumb_filename" ;
$entry->{thumb_url} = "$local_llgal_url/". make_safe_url_nowarn ($thumb_filename) ;
# cache real thumb filename for later
my $real_thumb_filename = "$self->{destination_dir}$entry->{thumb_filename}" ;
# if the thumbnail does not exist, or is older than the image, or regeneration is forced
if ((! -e $real_thumb_filename)
or (-M $real_thumb_filename) > (-M $real_filename)
or $opts->{force_image_regeneration}) {
my ($status, @output) ;
# only scale down, never up.
if ($entry->{ydim} <= $opts->{thumbnail_height_max}
and ($opts->{thumbnail_width_max} <= 0 or $entry->{xdim} <= $opts->{thumbnail_width_max})) {
# the original is not large enough, keep it
$entry->{thumb_filename} = $entry->{filename} ;
$entry->{thumb_url} = $entry->{url} ;
$real_thumb_filename = $real_filename ;
} else {
# scale down
my @cmdline = map {
s/<IN>/$real_filename/g ;
s/<OUT>/$real_thumb_filename/g ;
$_ ;
} Text::ParseWords::parse_line('\s+', 0, $opts->{thumbnail_create_command}) ;
($status, @output) = Llgal::Utils::system_with_output ( "create '$filename' thumbnail", @cmdline ) ;
if ($status == -1) {
$messages->warning (@output) ;
$messages->abort_percentage ;
die "Failed to create '$real_filename' thumbnail.\n" ;
} elsif ($status) {
$messages->warning ("Failed to create '$real_filename' thumbnail.") ;
$messages->warning (@output) ;
return undef ;
}
}
}
# thumbnail dimensions
my ($tx,$ty) = imgsize ($real_thumb_filename) ;
# check thumbnail validity
if (not defined $tx or not defined $ty) {
$messages->warning ("Bad thumbnail file '$real_thumb_filename', cannot retrieve its dimensions, skipping this entry.") ;
return undef ;
}
# store values
($entry->{thumb_xdim},$entry->{thumb_ydim}) = ($tx,$ty) ;
# dimstring
if ($opts->{show_dimensions}) {
$entry->{thumb_dimstring} = "(${tx}x${ty})" ;
} else {
$entry->{thumb_dimstring} = "" ;
}
} else {
# default thumbnail dimensions
$entry->{thumb_xdim} = $opts->{default_thumb_xdim} ;
$entry->{thumb_ydim} = $opts->{default_thumb_ydim} ;
# dimstring is empty
$entry->{thumb_dimstring} = "" ;
}
if (!$opts->{make_no_slides}) {
#######################################
# scaled image, its url and dimensions
my $scaled_filename = find_user_scaled ($entry) ;
if ($scaled_filename) {
# there's a user provided scaled, just use it
# cache real thumb filename for later
my $real_scaled_filename = "$self->{destination_dir}$self->{local_llgal_dir}/$scaled_filename" ;
$entry->{scaled_filename} = "$self->{local_llgal_dir}/$scaled_filename" ;
$entry->{scaled_url} = "$local_llgal_url/". make_safe_url_nowarn ($scaled_filename) ;
# scaled image dimensions
my ($sx, $sy) = imgsize ($real_scaled_filename) ;
my $skbytes = (-s $real_scaled_filename) >> 10 ;
# check scaled validity
if (not defined $sx or not defined $sy) {
$messages->warning ("Bad user-given scaled image file '$real_scaled_filename', cannot retrieve its dimensions, ignoring it.") ;
goto NO_USER_SCALED ;
}
# resize in the gallery if necessary
if ($opts->{slide_height_max} > 0 and $sy > $opts->{slide_height_max}) {
$sx = $sx * ($opts->{slide_height_max} / $sy) ;
$sy = $opts->{slide_height_max} ;
}
if ($opts->{slide_width_max} > 0 and $sx > $opts->{slide_width_max}) {
$sy = $sy * ($opts->{slide_width_max} / $sx) ;
$sx = $opts->{slide_width_max} ;
}
# store values
($entry->{scaled_xdim},$entry->{scaled_ydim}) = ($sx, $sy) ;
$entry->{scaled_kbytes} = $skbytes ;
# dimstring
if ($opts->{show_dimensions} and $opts->{show_size}) {
$entry->{scaled_dimstring} = "(${sx}x${sy}, ${skbytes}$opts->{show_size_unit})" ;
} elsif ($opts->{show_size}) {
$entry->{scaled_dimstring} = "(${skbytes}$opts->{show_size_unit})" ;
} elsif ($opts->{show_dimensions}) {
$entry->{scaled_dimstring} = "(${sx}x${sy})" ;
} else {
$entry->{scaled_dimstring} = "" ;
}
} elsif ($type == $TYPE_IMAGE and ($opts->{slide_width_max} > 0 or $opts->{slide_height_max} > 0)) {
NO_USER_SCALED:
# no user provided scaled, generate one
# filename and url
$scaled_filename = "$opts->{scaled_image_filenameprefix}$filename" ;
$scaled_filename =~ s@/@$opts->{path_separator_replacement}@g ;
$entry->{scaled_filename} = "$self->{local_llgal_dir}/$scaled_filename" ;
$entry->{scaled_url} = "$local_llgal_url/". make_safe_url_nowarn ($scaled_filename) ;
# cache real thumb filename for later
my $real_scaled_filename = "$self->{destination_dir}$entry->{scaled_filename}" ;
# if the thumbnail does not exist, or is older than the image, or regeneration is forced
if ((! -e $real_scaled_filename)
or (-M $real_scaled_filename) > (-M $real_filename)
or $opts->{force_image_regeneration}) {
my ($status, @output) ;
# only scale down, never up.
if (($opts->{slide_width_max} <= 0 or $entry->{xdim} <= $opts->{slide_width_max})
and ($opts->{slide_height_max} <= 0 or $entry->{ydim} <= $opts->{slide_height_max})) {
# the original is not large enough, keep it
$entry->{scaled_filename} = $entry->{filename} ;
$entry->{scaled_url} = $entry->{url} ;
$real_scaled_filename = $real_filename ;
} else {
# scale down
my @cmdline = map {
s/<IN>/$real_filename/g ;
s/<OUT>/$real_scaled_filename/g ;
$_ ;
} Text::ParseWords::parse_line('\s+', 0, $opts->{scaled_create_command}) ;
($status, @output) = Llgal::Utils::system_with_output ( "create '$filename' scaled image", @cmdline ) ;
if ($status == -1) {
$messages->warning (@output) ;
$messages->abort_percentage ;
die "Failed to create '$real_filename' scaled image.\n" ;
} elsif ($status) {
$messages->warning ("Failed to create '$real_filename' scaled image.") ;
$messages->warning (@output) ;
return undef ;
}
}
}
# scaled dimensions
my ($sx,$sy) = imgsize ($real_scaled_filename) ;
my $skbytes = (-s $real_scaled_filename) >> 10 ;
# check scaled validity
if (not defined $sx or not defined $sy) {
$messages->warning ("Bad scaled image file '$real_scaled_filename', cannot retrieve its dimensions, skipping this entry.") ;
return undef ;
}
# store values
($entry->{scaled_xdim},$entry->{scaled_ydim}) = ($sx,$sy) ;
$entry->{scaled_kbytes} = $skbytes ;
# dimstring
if ($opts->{show_dimensions} and $opts->{show_size}) {
$entry->{scaled_dimstring} = "(${sx}x${sy}, ${skbytes}$opts->{show_size_unit})" ;
} elsif ($opts->{show_size}) {
$entry->{scaled_dimstring} = "(${skbytes}$opts->{show_size_unit})" ;
} elsif ($opts->{show_dimensions}) {
$entry->{scaled_dimstring} = "(${sx}x${sy})" ;
} else {
$entry->{scaled_dimstring} = "" ;
}
} else {
$entry->{scaled_dimstring} = $entry->{dimstring} ;
}
} else {
$entry->{scaled_dimstring} = $entry->{dimstring} ;
}
return $entry ;
}
sub create_nofile_entry {
my $entry = () ;
my $type = shift ;
my $url = shift ;
my $linktext = shift ;
my $caption = shift ;
# special type case
if ($type == $TYPE_LINKNOSLIDE) {
$entry->{no_slide} = 1 ;
$type = $TYPE_LINK ;
}
# set the type
$entry->{type} = $type ;
# row break and horizontal lines need nothing
if ($type == $TYPE_LINE or $type == $TYPE_BREAK) {
$entry->{no_slide} = 1 ;
$entry->{no_thumb} = 1 ;
return $entry ;
}
# make a safe url
if (defined $url) {
if ($type == $TYPE_LINK) {
$entry->{url} = $url ;
} else {
$entry->{url} = make_safe_url ($url) ;
}
}
# the link text (will be web-safe after caption file generation)
$entry->{linktext} = $linktext ;
# the caption (will be web-safe after caption file generation)
$caption = "" unless defined $caption ;
$entry->{caption} = $caption ;
# add caption to the title if asked
my $title = "" ;
$title = $caption
if $opts->{make_slide_title_from_caption} ;
$entry->{title} = $title ;
# do not generate more if we are only generating captions
return $entry ;
}
sub fill_nofile_entry {
my $entry = shift ;
# dimstring
$entry->{dimstring} = "" ;
# default thumbnail dimensions
$entry->{thumb_xdim} = $opts->{default_thumb_xdim} ;
$entry->{thumb_ydim} = $opts->{default_thumb_ydim} ;
# dimstring is empty
$entry->{thumb_dimstring} = "" ;
if (!$opts->{make_no_slides}) {
# scaled image dimensions
$entry->{scaled_dimstring} = $entry->{dimstring} ;
}
return $entry ;
}
# fill entries, everything that's not required for
# captions generation, but may be done early
sub fill_entries {
my $gallery = shift ;
my @entries = @{$gallery->{entries}} ;
my @final_entries = () ;
$messages->print ("Preparing entries: ") ;
$messages->init_percentage (scalar @entries) ;
my $i = 0 ;
for my $entry (@entries) {
my $type = $entry->{type} ;
my $final_entry ;
if ($type == $TYPE_LINE or $type == $TYPE_BREAK) {
$final_entry = $entry ; # nothing to do
} elsif ($type == $TYPE_TEXT or $type == $TYPE_LINK) {
$final_entry = fill_nofile_entry $entry ;
} else {
$final_entry = fill_file_entry $entry ;
}
push @final_entries, $final_entry
if defined $final_entry ;
$messages->update_percentage (($i++)+1) ;
}
$messages->end_percentage () ;
@{$gallery->{entries}} = @final_entries ;
}
# generate slide number, filename and url only when the order of entries is fixed
sub finalize_entries {
my $gallery = shift ;
my $opts = shift ;
my @entries = @{$gallery->{entries}} ;
if ($opts->{list_links}) {
map {
my $type = $_->{type} ;
if (!$_->{thumb_filename}
and ($type == $TYPE_LINK or $type == $TYPE_DIR or $type == $TYPE_MOVIE
or $type == $TYPE_FILE or $type == $TYPE_TEXT)) {
$_->{no_thumb} = 1 ;
}
} @entries ;
}
# account slides
my $nslides = scalar (grep { ! $_->{no_slide} } @entries) ;
$gallery->{nslides} = $nslides ;
# prepare numbering
my $counter_length = length $nslides ;
my $prev = undef ;
my $first = undef ;
my $indix = 0 ;
for(my $i=0; $i < @entries; $i++) {
my $entry = $entries[$i] ;
# entries without a slide are not taken into account
next if defined $entry->{no_slide} ;
# if making no slides, only subgallery may be linked together
next if $opts->{make_no_slides} and $entry->{type} != $TYPE_DIR and $opts->{link_subgalleries} ;
# slide counter
$indix++;
$entry->{counter} = $indix ;
my $counter_with_zeros = sprintf "%0${counter_length}d", $indix ;
$entry->{counter_with_zeros} = $counter_with_zeros ;
my $type = $entry->{type} ;
my $filename = $entry->{filename} ;
# HTML slide and its url
if (!$opts->{make_no_slides} and $type != $TYPE_LINE and $type != $TYPE_BREAK) {
my $slide_filename ;
if ($opts->{make_slide_filename_from_filename}) {
if ($type == $TYPE_TEXT or $type == $TYPE_LINK) {
$slide_filename = "$opts->{slide_filenameprefix}$counter_with_zeros.$opts->{www_extension}" ;
} elsif ($opts->{make_slide_filename_from_extension} or $type == $TYPE_DIR) {
$slide_filename = "$opts->{slide_filenameprefix}$filename.$opts->{www_extension}" ;
} else {
my @parts = split (/\./, $filename) ;
pop @parts ;
my $basename = join ('.', @parts) ;
$slide_filename = "$opts->{slide_filenameprefix}$basename.$opts->{www_extension}" ;
}
} else {
$slide_filename = "$opts->{slide_filenameprefix}$counter_with_zeros.$opts->{www_extension}" ;
}
$entry->{slide_filename} = $slide_filename ;
$entry->{slide_url} = make_safe_url_nowarn ($slide_filename) ;
}
# initialize to -1 for no link to prev/next slide
$entry->{prev} = -1 ;
$entry->{next} = -1 ;
# link to previous entry
if (defined $prev) {
$entry->{prev} = $prev;
$entries[$prev]->{next} = $i ;
}
$prev = $i ;
# store first
$first = $i unless defined $first ;
}
if (defined $first and $opts->{link_between_last_and_first}) {
# link last to first
$entries[$prev]->{next} = $first ;
$entries[$first]->{prev} = $prev ;
}
}
#######################################################################
# Generating entry table from files in target directory
sub sort_entries {
my $path = shift ;
if ($opts->{sort_criteria} eq "name") {
return sort @_ ;
} elsif ($opts->{sort_criteria} eq "revname") {
return sort { $b cmp $a } @_ ;
} elsif ($opts->{sort_criteria} eq "iname") {
return sort { uc($a) cmp uc($b) } @_ ;