support panoramas
[booh] / bin / booh-backend
1 #! /usr/bin/ruby
2 #
3 #                         *  BOOH  *
4 #
5 # A.k.a `Best web-album Of the world, Or your money back, Humerus'.
6 #
7 # The acronyn sucks, however this is a tribute to Dragon Ball by
8 # Akira Toriyama, where the last enemy beaten by heroes of Dragon
9 # Ball is named "Boo". But there was already a free software project
10 # called Boo, so this one will be it "Booh". Or whatever.
11 #
12 #
13 # Copyright (c) 2005 Guillaume Cottenceau <gc3 at bluewin.ch>
14 #
15 # This software may be freely redistributed under the terms of the GNU
16 # public license version 2.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
22 require 'getoptlong'
23 require 'gettext'
24 include GetText
25 require 'rexml/document'
26 include REXML
27
28 require 'booh/booh-lib'
29 require 'booh/html-merges'
30
31 #- bind text domain as soon as possible because some _() functions are called early to build data structures
32 bindtextdomain("booh")
33
34 #- options
35 $options = [
36     [ '--help',          '-h', GetoptLong::NO_ARGUMENT,       _("Get help message") ],
37     [ '--version',       '-V', GetoptLong::NO_ARGUMENT,       _("Print version and exit") ],
38
39     [ '--source',        '-s', GetoptLong::REQUIRED_ARGUMENT, _("Directory which contains original images/videos as files or subdirs") ],
40     [ '--destination',   '-d', GetoptLong::REQUIRED_ARGUMENT, _("Directory which will contain the web-album") ],
41 #    [ '--clean',         '-c', GetoptLong::NO_ARGUMENT,       _("Clean destination directory") ],
42
43     [ '--theme',         '-t', GetoptLong::REQUIRED_ARGUMENT, _("Select HTML theme to use") ],
44     [ '--config',        '-C', GetoptLong::REQUIRED_ARGUMENT, _("File containing config listing images and videos within directories with captions") ],
45     [ '--config-skel',   '-k', GetoptLong::REQUIRED_ARGUMENT, _("Filename where the script will output a config skeleton") ],
46     [ '--merge-config',  '-M', GetoptLong::REQUIRED_ARGUMENT, _("File containing config listing, where to merge new images/videos from --source, and change theme info") ],
47     [ '--merge-config-onedir',  '-O', GetoptLong::REQUIRED_ARGUMENT, _("File containing config listing, for merging the subdir specified with --dir") ],
48     [ '--merge-config-subdirs', '-U', GetoptLong::REQUIRED_ARGUMENT, _("File containing config listing, for merging the new subdirs down the subdir specified with --dir") ],
49     [ '--dir',           '-D', GetoptLong::REQUIRED_ARGUMENT, _("Directory for merge with --merge-config-onedir or --merge-config-subdirs") ],
50     [ '--use-config',    '-u', GetoptLong::REQUIRED_ARGUMENT, _("File containing config listing, where to change theme info") ],
51     [ '--force',         '-f', GetoptLong::NO_ARGUMENT,       _("Force generation of album even if the GUI marked some directories as already generated") ],
52
53     [ '--sizes',         '-S', GetoptLong::REQUIRED_ARGUMENT, _("Specify the list of images sizes to use instead of all specified in the theme (this is a comma-separated list)") ],
54     [ '--thumbnails-per-row', '-T', GetoptLong::REQUIRED_ARGUMENT, _("Specify the amount of thumbnails per row in the thumbnails page (if applicable in theme)") ],
55     [ '--optimize-for-32', '-o', GetoptLong::NO_ARGUMENT,       _("Resize images with optimized sizes for 3/2 aspect ratio rather than 4/3 (typical aspect ratio of pictures from non digital cameras are 3/2 when pictures from digital cameras are 4/3)") ],
56     [ '--empty-comments','-e', GetoptLong::NO_ARGUMENT,       _("Prefer empty comments over filename when creating new albums") ],
57
58     [ '--mproc',         '-m', GetoptLong::REQUIRED_ARGUMENT, _("Specify the number of processors for multi-processors machines") ],
59
60     [ '--for-gui',       '-g', GetoptLong::NO_ARGUMENT,       _("Do the minimum work to be able to see the album under the GUI (don't generate all thumbnails)") ],
61
62     [ '--verbose-level', '-v', GetoptLong::REQUIRED_ARGUMENT, _("Set max verbosity level (0: errors, 1: warnings, 2: important messages, 3: other messages)") ],
63     [ '--info-pipe',     '-i', GetoptLong::REQUIRED_ARGUMENT, _("Name a file where to write information about what's going on (used by the GUI)") ],
64 ]
65
66 #- default values for some globals 
67 $switches = []
68 $stdout.sync = true
69 $no_identify = false
70 $ignore_videos = false
71 $forgui = false
72
73 def usage
74     puts _("Usage: %s [OPTION]...") % File.basename($0)
75     $options.each { |ary|
76         printf " %3s, %-18s %s\n", ary[1], ary[0], ary[3]
77     }
78 end
79
80 def handle_options
81     parser = GetoptLong.new
82     parser.set_options(*$options.collect { |ary| ary[0..2] })
83     begin
84         parser.each_option do |name, arg|
85             case name
86             when '--help'
87                 usage
88                 exit(0)
89
90             when '--version'
91                 puts _("Booh version %s
92
93 Copyright (c) 2005 Guillaume Cottenceau.
94 This is free software; see the source for copying conditions.  There is NO
95 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.") % $VERSION
96
97                 exit(0)
98
99             when '--source'
100                 $source = File.expand_path(arg.sub(%r|/$|, ''))
101                 if !File.directory?($source)
102                     die _("Argument to --source must be a directory")
103                 end
104             when '--destination'
105                 $dest = File.expand_path(arg.sub(%r|/$|, ''))
106                 if File.exists?($dest) && !File.directory?($dest)
107                     die _("If --destination exists, it must be a directory")
108                 end
109                 if $dest != make_dest_filename($dest)
110                     die _("Sorry, destination directory can't contain non simple alphanumeric characters.")
111                 end
112 #            when '--clean'
113 #                system("rm -rf #{$dest}")
114
115             when '--theme'
116                 $theme = arg
117             when '--config'
118                 arg = File.expand_path(arg)
119                 if File.readable?(arg)
120                     $xmldoc = REXML::Document.new File.new(arg)
121                     $mode = 'use_config'
122                 else
123                     die _('Config file does not exist or is unreadable.')
124                 end
125             when '--config-skel'
126                 arg = File.expand_path(arg)
127                 if File.exists?(arg)
128                     if File.directory?(arg)
129                         die _("Config skeleton file (%s) already exists and is a directory! Please change the filename.") % arg
130                     else
131                         msg 1, _("Config skeleton file already exists, backuping to %s.backup") % arg
132                         File.rename(arg, "#{arg}.backup")
133                     end
134                 end
135                 $config_writeto = arg
136                 $mode = 'gen_config'
137             when '--merge-config'
138                 arg = File.expand_path(arg)
139                 if File.readable?(arg)
140                     msg 2, _("Merge config notice: backuping current config file to %s.backup") % arg
141                     $xmldoc = REXML::Document.new File.new(arg)
142                     File.rename(arg, "#{arg}.backup")
143                     $config_writeto = arg
144                     $mode = 'merge_config'
145                 else
146                     die _('Config file does not exist or is unreadable.')
147                 end
148             when '--merge-config-onedir'
149                 arg = File.expand_path(arg)
150                 if File.readable?(arg)
151                     msg 2, _("Merge config notice: backuping current config file to %s.backup") % arg
152                     $xmldoc = REXML::Document.new File.new(arg)
153                     File.rename(arg, "#{arg}.backup")
154                     $config_writeto = arg
155                     $mode = 'merge_config_onedir'
156                 else
157                     die _('Config file does not exist or is unreadable.')
158                 end
159             when '--merge-config-subdirs'
160                 arg = File.expand_path(arg)
161                 if File.readable?(arg)
162                     msg 2, _("Merge config notice: backuping current config file to %s.backup") % arg
163                     $xmldoc = REXML::Document.new File.new(arg)
164                     File.rename(arg, "#{arg}.backup")
165                     $config_writeto = arg
166                     $mode = 'merge_config_subdirs'
167                 else
168                     die _('Config file does not exist or is unreadable.')
169                 end
170             when '--dir'
171                 arg = File.expand_path(arg)
172                 if !File.readable?(arg)
173                     die _('Specified directory to merge with --dir is not readable')
174                 else
175                     $onedir = arg
176                 end
177             when '--use-config'
178                 arg = File.expand_path(arg)
179                 if File.readable?(arg)
180                     msg 2, _("Use config notice: backuping current config file to %s.backup") % arg
181                     $xmldoc = REXML::Document.new File.new(arg)
182                     File.rename(arg, "#{arg}.backup")
183                     $config_writeto = arg
184                     $mode = 'use_config_changetheme'
185                 else
186                     die _('Config file does not exist or is unreadable.')
187                 end
188
189             when '--sizes'
190                 $limit_sizes = arg
191
192             when '--thumbnails-per-row'
193                 $N_per_row = arg
194
195             when '--optimize-for-32'
196                 $optimize_for_32 = true
197
198             when '--empty-comments'
199                 $empty_comments = true
200
201             when '--force'
202                 $force = true
203
204             when '--mproc'
205                 $mproc = arg.to_i
206                 $pids = []
207
208             when '--for-gui'
209                 $forgui = true
210
211             when '--verbose-level'
212                 $verbose_level = arg.to_i
213
214             when '--info-pipe'
215                 $info_pipe = File.open(arg, File::WRONLY)
216                 $info_pipe.sync = true
217             end
218         end
219     rescue
220         puts $!
221         usage
222         exit(1)
223     end
224
225     if !$source && $xmldoc
226         $source = from_utf8($xmldoc.root.attributes['source'])
227         $dest = from_utf8($xmldoc.root.attributes['destination'])
228         $theme ||= $xmldoc.root.attributes['theme']
229         $limit_sizes ||= $xmldoc.root.attributes['limit-sizes']
230         if $mode == 'use_config' || $mode =~ /^merge_config/
231             $optimize_for_32 = !$xmldoc.root.attributes['optimize-for-32'].nil?
232             $N_per_row = $xmldoc.root.attributes['thumbnails-per-row']
233         end
234     end
235
236     if $mode == 'merge_config_onedir' && !$onedir
237         die _("Missing --dir for --merge-config-onedir")
238     end
239     if $mode == 'merge_config_subdirs' && !$onedir
240         die _("Missing --dir for --merge-config-subdirs")
241     end
242
243     if !$source
244         usage
245         exit(0)
246     end
247     if !$dest
248         die _("Missing --destination parameter.")
249     end
250     if !$theme
251         $theme = 'simple'
252     end
253
254     select_theme($theme, $limit_sizes, $optimize_for_32, $N_per_row)
255
256     if !$xmldoc
257         additional_params = ''
258         if $limit_sizes
259             additional_params += "limit-sizes='#{$limit_sizes}'"
260         end
261         if $optimize_for_32
262             additional_params += " optimize-for-32='true'"
263         end
264         if $N_per_row
265             additional_params += " thumbnails-per-row='#{$N_per_row}'"
266         end
267         $xmldoc = Document.new "<booh version='#{$VERSION}' source='#{utf8($source)}' destination='#{utf8($dest)}' theme='#{$theme}' #{additional_params}/>"
268         $xmldoc << XMLDecl.new(XMLDecl::DEFAULT_VERSION, $CURRENT_CHARSET)
269         $mode = 'gen_config'
270     end
271
272     if $mode == 'merge_config' || $mode == 'use_config_changetheme'
273         $xmldoc.root.add_attribute('theme', $theme)
274         $xmldoc.root.add_attribute('version', $VERSION)
275         if $limit_sizes
276             $xmldoc.root.add_attribute('limit-sizes', $limit_sizes)
277         else
278             $xmldoc.root.delete_attribute('limit-sizes')
279         end
280         if $optimize_for_32
281             $xmldoc.root.add_attribute('optimize-for-32', 'true')
282         else
283             $xmldoc.root.delete_attribute('optimize-for-32')
284         end
285         if $N_per_row
286             $xmldoc.root.add_attribute('thumbnails-per-row', $N_per_row)
287         else
288             $xmldoc.root.delete_attribute('thumbnails-per-row')
289         end
290     end
291 end
292
293 def read_config
294     $config = {}
295 end
296
297 def write_config
298 end
299
300 def info(value)
301     if $info_pipe
302         $info_pipe.puts(value)
303     end
304 end
305
306 def check_installation
307     if !system("which convert >/dev/null 2>/dev/null")
308         die _("The program 'convert' is needed. Please install it. 
309 It is generally available with the 'ImageMagick' software package.")
310     end
311     if !system("which identify >/dev/null 2>/dev/null")
312         msg 1, _("the program 'identify' is needed to get images sizes and EXIF data. Please install it.
313 It is generally available with the 'ImageMagick' software package.")
314         $no_identify = true
315     end
316     missing = %w(transcode mencoder).delete_if { |prg| system("which #{prg} >/dev/null 2>/dev/null") }
317     if missing != []
318         msg 1, _("the following program(s) are needed to handle videos: '%s'. Videos will be ignored.") % missing.join(', ')
319         $ignore_videos = true
320     end
321 end
322
323 def replace_line(surround, keyword, line)
324     begin
325         contents = eval "$#{keyword}"
326         line.sub!(/#{surround}#{keyword}#{surround}/, contents)
327     rescue TypeError
328         die _("No '%s' found for substitution") % keyword
329     end
330 end
331
332 def build_html_skeletons
333     $html_images     = File.open("#{$FPATH}/themes/#{$theme}/skeleton_image.html").readlines
334     $html_thumbnails = File.open("#{$FPATH}/themes/#{$theme}/skeleton_thumbnails.html").readlines
335     $html_index      = File.open("#{$FPATH}/themes/#{$theme}/skeleton_index.html").readlines
336     for line in $html_images + $html_thumbnails + $html_index
337         while line =~ /~~~(\w+)~~~/
338             replace_line('~~~', $1, line)
339         end
340     end
341 end
342
343 def find_caption_value(xmldir, filename)
344     if cap = xmldir.elements["*[@filename='#{utf8(filename)}']"].attributes['caption']
345         return cap.gsub("\n", '<br/>')
346     else
347         return nil
348     end
349 end
350
351 def find_captions(xmldir, images)
352     return images.collect { |img| find_caption_value(xmldir, img) }
353 end
354
355 #- stolen from CVSspam
356 def urlencode(text)
357   text.sub(/[^a-zA-Z0-9\-,.*_\/]/) do
358     "%#{sprintf('%2X', $&[0])}"
359   end
360 end
361
362 def all_images_sizes
363     return $limit_sizes =~ /original/ ? $images_size + [ { 'name' => 'original' } ] : $images_size
364 end
365
366 def html_reload_to_thumbnails
367     html_reload_to_thumbnails = $preferred_size_reloader.clone
368     html_reload_to_thumbnails.gsub!(/~~theme~~/, $theme)
369     html_reload_to_thumbnails.gsub!(/~~default_size~~/, $default_size['name'])
370     html_reload_to_thumbnails.gsub!(/~~all_sizes~~/, all_images_sizes.collect { |s| "\"#{size2js(s['name'])}\"" }.join(', '))
371     return html_reload_to_thumbnails
372 end
373
374 def discover_iterations(iterations, line)
375     if line =~ /~~iterate(\d)_open(_max(\d+|N))?~~/
376         for iter in iterations.values
377             if iter['open']
378                 iter['open'] = false
379                 iter['close_wait'] = $1.to_i
380             end
381         end
382         max = $3 == 'N' ? ($N_per_row || $default_N) : $3
383         iterations[$1.to_i] = { 'open' => true, 'max' => max, 'opening' => '', 'closing' => '' }
384         if $1.to_i == 1
385             line.sub!(/.*/, '~~thumbnails~~')
386         else
387             line.sub!(/.*/, '')
388         end
389     elsif line =~ /~~iterate(\d)_close~~/
390         iterations[$1.to_i]['open']  = false;
391         iterations[$1.to_i]['close'] = true;
392         line.sub!(/.*/, '')
393     else
394         for iter in iterations.values
395             if iter['open']
396                 iter['opening'] += line
397                 line.sub!(/.*/, '')
398             end
399             if !iter['close'] && iter['close_wait'] && iterations[iter['close_wait']]['close']
400                 iter['closing'] += line
401                 line.sub!(/.*/, '')
402             end
403         end
404     end
405 end
406
407 def reset_iterations(iterations)
408     for iter in iterations.values
409         iter['value'] = 0
410     end
411 end
412
413 def run_iterations(iterations, amount)
414     html = ''
415     should_rerun = false
416     for level in iterations.keys.sort
417         if iterations[level]['value'] == 0
418             html += iterations[level]['opening']
419         elsif level == iterations.keys.max
420             if !iterations[level]['max'] || iterations[level]['max'] && iterations[level]['value'] + amount <= iterations[level]['max'].to_i
421                 html += iterations[level]['opening']
422             else
423                 should_rerun = true
424             end
425         end
426         iterations[level]['value'] += amount
427         if iterations[level]['max'] && iterations[level]['value'] > iterations[level]['max'].to_i
428             iterations[level]['value'] = 0
429             iterations[level-1]['value'] = 0
430             html += iterations[level-1]['closing']
431         end
432     end
433     if should_rerun
434         return html + run_iterations(iterations, amount)
435     else
436         return html
437     end
438 end
439
440 def close_iterations(iterations)
441     html = ''
442     for level in iterations.keys.sort.reverse
443         html += iterations[level]['closing']
444     end
445     return html
446 end
447
448 def img_element(fullpath)
449     if size = get_image_size(fullpath)
450         sizespec = 'width="' + size[:x].to_s + '" height="' + size[:y].to_s + '"'
451     else
452         sizespec = ''
453     end
454     return '<img src="' + File.basename(fullpath) + '" ' + sizespec + ' class="image"/>'
455 end
456
457 def size2js(name)
458     return name.gsub(/-/, '')
459 end
460
461 def substitute_html_sizes(html, sizeobj, type)
462     sizestrings = []
463     if $images_size.length > 1 || (type == 'image' && $limit_sizes =~ /original/)
464         for sizeobj2 in $images_size
465             if sizeobj != sizeobj2
466                 if type == 'thumbnails'
467                     sizestrings << '<a href="thumbnails-' + size2js(sizeobj2['name']) + '.html">' + sizename(sizeobj2['name']) + '</a>'
468                 else
469                     sizestrings << '<a id="link' + size2js(sizeobj2['name']) + '">' + sizename(sizeobj2['name']) + '</a>'
470                 end
471             else
472                 sizestrings << sizename(sizeobj2['name'])
473             end
474         end
475         if type == 'image' && $limit_sizes =~ /original/
476             sizestrings << '<a id="linkoriginal" target="newframe">' + sizename('original') + '</a>'
477         end
478     end
479     html.sub!(/~~sizes~~(.+)~~/) { sizestrings.join($1) }
480 end
481
482 def xmldir2destdir(xmldir)
483     return make_dest_filename(from_utf8(File.basename(xmldir.attributes['path'])))
484 end
485
486 def find_previous_album(xmldir)
487     relative_pos = ''
488     begin
489         #- move to previous dir element if exists
490         if prevelem = xmldir.previous_element_byname_notattr('dir', 'deleted')
491             xmldir = prevelem
492             relative_pos += '../' + xmldir2destdir(xmldir) + '/'
493             child = nil
494             #- after having moved to previous dir, we need to go down last subdir until the last one
495             while child = xmldir.elements['dir']
496                 while nextchild = child.next_element_byname_notattr('dir', 'deleted')
497                     child = nextchild
498                 end
499                 relative_pos += xmldir2destdir(child) + '/'
500                 xmldir = child
501             end
502         else
503             #- previous dir doesn't exist, move to previous dir element if exists
504             xmldir = xmldir.parent
505             if xmldir.name == 'dir' && !xmldir.attributes['deleted']
506                 relative_pos += '../'
507             else
508                 return nil
509             end
510         end
511     end while !xmldir.child_byname_notattr('image', 'deleted') && !xmldir.child_byname_notattr('video', 'deleted')
512     return File.reduce_path(relative_pos)
513 end
514
515 def find_next_album(xmldir)
516     relative_pos = ''
517     begin
518         #- first child dir element (catches when initial xmldir has both thumbnails and subdirs)
519         if firstchild = xmldir.child_byname_notattr('dir', 'deleted')
520             xmldir = firstchild
521             relative_pos += xmldir2destdir(xmldir) + '/'
522         #- next brother
523         elsif nextbro = xmldir.next_element_byname_notattr('dir', 'deleted')
524             xmldir = nextbro
525             relative_pos += '../' + xmldir2destdir(xmldir) + '/'
526         else
527             #- go up until we have a next brother or we are finished
528             begin
529                 xmldir = xmldir.parent
530                 relative_pos += '../'
531             end while xmldir && !xmldir.next_element_byname_notattr('dir', 'deleted')
532             if xmldir
533                 xmldir = xmldir.next_element_byname('dir')
534                 relative_pos += '../' + xmldir2destdir(xmldir) + '/'
535             else
536                 return nil
537             end
538         end
539     end while !xmldir.child_byname_notattr('image', 'deleted') && !xmldir.child_byname_notattr('video', 'deleted')
540     return File.reduce_path(relative_pos)
541 end
542
543 def sub_previous_next_album(previous_album, next_album, html)
544     if previous_album
545         html.gsub!(/~~previous_album~~/, '<a href="' + previous_album + 'thumbnails.html">' + utf8(_('previous album')) + '</a>')
546         html.gsub!(/~~ifprevious_album\?~~(.+?)~~fi~~/) { $1 }
547     else
548         html.gsub!(/~~previous_album~~/, '')
549         html.gsub!(/~~ifprevious_album\?~~(.+?)~~fi~~/, '')
550     end
551     if next_album
552         html.gsub!(/~~next_album~~/, '<a href="' + next_album + 'thumbnails.html">' + utf8(_('next album')) + '</a>')
553         html.gsub!(/~~ifnext_album\?~~(.+?)~~fi~~/) { $1 }
554     else
555         html.gsub!(/~~next_album~~/, '')
556         html.gsub!(/~~ifnext_album\?~~(.+?)~~fi~~/, '')
557     end
558     return html
559 end
560
561 def walk_source_dir
562
563     #- preprocess the path->dir, rexml is very slow with that; we seem to improve speed by 7%
564     optxpath = {}
565     $xmldoc.elements.each('//dir') { |elem|
566         optxpath[elem.attributes['path']] = elem
567     }
568
569     examined_dirs = nil
570     if $mode == 'merge_config_onedir'
571         examined_dirs = [ $onedir ]
572     elsif $mode == 'merge_config_subdirs'
573         examined_dirs = `find '#{$onedir}' -type d -follow`.sort.collect { |v| v.chomp }.delete_if { |v| optxpath.has_key?(utf8(v)) }
574     else
575         examined_dirs = `find '#{$source}' -type d -follow`.sort
576     end
577     info("directories: #{examined_dirs.length}, sizes: #{$images_size.length}")
578
579     examined_dirs.each { |dir|
580         dir.chomp!
581         if File.basename(dir) =~ /^\./
582             msg 1, _("Ignoring directory %s, begins with a dot (indicating a hidden directory)") % dir
583             next
584         end
585
586         if dir =~ /'/
587             die _("Source directory or sub-directories can't contain a single-quote character, sorry: %s") % dir
588         end
589
590         #- place xml document on proper node if exists, else create
591         xmldir = optxpath[utf8(dir)]
592         if $mode == 'use_config' || $mode == 'use_config_changetheme'
593             if !xmldir || (xmldir.attributes['already-generated'] && !$force) || xmldir.attributes['deleted']
594                 info("walking: #{dir}|#{$source}, 0 elements")
595                 next
596             end
597         else
598             if $mode == 'gen_config' || (($mode == 'merge_config' || $mode == 'merge_config_subdirs') && !xmldir)
599                 #- add the <dir..> element if necessary
600                 parent = File.dirname(dir)
601                 xmldir = $xmldoc.elements["//dir[@path='#{utf8(parent)}']"]
602                 if !xmldir
603                     xmldir = $xmldoc.root
604                 end
605                 #- need to remove the already-generated mark of the parent because of the sub-albums page containing now one more element
606                 xmldir.delete_attribute('already-generated')
607                 xmldir = optxpath[utf8(dir)] = xmldir.add_element('dir', { 'path' => utf8(dir) })
608             end
609         end
610         xmldir.delete_attribute('already-generated')
611
612         #- read images/videos entries from config or from directories depending on mode
613         entries = []
614         if $mode == 'use_config' || $mode == 'use_config_changetheme'
615             msg 2, _("Handling %s from config list...") % dir
616             xmldir.elements.each { |element|
617                 if %w(image video).include?(element.name) && !element.attributes['deleted']
618                     entries << from_utf8(element.attributes['filename'])
619                 end
620             }
621         else
622             msg 2, _("Examining %s...") % dir
623             entries = Dir.entries(dir).sort
624             #- populate config in case of gen_config, add new files in case of merge_config
625             for file in entries
626                 if file =~ /['"\[\]]/
627                     msg 1, _("Ignoring %s, contains one of forbidden characters: '\"[]") % "#{dir}/#{file}"
628                 else
629                     type = entry2type(file)
630                     if type && !xmldir.elements["#{type}[@filename='#{utf8(file)}']"]
631                         xmldir.add_element type, { "filename" => utf8(file), "caption" => $empty_comments ? '' : utf8(file.sub(/\.[^\.]+$/, '')[0..17]) }
632                     end
633                 end
634             end
635             if $mode != 'gen_config'
636                 #- cleanup removed files from config and reread entries from config to get proper ordering
637                 entries = []
638                 xmldir.elements.each { |element|
639                     fullpath = "#{dir}/#{from_utf8(element.attributes['filename'])}"
640                     if %w(image video).include?(element.name)
641                         if !File.readable?(fullpath)
642                             msg 1, _("Config merge: removing %s from config; use the backup file to retrieve caption info if this was a mistake") % fullpath
643                             xmldir.delete(element)
644                         elsif !element.attributes['deleted']
645                             entries << from_utf8(element.attributes['filename'])
646                         end
647                     end
648                 }
649                 #- if there is no more elements here, there is no album here anymore
650                 if !xmldir.child_byname_notattr('image', 'deleted') && !xmldir.child_byname_notattr('video', 'deleted')
651                     xmldir.delete_attribute('thumbnails-caption')
652                     xmldir.delete_attribute('thumbnails-captionfile')
653                 end
654             end
655         end
656         images = entries.find_all { |e| entry2type(e) == 'image' }
657         msg 3, _("\t%s images") % images.length
658         videos = entries.find_all { |e| entry2type(e) == 'video' }
659         msg 3, _("\t%s videos") % videos.length
660         info("walking: #{dir}|#{$source}, #{images.length + videos.length} elements")
661
662         dest_dir = make_dest_filename(dir.sub(/^#{Regexp.quote($source)}/, $dest))
663         system("mkdir -p '#{dest_dir}'")
664
665         #- pass through if there are no images and videos
666         if images.size == 0 && videos.size == 0
667             if !$forgui
668                 #- cleanup old images/videos, especially if this directory contained images/videos previously.
669                 themestuff = Dir.entries("#{$FPATH}/themes/#{$theme}").
670                                 find_all { |e| !%w(. .. skeleton_image.html skeleton_thumbnails.html skeleton_index.html metadata CVS).include?(e) }
671                 if $mode != 'gen_config'
672                     rightful_images = []
673                     if xmldir.attributes['thumbnails-caption']
674                         rightful_images << 'thumbnails-thumbnail.jpg'
675                     end
676                     xmldir.elements.each('dir') { |child|
677                         if child.attributes['deleted']
678                             next
679                         end
680                         subdir = make_dest_filename(from_utf8(File.basename(child.attributes['path'])))
681                         rightful_images << "thumbnails-#{subdir}.jpg"
682                     }
683                     to_del = Dir.entries(dest_dir).find_all { |e| !File.directory?("#{dest_dir}/#{e}") && !rightful_images.include?(e) } - themestuff
684                     if to_del.size > 0
685                         system("rm -f " + to_del.collect { |e| "#{dest_dir}/#{e}" }.join(' '))
686                     end
687                 end
688                 
689                 #- copy any resource file that goes with the theme (css, images..)
690                 themestuff.each { |entry|
691                     if !File.exists?("#{dest_dir}/#{entry}")
692                         psys("cp '#{$FPATH}/themes/#{$theme}/#{entry}' '#{dest_dir}'")
693                     end
694                 }
695             end
696             next
697         end
698
699         msg 2, _("Outputting in %s...") % dest_dir
700
701         #- populate data structure with sizes from theme
702         for sizeobj in $images_size
703             fullscreen_images ||= {}
704             fullscreen_images[sizeobj['name']] = []
705             thumbnail_images ||= {}
706             thumbnail_images[sizeobj['name']] = []
707             thumbnail_videos ||= {}
708             thumbnail_videos[sizeobj['name']] = []
709         end
710         #- a special dummy size to keep 'references' to thumbnails in case of panorama, because the GUI will use the regular thumbnails
711         thumbnail_images['dont-delete-file-for-gui'] = []
712         if $limit_sizes =~ /original/
713             fullscreen_images['original'] = []
714         end
715
716         images.size >= 1 and msg 3, _("\tcreating images thumbnails...")
717
718         #- create thumbnails for images
719         images.each { |img|
720             info("processing element")
721             base_dest_img = dest_dir + '/' + make_dest_filename(img.sub(/\.[^\.]+$/, ''))
722             if $forgui
723                 thumbnail_dest_img = base_dest_img + "-#{$default_size['thumbnails']}.jpg"
724                 gen_thumbnails_element("#{dir}/#{img}", xmldir, true, [ { 'filename' => thumbnail_dest_img, 'size' => $default_size['thumbnails'] } ])
725             else
726                 todo = []
727                 elem = xmldir.elements["image[@filename='#{utf8(img)}']"]
728                 for sizeobj in $images_size
729                     size_fullscreen = sizeobj['fullscreen']
730                     size_thumbnails = sizeobj['thumbnails']
731                     fullscreen_dest_img = base_dest_img + "-#{size_fullscreen}.jpg"
732                     fullscreen_images[sizeobj['name']] << File.basename(fullscreen_dest_img)
733                     todo << { 'filename' => fullscreen_dest_img, 'size' => size_fullscreen }
734                     if pano = pano_amount(elem)
735                         thumbnail_images['dont-delete-file-for-gui'] << File.basename(base_dest_img + "-#{size_thumbnails}.jpg")
736                         size_thumbnails = size_thumbnails.sub(/(\d+)/) { ($1.to_i * pano).to_i }
737                     end
738                     thumbnail_dest_img = base_dest_img + "-#{size_thumbnails}.jpg"
739                     thumbnail_images[sizeobj['name']] << File.basename(thumbnail_dest_img)
740                     todo << { 'filename' => thumbnail_dest_img,  'size' => size_thumbnails }
741                 end
742                 gen_thumbnails_element("#{dir}/#{img}", xmldir, true, todo)
743                 if $limit_sizes =~ /original/
744                     fullscreen_images['original'] << img
745                 end
746                 destimg = "#{dest_dir}/#{img}"
747                 if $limit_sizes =~ /original/ && !File.exists?(destimg)
748                     psys("cp '#{dir}/#{img}' '#{destimg}'")
749                 end
750             end
751         }
752
753         videos.size >= 1 and msg 3, _("\tcreating videos thumbnails...")
754
755         #- create thumbnails for videos
756         videos.each { |video|
757             info("processing element")
758             if $forgui
759                 thumbnail_dest_img = dest_dir + '/' + make_dest_filename(video.sub(/\.[^\.]+$/, '')) + "-#{$default_size['thumbnails']}.jpg"
760                 gen_thumbnails_element("#{dir}/#{video}", xmldir, true, [ { 'filename' => thumbnail_dest_img, 'size' => $default_size['thumbnails'] } ])
761             else
762                 todo = []
763                 for sizeobj in $images_size
764                     size_thumbnails = sizeobj['thumbnails']
765                     thumbnail_dest_img = dest_dir + '/' + make_dest_filename(video.sub(/\.[^\.]+$/, '')) + "-#{size_thumbnails}.jpg"
766                     thumbnail_videos[sizeobj['name']] << File.basename(thumbnail_dest_img)
767                     todo << { 'filename' => thumbnail_dest_img, 'size' => size_thumbnails }
768                 end
769                 gen_thumbnails_element("#{dir}/#{video}", xmldir, true, todo)
770             end
771             destvideo = "#{dest_dir}/#{video}"
772             if !File.exists?(destvideo)
773                 psys("cp '#{dir}/#{video}' '#{destvideo}'")
774             end
775         }
776
777         if !$forgui
778             themestuff = Dir.entries("#{$FPATH}/themes/#{$theme}").
779                              find_all { |e| !%w(. .. skeleton_image.html skeleton_thumbnails.html skeleton_index.html metadata CVS).include?(e) }
780
781             #- cleanup old images/videos (for when removing elements or sizes)
782             all_elements = fullscreen_images.collect { |e| e[1] }.flatten.
783                      concat(thumbnail_images.collect { |e| e[1] }.flatten).
784                      concat(thumbnail_videos.collect { |e| e[1] }.flatten).
785                      concat(videos)
786             to_del = Dir.entries(dest_dir).find_all { |e| !File.directory?("#{dest_dir}/#{e}") && !all_elements.include?(e) && e !~ /^thumbnails-\w+\.jpg/ } - themestuff
787             if to_del.size > 0
788                 system("rm -f " + to_del.collect { |e| "#{dest_dir}/#{e}" }.join(' '))
789             end
790
791             #- copy any resource file that goes with the theme (css, images..)
792             themestuff.each { |entry|
793                 if !File.exists?("#{dest_dir}/#{entry}")
794                     psys("cp '#{$FPATH}/themes/#{$theme}/#{entry}' '#{dest_dir}'")
795                 end
796             }
797
798             msg 3, _("\tgenerating HTML pages...")
799
800             #- generate thumbnails.html (page with thumbnails)
801             for sizeobj in $images_size
802                 info("processing size")
803                 html = $html_thumbnails.collect { |l| l.clone }
804                 iterations = {}
805                 for i in html
806                     i.sub!(/~~run_slideshow~~/, images.size <= 1 ? '' : '<a href="image-' + size2js(sizeobj['name']) + '.html#run_slideshow=1">' + utf8(_('Run slideshow!'))+'</a>')
807                     i.sub!(/~~title~~/, xmldir.attributes['thumbnails-caption'] || utf8(File.basename(dir)))
808                     substitute_html_sizes(i, sizeobj, 'thumbnails')
809                     discover_iterations(iterations, i)
810                 end
811                 html_thumbnails = ''
812                 reset_iterations(iterations)
813                 for file in entries
814                     type = images.include?(file) ? 'image' : videos.include?(file) ? 'video' : nil
815                     if type
816                         if type == 'image' && elem = xmldir.elements["image[@filename='#{utf8(file)}']"]
817                             if  pano = pano_amount(elem)
818                                 html_thumbnails += run_iterations(iterations, pano)
819                                 html_thumbnails.gsub!(/~~colspan~~/) { 'colspan="' + pano.ceil.to_s + '"' }
820                             else
821                                 html_thumbnails += run_iterations(iterations, 1)
822                                 html_thumbnails.gsub!(/~~colspan~~/, '')
823                             end
824                         else 
825                             html_thumbnails += run_iterations(iterations, 1)
826                             html_thumbnails.gsub!(/~~colspan~~/, '')
827                         end
828                         if type == 'image'
829                             index = images.index(file)
830                             html_thumbnails.gsub!(/~~image_iteration~~/,
831                                                   '<a href="image-' + size2js(sizeobj['name']) + '.html#current=' + fullscreen_images[sizeobj['name']][index] +
832                                                       '" name="' + fullscreen_images[sizeobj['name']][index] + '">' +
833                                                       img_element("#{dest_dir}/#{thumbnail_images[sizeobj['name']][index]}") + '</a>')
834                             html_thumbnails.gsub!(/~~caption_iteration~~/,
835                                                   find_caption_value(xmldir, images[index]) || utf8(images[index]))
836                             html_thumbnails.gsub!(/~~ifimage\?~~(.+?)~~fi~~/) { $1 }
837                             html_thumbnails.gsub!(/~~ifvideo\?~~(.+?)~~fi~~/, '')
838                         elsif type == 'video'
839                             index = videos.index(file)
840                             if File.exists?("#{dest_dir}/#{thumbnail_videos[sizeobj['name']][index]}")
841                                 html_thumbnails.gsub!(/~~image_iteration~~/,
842                                                       '<a href="' + videos[index] + '">' + img_element("#{dest_dir}/#{thumbnail_videos[sizeobj['name']][index]}") + '</a>')
843                             else
844                                 html_thumbnails.gsub!(/~~image_iteration~~/,
845                                                       '<a href="' + videos[index] + '">' + utf8(_("(no preview)")) + '</a>')
846                             end
847                             html_thumbnails.gsub!(/~~caption_iteration~~/,
848                                                   find_caption_value(xmldir, videos[index]) || utf8(videos[index]))
849                             html_thumbnails.gsub!(/~~ifimage\?~~(.+?)~~fi~~/, '')
850                             html_thumbnails.gsub!(/~~ifvideo\?~~(.+?)~~fi~~/) { $1 }
851                         end
852                     end
853                 end
854                 html_thumbnails += close_iterations(iterations)
855                 for i in html
856                     i.sub!(/~~thumbnails~~/, html_thumbnails)
857                     i.gsub!(/~~theme~~/, $theme)
858                     i.gsub!(/~~current_size~~/, sizeobj['name'])
859                     i.gsub!(/~~current_size_js~~/, size2js(sizeobj['name']))
860                 end
861                 ios = File.open("#{dest_dir}/thumbnails-#{size2js(sizeobj['name'])}.html", "w")
862                 ios.write(html)
863                 ios.close
864             end
865
866             info("finished processing sizes")
867
868             #- generate "main" thumbnails.html page that will reload to correct size thanks to cookie
869             ios = File.open("#{dest_dir}/thumbnails.html", "w")
870             ios.write(html_reload_to_thumbnails)
871             ios.close
872
873             #- generate image.html (page with fullscreen images)
874             if images.size > 0
875                 captions4js = find_captions(xmldir, images).collect { |e| e ? '"' + e.gsub('"', '\"') + '"' : '""' }.join(', ')
876
877                 for sizeobj in $images_size
878                     html = $html_images.collect { |l| l.clone }
879                     images4js = fullscreen_images[sizeobj['name']].collect { |e| "\"#{e}\"" }.join(', ')
880                     otherimages4js = ''
881                     othersizes = []
882                     for sizeobj2 in all_images_sizes
883                         if sizeobj != sizeobj2
884                             otherimages4js += "var images_#{size2js(sizeobj2['name'])} = new Array(" + fullscreen_images[sizeobj2['name']].collect { |e| "\"#{e}\"" }.join(', ') + ")\n"
885                             othersizes << "\"#{size2js(sizeobj2['name'])}\""
886                         end
887                     end
888                     for i in html
889                         i.gsub!(/~~images~~/, images4js)
890                         i.gsub!(/~~other_images~~/, otherimages4js)
891                         i.gsub!(/~~other_sizes~~/, othersizes.join(', '))
892                         i.gsub!(/~~captions~~/, captions4js)
893                         i.gsub!(/~~title~~/, xmldir.attributes['thumbnails-caption'] || utf8(File.basename(dir)))
894                         i.gsub!(/~~thumbnails~~/, '<a href="thumbnails-' + size2js(sizeobj['name']) + '.html" id="thumbnails">' + utf8(_('return to thumbnails')) + '</a>')
895                         i.gsub!(/~~theme~~/, $theme)
896                         i.gsub!(/~~current_size~~/, size2js(sizeobj['name']))
897                         substitute_html_sizes(i, sizeobj, 'image')
898                     end
899                     ios = File.open("#{dest_dir}/image-#{size2js(sizeobj['name'])}.html", "w")
900                     ios.write(html)
901                     ios.close
902                 end
903             end
904         end
905     }
906
907     msg 3, ''
908
909     #- add attributes to <dir..> elements needing so
910     if $mode != 'use_config'
911         msg 3, _("\tfixating configuration file...")
912         $xmldoc.elements.each('//dir') { |element|
913             path = captionpath = element.attributes['path']
914             descendant_element = element.elements['descendant::image'] || element.elements['descendant::video']
915             if !descendant_element
916                 msg 3, _("\t\tremoving %s, no element in it") % path
917                 element.remove  #- means we have a directory with nothing interesting in it
918             else
919                 captionfile = "#{descendant_element.parent.attributes['path']}/#{descendant_element.attributes['filename']}"
920                 basename = File.basename(path)
921                 if element.elements['dir']
922                     if !element.attributes['subdirs-caption']
923                         element.add_attribute('subdirs-caption', basename)
924                     end
925                     if !element.attributes['subdirs-captionfile']
926                         element.add_attribute('subdirs-captionfile', captionfile)
927                     end
928                 end
929                 if element.child_byname_notattr('image', 'deleted') || element.child_byname_notattr('video', 'deleted')
930                     if !element.attributes['thumbnails-caption']
931                         element.add_attribute('thumbnails-caption', basename)
932                     end
933                     if !element.attributes['thumbnails-captionfile']
934                         element.add_attribute('thumbnails-captionfile', captionfile)
935                     end
936                 end
937             end
938         }
939     end
940
941     #- write down to disk config if necessary
942     if $config_writeto
943         ios = File.open($config_writeto, "w")
944         $xmldoc.write(ios, 0)
945         ios.close
946     end
947
948     if $forgui
949         msg 3, _(" completed necessary stuff for GUI, exiting.")
950         return
951     end
952
953     #- second pass to create index.html files and previous/next links
954     info("creating index.html")
955     msg 3, _("\trescanning directories to generate all `index.html' files...")
956
957     #- recompute the memoization because elements mights have been removed (the ones with no element in them)
958     optxpath = {}
959     $xmldoc.elements.each('//dir') { |elem|
960         optxpath[elem.attributes['path']] = elem
961     }
962
963     examined_dirs.each { |dir|
964         dir.chomp!
965         info("index.html: #{dir}|#{$source}")
966
967         xmldir = optxpath[utf8(dir)]
968         if !xmldir || (xmldir.attributes['already-generated'] && !$force) || xmldir.attributes['deleted']
969             next
970         end
971         dest_dir = make_dest_filename(dir.sub(/^#{Regexp.quote($source)}/, $dest))
972
973         previous_album = find_previous_album(xmldir)
974         next_album = find_next_album(xmldir)
975
976         if xmldir.elements['dir']
977             html = $html_index.collect { |l| l.clone }
978             iterations = {}
979             for i in html
980                 caption = xmldir.attributes['subdirs-caption']
981                 i.gsub!(/~~title~~/, caption)
982                 if xmldir.parent.name == 'dir'
983                     nav = ''
984                     path = '..'
985                     parent = xmldir.parent
986                     while parent.name == 'dir'
987                         parentcaption = parent.attributes['subdirs-caption']
988                         nav = "<a href='#{path}/index.html'>#{parentcaption}</a> #{utf8(_(" > "))} #{nav}"
989                         path += '/..'
990                         parent = parent.parent
991                     end
992                     i.gsub!(/~~ifnavigation\?~~(.+?)~~fi~~/) { $1 }
993                     i.gsub!(/~~navigation~~/, nav + caption)
994                 else
995                     i.gsub!(/~~ifnavigation\?~~(.+?)~~fi~~/, '')
996                 end
997                 discover_iterations(iterations, i)
998             end
999             
1000             html_index = ''
1001             reset_iterations(iterations)
1002             
1003             #- deal with "current" album (directs to "thumbnails" page)
1004             if xmldir.attributes['thumbnails-caption']
1005                 thumbnail = "#{dest_dir}/thumbnails-thumbnail.jpg"
1006                 gen_thumbnails_subdir(from_utf8(xmldir.attributes['thumbnails-captionfile']), xmldir, false,
1007                                       [ { 'filename' => thumbnail, 'size' => $albums_thumbnail_size } ], 'thumbnails')
1008                 html_index += run_iterations(iterations, 1)
1009                 html_index.gsub!(/~~image_iteration~~/, "<a href='thumbnails.html'>" + img_element(thumbnail) + '</a>')
1010                 html_index.gsub!(/~~caption_iteration~~/, xmldir.attributes['thumbnails-caption'])
1011             end
1012
1013             #- deal with sub-albums (direct to subdirs/index.html pages)
1014             xmldir.elements.each('dir') { |child|
1015                 if child.attributes['deleted']
1016                     next
1017                 end
1018                 subdir = make_dest_filename(from_utf8(File.basename(child.attributes['path'])))
1019                 thumbnail = "#{dest_dir}/thumbnails-#{subdir}.jpg"
1020                 html_index += run_iterations(iterations, 1)
1021                 captionfile, caption = find_subalbum_caption_info(child)
1022                 gen_thumbnails_subdir(captionfile, child, false,
1023                                       [ { 'filename' => thumbnail, 'size' => $albums_thumbnail_size } ], find_subalbum_info_type(child))
1024                 html_index.gsub!(/~~caption_iteration~~/, caption)
1025                 html_index.gsub!(/~~image_iteration~~/, "<a href='#{make_dest_filename(subdir)}/index.html'>" + img_element(thumbnail) + '</a>')
1026             }
1027
1028             html_index += close_iterations(iterations)
1029
1030             for i in html
1031                 i.gsub!(/~~thumbnails~~/, html_index)
1032             end
1033             
1034         else
1035             html = html_reload_to_thumbnails
1036         end
1037
1038         ios = File.open("#{dest_dir}/index.html", "w")
1039         ios.write(html)
1040         ios.close
1041
1042         #- substitute "return to albums" and previous/next correctly
1043         if xmldir.child_byname_notattr('image', 'deleted') || xmldir.child_byname_notattr('video', 'deleted')
1044             for sizeobj in $images_size
1045                 substInFile("#{dest_dir}/thumbnails-#{size2js(sizeobj['name'])}.html") { |line|
1046                     sub_previous_next_album(previous_album, next_album, line)
1047                     if xmldir.elements['dir']
1048                         line.sub!(/~~return_to_albums~~/, '<a href="index.html">' + utf8(_('return to albums')) + '</a>')
1049                     else
1050                         if xmldir.parent.name == 'dir'
1051                             line.sub!(/~~return_to_albums~~/, '<a href="../index.html">' + utf8(_('return to albums')) + '</a>')
1052                         else
1053                             line.sub!(/~~return_to_albums~~/, '')
1054                         end
1055                     end
1056                     line
1057                 }
1058                 if xmldir.child_byname_notattr('image', 'deleted')
1059                     substInFile("#{dest_dir}/image-#{size2js(sizeobj['name'])}.html") { |line|
1060                         sub_previous_next_album(previous_album, next_album, line)
1061                     }
1062                 end
1063             end
1064         end
1065     }
1066
1067     msg 3, _(" all done.")
1068 end
1069
1070 handle_options
1071 read_config
1072 check_installation
1073
1074 build_html_skeletons
1075
1076 walk_source_dir