CVS is history
[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) 2004-2009 Guillaume Cottenceau <http://zarb.org/~gc/resource/gc_mail.png>
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
22 require 'getoptlong'
23 require 'gettext'
24 require 'gettext/locale'
25 include GetText
26 require 'booh/rexml/document'
27 include REXML
28
29 require 'booh/booh-lib'
30 require 'booh/html-merges'
31
32 #- bind text domain as soon as possible because some _() functions are called early to build data structures
33 bindtextdomain("booh")
34 #- save locale for restoring for multi languages
35 $default_locale = Locale.get
36
37 #- options
38 $options = [
39     [ '--help',          '-h', GetoptLong::NO_ARGUMENT, _("Get help message") ],
40     [ '--version',       '-V', GetoptLong::NO_ARGUMENT, _("Print version and exit") ],
41
42     [ '--source',        '-s', GetoptLong::REQUIRED_ARGUMENT, _("Directory which contains original photos/videos as files or subdirs") ],
43     [ '--destination',   '-d', GetoptLong::REQUIRED_ARGUMENT, _("Directory which will contain the web-album; if it already exits, then all existing files and directories inside it will be removed!") ],
44
45     [ '--theme',         '-t', GetoptLong::REQUIRED_ARGUMENT, _("Select HTML theme to use") ],
46     [ '--config',        '-C', GetoptLong::REQUIRED_ARGUMENT, _("File containing config listing photos and videos within directories with captions") ],
47     [ '--config-skel',   '-k', GetoptLong::REQUIRED_ARGUMENT, _("Filename where the script will output a config skeleton") ],
48     [ '--merge-config',  '-M', GetoptLong::REQUIRED_ARGUMENT, _("File containing config listing, where to merge new/removed photos/videos from --source, and change theme info") ],
49     [ '--merge-config-onedir',  '-O', GetoptLong::REQUIRED_ARGUMENT, _("File containing config listing, for merging the subdir specified with --dir") ],
50     [ '--merge-config-subdirs', '-U', GetoptLong::REQUIRED_ARGUMENT, _("File containing config listing, for merging the new subdirs down the subdir specified with --dir") ],
51     [ '--dir',           '-D', GetoptLong::REQUIRED_ARGUMENT, _("Directory for merge with --merge-config-onedir or --merge-config-subdirs") ],
52     [ '--use-config',    '-u', GetoptLong::REQUIRED_ARGUMENT, _("File containing config listing, where to change theme info") ],
53     [ '--force',         '-f', GetoptLong::NO_ARGUMENT, _("Force generation of album even if the GUI marked some directories as already generated") ],
54
55     [ '--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)") ],
56     [ '--multi-languages', '-L', GetoptLong::REQUIRED_ARGUMENT, _("Specify the list of languages to support (uses Apache MultiViews); this is a comma-separated list of supported languages, with last element used as the fallback language; for example: 'fr,eo,en,en'; supported languages: %s") % SUPPORTED_LANGUAGES.join(', ') ],
57     [ '--thumbnails-per-row', '-T', GetoptLong::REQUIRED_ARGUMENT, _("Specify the amount of thumbnails per row in the thumbnails page (if applicable in theme)") ],
58     [ '--thumbnails-per-page', '-p', GetoptLong::REQUIRED_ARGUMENT, _("Specify the amount of thumbnails per page in the thumbnails page, after which split occurs") ],
59     [ '--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 photos from point-and-shoot cameras - also called compact cameras - is 4/3, whereas photos from SLR cameras - also called reflex cameras - is 3/2)") ],
60     [ '--transcode-videos', '-r', GetoptLong::REQUIRED_ARGUMENT, _("Transcode videos with given external program; %f is the placeholder for the input video, %o for the output video; before the external program, the output video extension should be given followed by a colon") ],
61     [ '--index-link',    '-l', GetoptLong::REQUIRED_ARGUMENT, _("Specify the HTML markup to use on the bottom of pages for a small link returning to wherever you see fit in your website (or somewhere else)") ],
62     [ '--made-with',     '-n', GetoptLong::REQUIRED_ARGUMENT, _("Specify the HTML markup to use on the bottom of pages for a small 'made with' message") ],
63     [ '--comments-format','-c', GetoptLong::REQUIRED_ARGUMENT, _("Specify comments format to use for images instead of only filename when creating new albums; use ImageMagick's format") ],
64
65     [ '--mproc',         '-m', GetoptLong::REQUIRED_ARGUMENT, _("Specify the number of processors for multi-processors machines") ],
66
67     [ '--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)") ],
68
69     [ '--verbose-level', '-v', GetoptLong::REQUIRED_ARGUMENT, _("Set max verbosity level (0: errors, 1: warnings, 2: important messages, 3: other messages)") ],
70     [ '--info-pipe',     '-i', GetoptLong::REQUIRED_ARGUMENT, _("Name a file where to write information about what's going on (used by the GUI)") ],
71 ]
72
73 #- default values for some globals 
74 $switches = []
75 $stdout.sync = true
76 $no_identify = false
77 $ignore_videos = false
78 $forgui = false
79 $hardlinks_ok = true
80
81 def usage
82     puts _("Usage: %s [OPTION]...") % File.basename($0)
83     $options.each { |ary|
84         printf " %3s, %-18s %s\n", ary[1], ary[0], ary[3]
85     }
86 end
87
88 def handle_options
89     parser = GetoptLong.new
90     parser.set_options(*$options.collect { |ary| ary[0..2] })
91     begin
92         parser.each_option do |name, arg|
93             case name
94             when '--help'
95                 usage
96                 exit(0)
97
98             when '--version'
99                 puts _("Booh version %s
100
101 Copyright (c) 2005-2009 Guillaume Cottenceau.
102 This is free software; see the source for copying conditions.  There is NO
103 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.") % $VERSION
104
105                 exit(0)
106
107             when '--source'
108                 $source = File.expand_path(arg.sub(%r|/$|, ''))
109                 if !File.directory?($source)
110                     die _("Argument to --source must be a directory")
111                 end
112             when '--destination'
113                 $dest = File.expand_path(arg.sub(%r|/$|, ''))
114                 if File.exists?($dest) && !File.directory?($dest)
115                     die _("If --destination exists, it must be a directory")
116                 end
117                 if $dest != make_dest_filename($dest)
118                     die _("Sorry, destination directory can't contain non simple alphanumeric characters.")
119                 end
120 #            when '--clean'
121 #                system("rm -rf #{$dest}")
122
123             when '--theme'
124                 $theme = arg
125             when '--config'
126                 arg = File.expand_path(arg)
127                 if File.readable?(arg)
128                     $xmldoc = REXML::Document.new File.new(arg)
129                     $mode = 'use_config'
130                 else
131                     die _('Config file does not exist or is unreadable.')
132                 end
133             when '--config-skel'
134                 arg = File.expand_path(arg)
135                 if File.exists?(arg)
136                     if File.directory?(arg)
137                         die _("Config skeleton file (%s) already exists and is a directory! Please change the filename.") % arg
138                     else
139                         msg 1, _("Config skeleton file already exists, backuping to %s.backup") % arg
140                         File.rename(arg, "#{arg}.backup")
141                     end
142                 end
143                 $config_writeto = arg
144                 $mode = 'gen_config'
145             when '--merge-config'
146                 arg = File.expand_path(arg)
147                 if File.readable?(arg)
148                     msg 2, _("Merge config notice: backuping current config file to %s.backup") % arg
149                     $xmldoc = REXML::Document.new File.new(arg)
150                     File.rename(arg, "#{arg}.backup")
151                     $config_writeto = arg
152                     $mode = 'merge_config'
153                 else
154                     die _('Config file does not exist or is unreadable.')
155                 end
156             when '--merge-config-onedir'
157                 arg = File.expand_path(arg)
158                 if File.readable?(arg)
159                     msg 2, _("Merge config notice: backuping current config file to %s.backup") % arg
160                     $xmldoc = REXML::Document.new File.new(arg)
161                     File.rename(arg, "#{arg}.backup")
162                     $config_writeto = arg
163                     $mode = 'merge_config_onedir'
164                 else
165                     die _('Config file does not exist or is unreadable.')
166                 end
167             when '--merge-config-subdirs'
168                 arg = File.expand_path(arg)
169                 if File.readable?(arg)
170                     msg 2, _("Merge config notice: backuping current config file to %s.backup") % arg
171                     $xmldoc = REXML::Document.new File.new(arg)
172                     File.rename(arg, "#{arg}.backup")
173                     $config_writeto = arg
174                     $mode = 'merge_config_subdirs'
175                 else
176                     die _('Config file does not exist or is unreadable.')
177                 end
178             when '--dir'
179                 arg = File.expand_path(arg)
180                 if !File.readable?(arg)
181                     die _('Specified directory to merge with --dir is not readable')
182                 else
183                     $onedir = arg
184                 end
185             when '--use-config'
186                 arg = File.expand_path(arg)
187                 if File.readable?(arg)
188                     msg 2, _("Use config notice: backuping current config file to %s.backup") % arg
189                     $xmldoc = REXML::Document.new File.new(arg)
190                     File.rename(arg, "#{arg}.backup")
191                     $config_writeto = arg
192                     $mode = 'use_config_changetheme'
193                 else
194                     die _('Config file does not exist or is unreadable.')
195                 end
196
197             when '--sizes'
198                 $limit_sizes = arg
199
200             when '--multi-languages'
201                 parts = arg.split(',')
202                 if parts.size == 0 || parts.find_all { |e| ! SUPPORTED_LANGUAGES.include?(e.strip) }.size > 0
203                     die _("--multi-languages: argument must be a comma-separated list of supported languages, with last element used as the fallback language; for example: 'fr,eo,en,en'; supported languages: %s") % SUPPORTED_LANGUAGES.join(', ')
204                 end
205                 $multi_languages = [ parts[0..-2], parts[-1] ]
206
207             when '--thumbnails-per-row'
208                 $N_per_row = arg
209
210             when '--thumbnails-per-page'
211                 $N_per_page = arg
212
213             when '--optimize-for-32'
214                 $optimize_for_32 = true
215
216             when '--transcode-videos'
217                 parts = arg.split(':', 2)
218                 if parts.size != 2 || parts[0] =~ / / || arg !~ /%f/ || arg !~ /%o/
219                     die _("--transcode-videos: argument must be the external program for transcoding, and contain %f for the input video, %o for the output video, and before the external program, the output video extension should be given followed by a colon")
220                 end
221                 $transcode_videos = arg
222
223             when '--made-with'
224                 $madewith = arg
225
226             when '--index-link'
227                 $indexlink = arg
228
229             when '--comments-format'
230                 $commentsformat = arg
231
232             when '--force'
233                 $force = true
234
235             when '--mproc'
236                 $mproc = arg.to_i
237                 $pids = []
238
239             when '--for-gui'
240                 $forgui = true
241
242             when '--verbose-level'
243                 $verbose_level = arg.to_i
244
245             when '--info-pipe'
246                 $info_pipe = File.open(arg, File::WRONLY)
247                 $info_pipe.sync = true
248             end
249         end
250     rescue
251         puts $!
252         usage
253         exit(1)
254     end
255
256     if !$source && $xmldoc
257         $source = from_utf8($xmldoc.root.attributes['source']).sub(%r|/$|, '')
258         $dest = from_utf8($xmldoc.root.attributes['destination']).sub(%r|/$|, '')
259         $theme ||= $xmldoc.root.attributes['theme']
260         $limit_sizes ||= $xmldoc.root.attributes['limit-sizes']
261         if $mode == 'use_config' || $mode =~ /^merge_config/
262             $optimize_for_32 = !$xmldoc.root.attributes['optimize-for-32'].nil?
263             $N_per_row = $xmldoc.root.attributes['thumbnails-per-row']
264             languages = $xmldoc.root.attributes['multi-languages']
265             if languages
266                 languages = languages.split(',')
267                 $multi_languages = [ languages[0..-2], languages[-1] ]
268             end
269             $N_per_page = $xmldoc.root.attributes['thumbnails-per-page']
270             $madewith = $xmldoc.root.attributes['made-with']
271             $indexlink = $xmldoc.root.attributes['index-link']
272         end
273     end
274
275     if $mode == 'merge_config_onedir' && !$onedir
276         die _("Missing --dir for --merge-config-onedir")
277     end
278     if $mode == 'merge_config_subdirs' && !$onedir
279         die _("Missing --dir for --merge-config-subdirs")
280     end
281
282     if !$source
283         usage
284         exit(0)
285     end
286     if !$dest
287         die _("Missing --destination parameter.")
288     end
289     if !$theme
290         $theme = 'simple'
291     end
292
293     select_theme($theme, $limit_sizes, $optimize_for_32, $N_per_row)
294
295     if !$xmldoc
296         $xmldoc = Document.new "<booh/>"
297         $xmldoc << XMLDecl.new(XMLDecl::DEFAULT_VERSION, $CURRENT_CHARSET)
298         $xmldoc.root.add_attribute('version', $VERSION)
299         $xmldoc.root.add_attribute('source', $source)
300         $xmldoc.root.add_attribute('destination', $dest)
301         $xmldoc.root.add_attribute('theme', $theme)
302         if $limit_sizes
303             $xmldoc.root.add_attribute('limit-sizes', $limit_sizes)
304         end
305         if $multi_languages
306             $xmldoc.root.add_attribute('multi-languages', $multi_languages[0].join(',') + ',' + $multi_languages[1])
307         end
308         if $optimize_for_32
309             $xmldoc.root.add_attribute('optimize-for-32', 'true')
310         end
311         if $N_per_row
312             $xmldoc.root.add_attribute('thumbnails-per-row', $N_per_row)
313         end
314         if $N_per_page
315             $xmldoc.root.add_attribute('thumbnails-per-page', $N_per_page)
316         end
317         if $madewith
318             $xmldoc.root.add_attribute('made-with', $madewith)
319         end
320         if $indexlink
321             $xmldoc.root.add_attribute('index-link', $indexlink)
322         end
323         $mode = 'gen_config'
324     end
325
326     if $mode == 'merge_config' || $mode == 'use_config_changetheme'
327         $xmldoc.root.add_attribute('theme', $theme)
328         $xmldoc.root.add_attribute('version', $VERSION)
329         if $limit_sizes
330             $xmldoc.root.add_attribute('limit-sizes', $limit_sizes)
331         else
332             $xmldoc.root.delete_attribute('limit-sizes')
333         end
334         if $multi_languages
335             $xmldoc.root.add_attribute('multi-languages', $multi_languages[0].join(',') + ',' + $multi_languages[1])
336         else
337             $xmldoc.root.delete_attribute('multi-languages')
338         end
339
340         if $optimize_for_32
341             $xmldoc.root.add_attribute('optimize-for-32', 'true')
342         else
343             $xmldoc.root.delete_attribute('optimize-for-32')
344         end
345         if $N_per_row
346             $xmldoc.root.add_attribute('thumbnails-per-row', $N_per_row)
347         else
348             $xmldoc.root.delete_attribute('thumbnails-per-row')
349         end
350         if $N_per_page
351             $xmldoc.root.add_attribute('thumbnails-per-page', $N_per_page)
352         else
353             $xmldoc.root.delete_attribute('thumbnails-per-page')
354         end
355         if $madewith
356             $xmldoc.root.add_attribute('made-with', $madewith)
357         else
358             $xmldoc.root.delete_attribute('made-with')
359         end
360         if $indexlink
361             $xmldoc.root.add_attribute('index-link', $indexlink)
362         else
363             $xmldoc.root.delete_attribute('index-link')
364         end
365     end
366
367     if $transcode_videos
368         $xmldoc.root.add_attribute('transcode-videos', $transcode_videos)
369     else
370         $xmldoc.root.delete_attribute('transcode-videos')
371     end
372
373     if $madewith
374         $madewith = $madewith.gsub('%booh', '"http://booh.org/"')
375     end
376     if $multi_languages
377         $htmlsuffix = ''
378     else
379         $htmlsuffix = '.html'
380     end
381 end
382
383 def read_config
384     $config = {}
385 end
386
387 def write_config
388 end
389
390 def info(value)
391     if $info_pipe
392         $info_pipe.puts(value)
393     end
394 end
395
396 def die(value)
397     if $info_pipe
398         $info_pipe.puts("die: " + value)
399     end
400     die_ value
401 end
402
403 def check_installation
404     if !system("which convert >/dev/null 2>/dev/null")
405         die _("The program 'convert' is needed. Please install it. 
406 It is generally available with the 'ImageMagick' software package.")
407     end
408     if !system("which identify >/dev/null 2>/dev/null")
409         msg 1, _("the program 'identify' is needed to get images sizes and EXIF data. Please install it.
410 It is generally available with the 'ImageMagick' software package.")
411         $no_identify = true
412     end
413     missing = %w(mplayer).delete_if { |prg| system("which #{prg} >/dev/null 2>/dev/null") }
414     if missing != []
415         msg 1, _("the following program(s) are needed to handle videos: '%s'. Videos will be ignored.") % missing.join(', ')
416         $ignore_videos = true
417     end
418 end
419
420 def replace_line(surround, keyword, line)
421     begin
422         contents = eval "$#{keyword}"
423         line.sub!(/#{surround}#{keyword}#{surround}/, contents)
424     rescue TypeError
425         die _("No '%s' found for substitution") % keyword
426     end
427 end
428
429 def build_html_skeletons
430     $html_images     = File.open("#{$themedir}/skeleton_image.html").readlines
431     $html_thumbnails = File.open("#{$themedir}/skeleton_thumbnails.html").readlines
432     $html_index      = File.open("#{$themedir}/skeleton_index.html").readlines
433     for line in $html_images + $html_thumbnails + $html_index
434         while line =~ /~~~(\w+)~~~/
435             replace_line('~~~', $1, line)
436         end
437     end
438 end
439
440 def find_caption_value(xmldir, filename)
441     if cap = xmldir.elements["*[@filename='#{utf8(filename)}']"].attributes['caption']
442         return cap.gsub("\n", '<br/>')
443     else
444         return nil
445     end
446 end
447
448 def find_captions(xmldir, images)
449     return images.collect { |img| find_caption_value(xmldir, img) }
450 end
451
452 #- stolen from CVSspam
453 def urlencode(text)
454   text.sub(/[^a-zA-Z0-9\-,.*_\/]/) do
455     "%#{sprintf('%2X', $&[0])}"
456   end
457 end
458
459 def all_images_sizes
460     return $limit_sizes =~ /original/ ? $images_size + [ { 'name' => 'original' } ] : $images_size
461 end
462
463 def html_reload_to_thumbnails
464     html_reload_to_thumbnails = $preferred_size_reloader.clone
465     html_reload_to_thumbnails.gsub!(/~~theme~~/, $theme)
466     html_reload_to_thumbnails.gsub!(/~~default_size~~/, size2js($default_size['name']))
467     html_reload_to_thumbnails.gsub!(/~~htmlsuffix~~/, $htmlsuffix)
468     html_reload_to_thumbnails.gsub!(/~~all_sizes~~/, all_images_sizes.collect { |s| "\"#{size2js(s['name'])}\"" }.join(', '))
469     size_auto_chooser = '';
470     all_images_sizes.find_all { |s| s.has_key?('optimizedforwidth') }.
471                      sort { |a,b| b['optimizedforwidth'].to_i <=> a['optimizedforwidth'].to_i }.
472                      each { |s| size_auto_chooser += "if (w + 50 > #{s['optimizedforwidth']}) { return 'thumbnails-#{size2js(s['name'])}-0#{$htmlsuffix}'; }\n" }
473     html_reload_to_thumbnails.gsub!(/~~size_auto_chooser~~/, size_auto_chooser)
474     return html_reload_to_thumbnails
475 end
476
477 def discover_iterations(iterations, line)
478     if line =~ /~~iterate(\d)_open(_max(\d+|N))?~~/
479         for iter in iterations.values
480             if iter['open']
481                 iter['open'] = false
482                 iter['close_wait'] = $1.to_i
483             end
484         end
485         max = $3 == 'N' ? ($N_per_row || $default_N) : $3
486         iterations[$1.to_i] = { 'open' => true, 'max' => max, 'opening' => '', 'closing' => '' }
487         if $1.to_i == 1
488             line.sub!(/.*/, '~~thumbnails~~')
489         else
490             line.sub!(/.*/, '')
491         end
492     elsif line =~ /~~iterate(\d)_close~~/
493         iterations[$1.to_i]['open']  = false;
494         iterations[$1.to_i]['close'] = true;
495         line.sub!(/.*/, '')
496     else
497         for iter in iterations.values
498             if iter['open']
499                 iter['opening'] += line
500                 line.sub!(/.*/, '')
501             end
502             if !iter['close'] && iter['close_wait'] && iterations[iter['close_wait']]['close']
503                 iter['closing'] += line
504                 line.sub!(/.*/, '')
505             end
506         end
507     end
508 end
509
510 def reset_iterations(iterations)
511     for iter in iterations.values
512         iter['value'] = 0
513     end
514 end
515
516 def run_iterations(iterations, amount)
517     html = ''
518     should_rerun = false
519     for level in iterations.keys.sort
520         if iterations[level]['value'] == 0
521             html += iterations[level]['opening']
522         elsif level == iterations.keys.max
523             if !iterations[level]['max'] || iterations[level]['max'] && iterations[level]['value'] + amount <= iterations[level]['max'].to_i
524                 html += iterations[level]['opening']
525             else
526                 should_rerun = true
527             end
528         end
529         iterations[level]['value'] += amount
530         if iterations[level]['max'] && iterations[level]['value'] > iterations[level]['max'].to_i
531             iterations[level]['value'] = 0
532             iterations[level-1]['value'] = 0
533             html += iterations[level-1]['closing']
534         end
535     end
536     if should_rerun
537         return html + run_iterations(iterations, amount)
538     else
539         return html
540     end
541 end
542
543 def close_iterations(iterations)
544     html = ''
545     for level in iterations.keys.sort.reverse
546         html += iterations[level]['closing']
547     end
548     return html
549 end
550
551 def img_element(fullpath)
552     if size = get_image_size(fullpath)
553         sizespec = 'width="' + size[:x].to_s + '" height="' + size[:y].to_s + '"'
554     else
555         sizespec = ''
556     end
557     return '<img src="' + File.basename(fullpath) + '" ' + sizespec + ' alt="image"/>'
558 end
559
560 def size2js(name)
561     return name.gsub(/-/, '')
562 end
563
564 def substitute_html_sizes(html, sizeobj, type, suffix)
565     sizestrings = []
566     if $images_size.length > 1 || (type == 'image' && $limit_sizes =~ /original/)
567         for sizeobj2 in $images_size
568             sizejs = size2js(sizeobj2['name'])
569             sizen = defer_translation(sizename(sizeobj2['name'], false))
570             if sizeobj != sizeobj2
571                 if type == 'thumbnails'
572                     sizestrings << '<a href="thumbnails-' + sizejs + suffix + $htmlsuffix + '" onclick="set_preferred_size(\'' + sizejs + '\')">' + sizen + '</a>'
573                 else
574                     sizestrings << '<a id="link' + sizejs + '" onclick="set_preferred_size(\'' + sizejs + '\')">' + sizen + '</a>'
575                 end
576             else
577                 sizestrings << sizen
578             end
579         end
580         if type == 'image' && $limit_sizes =~ /original/
581             sizestrings << '<a id="linkoriginal" target="newframe">' + defer_translation(sizename('original', false)) + '</a>'
582         end
583     end
584     html.sub!(/~~sizes~~(.+)~~/) { sizestrings.join($1) }
585 end
586
587 def substitute_navigation(html, xmldir)
588     if xmldir.parent.name == 'dir'
589         nav = ''
590         path = '..'
591         parent = xmldir.parent
592         while parent.name == 'dir'
593             parentcaption = parent.attributes['subdirs-caption'] || File.basename(parent.attributes['path'])
594             nav = "<a href=\"#{path}/index#{$htmlsuffix}\">#{parentcaption}</a> #{defer_translation(N_(" > "))} #{nav}"
595             path += '/..'
596             parent = parent.parent
597         end
598         html.gsub!(/~~ifnavigation\?~~(.+?)~~fi~~/) { $1 }
599         html.gsub!(/~~navigation~~/, nav + (xmldir.attributes['subdirs-caption'] || File.basename(xmldir.attributes['path'])))
600     else
601         html.gsub!(/~~ifnavigation\?~~(.+?)~~fi~~/, '')
602     end
603 end
604
605 def substitute_pathtobase(html, xmldir)
606     path = ''
607     location = xmldir
608     while location.parent.name == 'dir'
609         path = '../' + path
610         location = location.parent
611     end
612     html.gsub!(/~~pathtobase~~/, path)
613 end
614
615 def xmldir2destdir(xmldir)
616     return make_dest_filename(from_utf8(File.basename(xmldir.attributes['path'])))
617 end
618
619 def find_previous_album(xmldir)
620     relative_pos = ''
621     begin
622         #- move to previous dir element if exists
623         if prevelem = xmldir.previous_element_byname_notattr('dir', 'deleted')
624             xmldir = prevelem
625             relative_pos += '../' + xmldir2destdir(xmldir) + '/'
626             child = nil
627             #- after having moved to previous dir, we need to go down last subdir until the last one
628             while child = xmldir.elements['dir']
629                 while nextchild = child.next_element_byname_notattr('dir', 'deleted')
630                     child = nextchild
631                 end
632                 relative_pos += xmldir2destdir(child) + '/'
633                 xmldir = child
634             end
635         else
636             #- previous dir doesn't exist, move to previous dir element if exists
637             xmldir = xmldir.parent
638             if xmldir.name == 'dir' && !xmldir.attributes['deleted']
639                 relative_pos += '../'
640             else
641                 return nil
642             end
643         end
644     end while !xmldir.child_byname_notattr('image', 'deleted') && !xmldir.child_byname_notattr('video', 'deleted')
645     return File.reduce_path(relative_pos)
646 end
647
648 def find_next_album(xmldir)
649     relative_pos = ''
650     begin
651         #- first child dir element (catches when initial xmldir has both thumbnails and subdirs)
652         if firstchild = xmldir.child_byname_notattr('dir', 'deleted')
653             xmldir = firstchild
654             relative_pos += xmldir2destdir(xmldir) + '/'
655         #- next brother
656         elsif nextbro = xmldir.next_element_byname_notattr('dir', 'deleted')
657             xmldir = nextbro
658             relative_pos += '../' + xmldir2destdir(xmldir) + '/'
659         else
660             #- go up until we have a next brother or we are finished
661             begin
662                 xmldir = xmldir.parent
663                 relative_pos += '../'
664             end while xmldir && !xmldir.next_element_byname_notattr('dir', 'deleted')
665             if xmldir
666                 xmldir = xmldir.next_element_byname('dir')
667                 relative_pos += '../' + xmldir2destdir(xmldir) + '/'
668             else
669                 return nil
670             end
671         end
672     end while !xmldir.child_byname_notattr('image', 'deleted') && !xmldir.child_byname_notattr('video', 'deleted')
673     return File.reduce_path(relative_pos)
674 end
675
676 def find_translation_for_file(file, msg)
677     if $multi_languages
678         if file =~ /\.(\w\w)\.html$/
679             bindtextdomain("booh", { :locale => "#{$1}.UTF-8" })
680             retval = _(msg)
681             Locale.set_current($default_locale)
682             return retval
683         else
684             die "Internal error: cannot find multi language suffix of file '#{file}'"
685         end
686     else
687         return utf8(_(msg))
688     end
689 end
690
691 def sub_previous_next_album(file, previous_album, next_album, html, previous_album_msg, next_album_msg)
692     if previous_album
693         html.gsub!(/~~previous_album~~/, '<a href="' + previous_album + 'thumbnails' + $htmlsuffix + '">' + previous_album_msg + '</a>')
694         html.gsub!(/~~ifprevious_album\?~~(.+?)~~fi~~/) { $1 }
695     else
696         html.gsub!(/~~previous_album~~/, '')
697         html.gsub!(/~~ifprevious_album\?~~(.+?)~~fi~~/, '')
698     end
699     if next_album
700         html.gsub!(/~~next_album~~/, '<a href="' + next_album + 'thumbnails' + $htmlsuffix + '">' + next_album_msg + '</a>')
701         html.gsub!(/~~ifnext_album\?~~(.+?)~~fi~~/) { $1 }
702     else
703         html.gsub!(/~~next_album~~/, '')
704         html.gsub!(/~~ifnext_album\?~~(.+?)~~fi~~/, '')
705     end
706     return html
707 end
708
709 def save_html(html, base_filename)
710     if html.class == Array
711         html = html.join('')
712     end
713     if $multi_languages
714         for language in ($multi_languages[0] + [ $multi_languages[1] ]).uniq
715             bindtextdomain("booh", { :locale => "#{language}.UTF-8" })
716             ios = File.open("#{base_filename}.#{language}.html", "w")
717             ios.write(html.gsub(/@@(.*?)@@/) { _($1) })
718             ios.close
719             Locale.set_current($default_locale)
720         end
721     else
722         ios = File.open("#{base_filename}.html", "w")
723         ios.write(html.gsub(/@@(.*?)@@/) { utf8(_($1)) })
724         ios.close
725     end
726 end
727
728 def walk_source_dir
729
730     #- preprocess the path->dir, rexml is very slow with that; we seem to improve speed by 7%
731     optxpath = {}
732     $xmldoc.elements.each('//dir') { |elem|
733         optxpath[elem.attributes['path']] = elem
734     }
735
736     examined_dirs = nil
737     if $mode == 'merge_config_onedir'
738         examined_dirs = [ $onedir ]
739     elsif $mode == 'merge_config_subdirs'
740         examined_dirs = `find '#{$onedir}' -type d -follow`.sort.collect { |v| v.chomp }.delete_if { |v| optxpath.has_key?(utf8(v)) }
741     else
742         examined_dirs = `find '#{$source}' -type d -follow`.sort.collect { |v| v.chomp }
743         if $mode == 'merge_config'
744             $xmldoc.elements.each('//dir') { |elem|
745                 if ! examined_dirs.include?(elem.attributes['path'])
746                     msg 2, _("Merging config: removing directory %s from config, isn't on filesystem anymore") % elem.attributes['path']
747                     elem.remove
748                 end
749             }
750         end
751     end
752     info("directories: #{examined_dirs.length}, sizes: #{$images_size.length}")
753
754     examined_dirs.each { |dir|
755         if dir =~ /'/
756             die _("Source directory or sub-directories can't contain a single-quote character, sorry: %s") % dir
757         end
758         if $mode !~ /^use_config/
759             Dir.entries(dir).each { |file|
760                 if file =~ /['"\[\]]/
761                     die _("Files can't contain any of the characters ', \", [ or ], sorry: %s") % "#{dir}/#{file}"
762                 end
763             }
764         end
765     }
766
767     examined_dirs.each { |dir|
768         if File.basename(dir) =~ /^\./
769             msg 1, _("Ignoring directory %s, begins with a dot (indicating a hidden directory)") % dir
770             next
771         end
772
773         dest_dir = make_dest_filename(dir.sub(/^#{Regexp.quote($source)}/, $dest))
774
775         #- place xml document on proper node if exists, else create
776         xmldir = optxpath[utf8(dir)]
777         if $mode == 'use_config' || $mode == 'use_config_changetheme'
778             if !xmldir || (xmldir.attributes['already-generated'] && !$force) || xmldir.attributes['deleted']
779                 info("walking: #{dir}|#{$source}, 0 elements")
780                 if xmldir && xmldir.attributes['deleted']
781                     system("rm -rf '#{dest_dir}'")
782                 end
783                 next
784             end
785         else
786             if $mode == 'gen_config' || (($mode == 'merge_config' || $mode == 'merge_config_subdirs') && !xmldir)
787                 #- add the <dir..> element if necessary
788                 parent = File.dirname(dir)
789                 xmldir = $xmldoc.elements["//dir[@path='#{utf8(parent)}']"]
790                 if !xmldir
791                     xmldir = $xmldoc.root
792                 end
793                 #- need to remove the already-generated mark of the parent because of the sub-albums page containing now one more element
794                 xmldir.delete_attribute('already-generated')
795                 xmldir = optxpath[utf8(dir)] = xmldir.add_element('dir', { 'path' => utf8(dir) })
796             end
797         end
798         xmldir.delete_attribute('already-generated')
799
800         #- preprocess all the existing elements, rexml is slow with that
801         optelements = {}
802         xmldir.elements.each { |elem|
803             if filename = elem.attributes['filename']
804                 optelements[elem.name + '|' + filename] = elem
805             end
806         }
807
808         #- read images/videos entries from config or from directories depending on mode
809         entries = []
810         if $mode == 'use_config' || $mode == 'use_config_changetheme'
811             msg 2, _("Handling %s from config list...") % dir
812             xmldir.elements.each { |element|
813                 if %w(image video).include?(element.name) && !element.attributes['deleted']
814                     entries << from_utf8(element.attributes['filename'])
815                 end
816             }
817         else
818             msg 2, _("Examining %s...") % dir
819             entries = Dir.entries(dir).sort
820             #- populate config in case of gen_config, add new files in case of merge_config
821             for file in entries
822                 if file =~ /['"\[\]]/
823                     msg 1, _("Ignoring %s, contains one of forbidden characters: '\"[]") % "#{dir}/#{file}"
824                 else
825                     type = entry2type(file)
826                     if type && ! optelements[type + '|' + utf8(file)]
827                         #- hack: don't run identify (which is slow) if format only contains default %t
828                         if $commentsformat && type == 'image' && $commentsformat != '%t'
829                             comment = utf8(`identify -format "#{$commentsformat}" '#{dir}/#{file}'`.chomp.sub(/\.$/, ''))
830                         else
831                             comment = utf8cut(file.sub(/\.[^\.]+$/, ''), 18)
832                         end
833                         optelements[type + '|' + utf8(file)] = xmldir.add_element(type, { "filename" => utf8(file), "caption" => comment })
834                     end
835                 end
836             end
837             if $mode != 'gen_config'
838                 #- cleanup removed files from config and reread entries from config to get proper ordering
839                 entries = []
840                 xmldir.elements.each { |element|
841                     fullpath = "#{dir}/#{from_utf8(element.attributes['filename'])}"
842                     if %w(image video).include?(element.name)
843                         if !File.readable?(fullpath)
844                             msg 1, _("Config merge: removing %s from config; use the backup file to retrieve caption info if this was a mistake") % fullpath
845                             xmldir.delete(element)
846                         elsif !element.attributes['deleted']
847                             entries << from_utf8(element.attributes['filename'])
848                         end
849                     end
850                 }
851                 #- if there is no more elements here, there is no album here anymore
852                 if !xmldir.child_byname_notattr('image', 'deleted') && !xmldir.child_byname_notattr('video', 'deleted')
853                     xmldir.delete_attribute('thumbnails-caption')
854                     xmldir.delete_attribute('thumbnails-captionfile')
855                 end
856             end
857         end
858         images = entries.find_all { |e| entry2type(e) == 'image' }
859         msg 3, _("\t%s photos") % images.length
860         videos = entries.find_all { |e| entry2type(e) == 'video' }
861         msg 3, _("\t%s videos") % videos.length
862         info("walking: #{dir}|#{$source}, #{images.length + videos.length} elements")
863
864         system("mkdir -p '#{dest_dir}'")
865
866         #- generate .htaccess file
867         if !$forgui
868             ios = File.open("#{dest_dir}/.htaccess", "w")
869             ios.write("AddCharset UTF-8 .html\n")
870             if auth_user_file = xmldir.attributes['password-protect']
871                 msg 3, _("\tgenerating password protection file #{dest_dir}/.htaccess")
872                 ios.write("AuthType Basic\nAuthName \"protected area\"\nAuthUserFile #{auth_user_file}\nrequire valid-user\n")
873             end
874             if $multi_languages
875                 ios.write("Options +Multiviews\n")
876                 ios.write("LanguagePriority #{$multi_languages[1]}\n")
877                 ios.write("ForceLanguagePriority Prefer Fallback\n")
878                 ios.write("DirectoryIndex index\n")
879             end
880             ios.close
881         end
882
883         #- pass through if there are no images and videos
884         if images.size == 0 && videos.size == 0
885             if !$forgui
886                 #- cleanup old images/videos, especially if this directory contained images/videos previously.
887                 if $mode != 'gen_config'
888                     rightful_images = [ '.htaccess' ]
889                     if xmldir.attributes['thumbnails-caption']
890                         rightful_images << 'thumbnails-thumbnail.jpg'
891                     end
892                     xmldir.elements.each('dir') { |child|
893                         if child.attributes['deleted']
894                             next
895                         end
896                         subdir = make_dest_filename(from_utf8(File.basename(child.attributes['path'])))
897                         rightful_images << "thumbnails-#{subdir}.jpg"
898                     }
899                     to_del = Dir.entries(dest_dir).find_all { |e| !File.directory?(File.join(dest_dir, e)) && !rightful_images.include?(e) }
900                     if to_del.size > 0
901                         File.delete(*to_del.collect { |e| File.join(dest_dir, e) })
902                     end
903                 end
904                 
905                 #- copy any resource file that goes with the theme (css, images..)
906                 themestuff = Dir.entries("#{$themedir}").
907                                  find_all { |e| !%w(. .. skeleton_image.html skeleton_thumbnails.html skeleton_index.html metadata root).include?(e) }
908                 themestuff.each { |entry|
909                     if !File.exists?(File.join(dest_dir, entry))
910                         psys("cp '#{$themedir}/#{entry}' '#{dest_dir}'")
911                     end
912                 }
913
914                 #- copy any root-only resource file that goes with the theme (css, images..)
915                 if xmldir.parent.name != 'dir'
916                     themestuff_root = Dir.entries("#{$themedir}/root").
917                                           find_all { |e| !%w(. ..).include?(e) }
918                     themestuff_root.each { |entry|
919                         if !File.exists?(File.join(dest_dir, entry))
920                             psys("cp '#{$themedir}/root/#{entry}' '#{dest_dir}'")
921                         end
922                     }
923                 end
924             end
925             next
926         end
927
928         msg 2, _("Outputting in %s...") % dest_dir
929
930         #- populate data structure with sizes from theme
931         for sizeobj in $images_size
932             fullscreen_images ||= {}
933             fullscreen_images[sizeobj['name']] = []
934             thumbnail_images ||= {}
935             thumbnail_images[sizeobj['name']] = []
936             thumbnail_videos ||= {}
937             thumbnail_videos[sizeobj['name']] = []
938         end
939         #- a special dummy size to keep 'references' to thumbnails in case of panorama, because the GUI will use the regular thumbnails
940         thumbnail_images['dont-delete-file-for-gui'] = []
941         if $limit_sizes =~ /original/
942             fullscreen_images['original'] = []
943         end
944
945         images.size >= 1 and msg 3, _("\tcreating photos thumbnails...")
946
947         #- create thumbnails for images
948         images.each { |img|
949             info("processing element")
950             base_dest_img = dest_dir + '/' + make_dest_filename(img.sub(/\.[^\.]+$/, ''))
951             elem = optelements['image|' + utf8(img)]
952             if $forgui
953                 thumbnail_dest_img = base_dest_img + "-#{$default_size['thumbnails']}.jpg"
954                 gen_thumbnails_element("#{dir}/#{img}", elem, true, [ { 'filename' => thumbnail_dest_img, 'size' => $default_size['thumbnails'] } ])
955             else
956                 todo = []
957                 for sizeobj in $images_size
958                     size_fullscreen = sizeobj['fullscreen']
959                     size_thumbnails = sizeobj['thumbnails']
960                     fullscreen_dest_img = base_dest_img + "-#{size_fullscreen}.jpg"
961                     fullscreen_images[sizeobj['name']] << File.basename(fullscreen_dest_img)
962                     todo << { 'filename' => fullscreen_dest_img, 'size' => size_fullscreen }
963                     if pano = pano_amount(elem)
964                         thumbnail_images['dont-delete-file-for-gui'] << File.basename(base_dest_img + "-#{size_thumbnails}.jpg")
965                         size_thumbnails = size_thumbnails.sub(/(\d+)/) { ($1.to_i * pano).to_i }
966                     end
967                     thumbnail_dest_img = base_dest_img + "-#{size_thumbnails}.jpg"
968                     thumbnail_images[sizeobj['name']] << File.basename(thumbnail_dest_img)
969                     todo << { 'filename' => thumbnail_dest_img,  'size' => size_thumbnails }
970                 end
971                 gen_thumbnails_element("#{dir}/#{img}", elem, true, todo)
972                 if $limit_sizes =~ /original/
973                     fullscreen_images['original'] << img
974                 end
975                 destimg = "#{dest_dir}/#{img}"
976                 if $limit_sizes =~ /original/ && !File.exists?(destimg)
977                     if $hardlinks_ok
978                         if ! sys("ln '#{dir}/#{img}' '#{destimg}'")
979                             $hardlinks_ok = false
980                         end
981                     end
982                     if ! $hardlinks_ok
983                         psys("cp '#{dir}/#{img}' '#{destimg}'")
984                     end
985                 end
986             end
987         }
988
989         videos.size >= 1 and msg 3, _("\tcreating videos thumbnails...")
990         transcoded_videos = {}
991
992         #- create thumbnails for videos
993         videos.each { |video|
994             info("processing element")
995             elem = optelements['video|' + utf8(video)]
996             if $forgui
997                 thumbnail_dest_img = dest_dir + '/' + make_dest_filename(video.sub(/\.[^\.]+$/, '')) + "-#{$default_size['thumbnails']}.jpg"
998                 gen_thumbnails_element("#{dir}/#{video}", elem, true, [ { 'filename' => thumbnail_dest_img, 'size' => $default_size['thumbnails'] } ])
999             else
1000                 todo = []
1001                 for sizeobj in $images_size
1002                     size_thumbnails = sizeobj['thumbnails']
1003                     thumbnail_dest_img = dest_dir + '/' + make_dest_filename(video.sub(/\.[^\.]+$/, '')) + "-#{size_thumbnails}.jpg"
1004                     thumbnail_videos[sizeobj['name']] << File.basename(thumbnail_dest_img)
1005                     todo << { 'filename' => thumbnail_dest_img, 'size' => size_thumbnails }
1006                 end
1007                 gen_thumbnails_element("#{dir}/#{video}", elem, true, todo)
1008
1009                 if $transcode_videos
1010                     parts = $transcode_videos.split(':', 2)
1011                     basedestvideo = video.sub(/\.\w+/, '') + '.' + parts[0]
1012                     transcoded_videos[video] = basedestvideo
1013                     destvideo = "#{dest_dir}/#{basedestvideo}"
1014                     if ! File.exists?(destvideo)
1015                         psys(parts[1].gsub(/%f/, "'#{dir}/#{video}'").gsub(/%o/, "'#{destvideo}'"))
1016                     end
1017                 else
1018                     destvideo = "#{dest_dir}/#{video}"
1019                     if ! File.exists?(destvideo)
1020                         if $hardlinks_ok
1021                             if ! sys("ln '#{dir}/#{video}' '#{destvideo}'")
1022                                 $hardlinks_ok = false
1023                             end
1024                         end
1025                         if ! $hardlinks_ok
1026                             psys("cp '#{dir}/#{video}' '#{destvideo}'")
1027                         end
1028                     end
1029                 end
1030             end
1031         }
1032
1033         if !$forgui            
1034             #- cleanup old images/videos (for when removing elements or sizes)
1035             all_elements = fullscreen_images.collect { |e| e[1] }.flatten.
1036                      concat(thumbnail_images.collect { |e| e[1] }.flatten).
1037                      concat(thumbnail_videos.collect { |e| e[1] }.flatten).
1038                      concat($transcode_videos ? transcoded_videos.values : videos).
1039                      push('.htaccess')
1040             to_del = Dir.entries(dest_dir).find_all { |e| !File.directory?(File.join(dest_dir, e)) && !all_elements.include?(e) && e !~ /^thumbnails-\w+\.jpg/ }
1041             if to_del.size > 0
1042                 msg 3, _("\tcleaning up: #{to_del.join(', ')}")
1043                 File.delete(*to_del.collect { |e| File.join(dest_dir, e) })
1044             end
1045
1046             #- copy any resource file that goes with the theme (css, images..)
1047             themestuff = Dir.entries("#{$themedir}").
1048                              find_all { |e| !%w(. .. skeleton_image.html skeleton_thumbnails.html skeleton_index.html metadata root).include?(e) }
1049             themestuff.each { |entry|
1050                 if !File.exists?(File.join(dest_dir, entry))
1051                     psys("cp '#{$themedir}/#{entry}' '#{dest_dir}'")
1052                 end
1053             }
1054             
1055             #- copy any root-only resource file that goes with the theme (css, images..)
1056             if xmldir.parent.name != 'dir'
1057                 themestuff_root = Dir.entries("#{$themedir}/root").
1058                                       find_all { |e| !%w(. ..).include?(e) }
1059                 themestuff_root.each { |entry|
1060                     if !File.exists?(File.join(dest_dir, entry))
1061                         psys("cp '#{$themedir}/root/#{entry}' '#{dest_dir}'")
1062                     end
1063                 }
1064             end
1065             
1066             msg 3, _("\tgenerating HTML pages...")
1067             #- fixup max per page
1068             if $N_per_page
1069                 $N_per_page = $N_per_page.to_i / ($N_per_row || $default_N).to_i * ($N_per_row || $default_N).to_i
1070             end
1071
1072             #- generate thumbnails*.html (page with thumbnails)
1073             image2thumbnailpage4js = []
1074             for sizeobj in $images_size
1075                 info("processing size")
1076                 html = $html_thumbnails.collect { |l| l.clone }
1077                 iterations = {}
1078                 for i in html
1079                     i.sub!(/~~title~~/,
1080                            xmldir.attributes['thumbnails-caption'] || utf8(File.basename(dir)))
1081                     discover_iterations(iterations, i)
1082                 end
1083                 all_pages = []
1084                 html_thumbnails = ''
1085                 html_thumbnails_nojs = ''
1086                 counter = 0
1087                 pagecount = 0
1088                 reset_iterations(iterations)
1089                 #- preprocess the @filename->elem, rexml is very slow with that; we dramatically improve this part of the processing
1090                 optfilename = {}
1091                 xmldir.elements.each('image') { |elem|
1092                     optfilename[elem.attributes['filename']] = elem
1093                 }
1094                 for file in entries
1095                     type = images.include?(file) ? 'image' : videos.include?(file) ? 'video' : nil
1096                     if type
1097                         homogeinize_width = 100 / ($N_per_row || $default_N).to_i
1098                         if type == 'image' && elem = optfilename[utf8(file)]
1099                             if pano = pano_amount(elem)
1100                                 html_elem = run_iterations(iterations, pano)
1101                                 counter += count = pano.ceil
1102                                 html_elem.gsub!(/~~colspan~~/) { "colspan=\"#{count}\"" }
1103                                 homogeinize_width *= count
1104                             else
1105                                 html_elem = run_iterations(iterations, 1)
1106                                 counter += 1
1107                                 html_elem.gsub!(/~~colspan~~/, '')
1108                             end
1109                         else 
1110                             html_elem = run_iterations(iterations, 1)
1111                             counter += 1
1112                             html_elem.gsub!(/~~colspan~~/, '')
1113                         end
1114                         html_elem.gsub!(/~~homogeinize_width~~/) { "width=\"#{homogeinize_width}%\"" }
1115                         if type == 'image'
1116                             index = images.index(file)
1117                             html_elem.gsub!(/~~caption_iteration~~/,
1118                                             find_caption_value(xmldir, images[index]) || utf8(images[index]))
1119                             html_elem.gsub!(/~~ifimage\?~~(.+?)~~fi~~/) { $1 }
1120                             html_elem.gsub!(/~~ifvideo\?~~(.+?)~~fi~~/, '')
1121                         elsif type == 'video'
1122                             index = videos.index(file)
1123                             if File.exists?("#{dest_dir}/#{thumbnail_videos[sizeobj['name']][index]}")
1124                                 html_elem.gsub!(/~~image_iteration~~/,
1125                                                 '<a href="' + ( $transcode_videos ? transcoded_videos[videos[index]] : videos[index] ) + '">' +
1126                                                     img_element("#{dest_dir}/#{thumbnail_videos[sizeobj['name']][index]}") + '</a>')
1127                             else
1128                                 html_elem.gsub!(/~~image_iteration~~/,
1129                                                 '<a href="' + ( $transcode_videos ? transcoded_videos[videos[index]] : videos[index] ) + '">' +
1130                                                     defer_translation(N_("(no preview)")) + '</a>')
1131                             end
1132                             html_elem.gsub!(/~~caption_iteration~~/, find_caption_value(xmldir, videos[index]) || utf8(videos[index]))
1133                             html_elem.gsub!(/~~ifimage\?~~(.+?)~~fi~~/, '')
1134                             html_elem.gsub!(/~~ifvideo\?~~(.+?)~~fi~~/) { $1 }
1135                         end
1136                         html_thumbnails += html_elem
1137                         html_thumbnails_nojs += html_elem
1138                         if type == 'image'
1139                             html_thumbnails.gsub!(/~~image_iteration~~/,
1140                                                   '<a href="image-' + size2js(sizeobj['name']) + $htmlsuffix + '#current=' + fullscreen_images[sizeobj['name']][index] +
1141                                                       '" name="' + fullscreen_images[sizeobj['name']][index] + '">' +
1142                                                       img_element("#{dest_dir}/#{thumbnail_images[sizeobj['name']][index]}") + '</a>')
1143                             html_thumbnails_nojs.gsub!(/~~image_iteration~~/, 
1144                                                        '<a href="' + fullscreen_images[sizeobj['name']][index] + '" name="' + fullscreen_images[sizeobj['name']][index] + '">' +
1145                                                        img_element("#{dest_dir}/#{thumbnail_images[sizeobj['name']][index]}") + '</a>')
1146                             #- remember in which thumbnails page is this element, for image->thumbnail link
1147                             if sizeobj == $images_size[0]
1148                                 image2thumbnailpage4js << pagecount
1149                             end
1150                         end
1151
1152                         if counter == $N_per_page
1153                             html_thumbnails      += close_iterations(iterations)
1154                             html_thumbnails_nojs += close_iterations(iterations)
1155                             all_pages << [ html_thumbnails, html_thumbnails_nojs ]
1156                             html_thumbnails = ''
1157                             html_thumbnails_nojs = ''
1158                             counter = 0
1159                             reset_iterations(iterations)
1160                             pagecount += 1
1161                         end
1162                     end
1163                 end
1164                 if counter > 0
1165                     html_thumbnails      += close_iterations(iterations)
1166                     html_thumbnails_nojs += close_iterations(iterations)
1167                     all_pages << [ html_thumbnails, html_thumbnails_nojs ]
1168                 end
1169                 for i in html
1170                     i.gsub!(/~~theme~~/, $theme)
1171                     i.gsub!(/~~current_size~~/, sizeobj['name'])
1172                     i.gsub!(/~~htmlsuffix~~/, $htmlsuffix)
1173                     i.gsub!(/~~current_size_js~~/, size2js(sizeobj['name']))
1174                     i.gsub!(/~~madewith~~/, $madewith || '')
1175                     i.gsub!(/~~indexlink~~/, $indexlink || '')
1176                     substitute_navigation(i, xmldir)
1177                 end
1178                 html_nojs = html.collect { |l| l.clone }
1179                 pagecount = 0
1180                 for page in all_pages
1181                     html_thumbnails, html_thumbnails_nojs = page
1182                     final_html = html.collect { |l| l.clone }
1183                     mstuff = defer_translation(N_("Pages: ")) +
1184                              (pagecount > 0 ? "<a href=\"thumbnails-#{size2js(sizeobj['name'])}%nojs-#{pagecount - 1}#{$htmlsuffix}\">" + defer_translation(N_("<- Previous")) + "</a> " : '') +
1185                              all_pages.collect_with_index { |p,idx| page == p ? idx + 1 : "<a href=\"thumbnails-#{size2js(sizeobj['name'])}%nojs-#{idx}#{$htmlsuffix}\">#{idx + 1}</a>" }.join(', ') +
1186                              (pagecount < all_pages.size - 1 ? " <a href=\"thumbnails-#{size2js(sizeobj['name'])}%nojs-#{pagecount + 1}#{$htmlsuffix}\">" + defer_translation(N_("Next ->")) + "</a> " : '')
1187                     for i in final_html
1188                         i.sub!(/~~run_slideshow~~/,
1189                                images.size <= 1 ? '' : '<a href="image-' + size2js(sizeobj['name']) + $htmlsuffix + '#run_slideshow=1">' + defer_translation(N_("Run slideshow!"))+'</a>')
1190                         i.sub!(/~~thumbnails~~/, html_thumbnails)
1191                         if all_pages.size == 1
1192                             i.gsub!(/~~ifmultiplepages\?~~.*~~fi~~/, '')
1193                         else
1194                             i.gsub!(/~~ifmultiplepages\?~~(.+?)~~fi~~/) { $1 }
1195                             i.gsub!(/~~multiplepagesstuff~~/, mstuff.gsub('%nojs', ''))
1196                         end
1197                         substitute_html_sizes(i, sizeobj, 'thumbnails', "-#{pagecount}")
1198                         substitute_pathtobase(i, xmldir)
1199                     end
1200                     save_html(final_html, "#{dest_dir}/thumbnails-#{size2js(sizeobj['name'])}-#{pagecount}")
1201                     final_html_nojs = html_nojs.collect { |l| l.clone }
1202                     for i in final_html_nojs
1203                         i.sub!(/~~run_slideshow~~/, defer_translation(N_("<i>Click on an image to view it larger</i>")))
1204                         i.sub!(/~~thumbnails~~/, html_thumbnails_nojs)
1205                         if all_pages.size == 1
1206                             i.gsub!(/~~ifmultiplepages\?~~.*~~fi~~/, '')
1207                         else
1208                             i.gsub!(/~~ifmultiplepages\?~~(.+?)~~fi~~/) { $1 }
1209                             i.gsub!(/~~multiplepagesstuff~~/, mstuff.gsub('%nojs', '-nojs'))
1210                         end
1211                         substitute_html_sizes(i, sizeobj, 'thumbnails', "-nojs-#{pagecount}")
1212                         substitute_pathtobase(i, xmldir)
1213                     end
1214                     save_html(final_html_nojs, "#{dest_dir}/thumbnails-#{size2js(sizeobj['name'])}-nojs-#{pagecount}")
1215                     pagecount += 1
1216                 end
1217             end
1218
1219             info("finished processing sizes")
1220
1221             #- generate "main" thumbnails.html page that will reload to correct size thanks to cookie
1222             save_html(html_reload_to_thumbnails, "#{dest_dir}/thumbnails")
1223
1224             #- generate image.html (page with fullscreen images)
1225             if images.size > 0
1226                 captions4js = find_captions(xmldir, images).collect { |e| e ? '"' + e.gsub('"', '\"') + '"' : '""' }.join(', ')
1227                 thumbnailspage4js = image2thumbnailpage4js.collect { |e| "\"#{e}\"" }.join(', ')
1228                 for sizeobj in $images_size
1229                     html = $html_images.collect { |l| l.clone }
1230                     images4js = fullscreen_images[sizeobj['name']].collect { |e| "\"#{e}\"" }.join(', ')
1231                     otherimages4js = ''
1232                     othersizes = []
1233                     for sizeobj2 in all_images_sizes
1234                         if sizeobj != sizeobj2
1235                             otherimages4js += "var images_#{size2js(sizeobj2['name'])} = new Array(" + fullscreen_images[sizeobj2['name']].collect { |e| "\"#{e}\"" }.join(', ') + ")\n"
1236                             othersizes << "\"#{size2js(sizeobj2['name'])}\""
1237                         end
1238                     end
1239                     for i in html
1240                         i.gsub!(/~~images~~/, images4js)
1241                         i.gsub!(/~~other_images~~/, otherimages4js)
1242                         i.gsub!(/~~thumbnailspages~~/, thumbnailspage4js)
1243                         i.gsub!(/~~other_sizes~~/, othersizes.join(', '))
1244                         i.gsub!(/~~captions~~/, captions4js)
1245                         i.gsub!(/~~title~~/, xmldir.attributes['thumbnails-caption'] || utf8(File.basename(dir)))
1246                         i.gsub!(/~~thumbnails~~/, '<a href="thumbnails-' + size2js(sizeobj['name']) + $htmlsuffix + '" id="thumbnails">' + defer_translation(N_('return to thumbnails')) + '</a>')
1247                         i.gsub!(/~~theme~~/, $theme)
1248                         i.gsub!(/~~current_size~~/, size2js(sizeobj['name']))
1249                         i.gsub!(/~~htmlsuffix~~/, $htmlsuffix)
1250                         i.gsub!(/~~madewith~~/, $madewith || '')
1251                         i.gsub!(/~~indexlink~~/, $indexlink || '')
1252                         substitute_html_sizes(i, sizeobj, 'image', '')
1253                         substitute_navigation(i, xmldir)
1254                         substitute_pathtobase(i, xmldir)
1255                     end
1256                     save_html(html, "#{dest_dir}/image-#{size2js(sizeobj['name'])}")
1257                 end
1258             end
1259         end
1260     }
1261
1262     msg 3, ''
1263
1264     #- add attributes to <dir..> elements needing so
1265     if $mode != 'use_config'
1266         msg 3, _("\tfixating configuration file...")
1267         $xmldoc.elements.each('//dir') { |element|
1268             path = captionpath = element.attributes['path']
1269             descendant_element = nil
1270             #- search if there is at least one image or video down that dir; use workarounds to rexml slowness
1271             #- first, look down only one level, very fast
1272             first_desc = nil
1273             element.elements.each { |elem|
1274                 if first_desc.nil?
1275                     first_desc = elem
1276                 end
1277                 if elem.name == 'image' || elem.name == 'video'
1278                     descendant_element = elem
1279                     break
1280                 end
1281             }
1282             if descendant_element.nil?
1283                 #- if there was nothing or only directory down one level, look down the first subdir
1284                 if ! first_desc.nil?
1285                     first_desc.elements.each { |elem|
1286                         if elem.name == 'image' || elem.name == 'video'
1287                             descendant_element = elem
1288                             break
1289                         end
1290                     }
1291                 end
1292                 if descendant_element.nil?
1293                     #- if there was still nothing found, use complete albeit slow method
1294                     descendant_element = element.elements['descendant::image'] || element.elements['descendant::video']
1295                 end
1296             end
1297             if !descendant_element
1298                 msg 3, _("\t\tremoving %s, no element in it") % path
1299                 element.remove  #- means we have a directory with nothing interesting in it
1300             else
1301                 captionfile = "#{descendant_element.parent.attributes['path']}/#{descendant_element.attributes['filename']}"
1302                 basename = File.basename(path)
1303                 if element.elements['dir']
1304                     if !element.attributes['subdirs-caption']
1305                         element.add_attribute('subdirs-caption', basename)
1306                     end
1307                     if !element.attributes['subdirs-captionfile']
1308                         element.add_attribute('subdirs-captionfile', captionfile)
1309                     end
1310                 end
1311                 if element.child_byname_notattr('image', 'deleted') || element.child_byname_notattr('video', 'deleted')
1312                     if !element.attributes['thumbnails-caption']
1313                         element.add_attribute('thumbnails-caption', basename)
1314                     end
1315                     if !element.attributes['thumbnails-captionfile']
1316                         element.add_attribute('thumbnails-captionfile', captionfile)
1317                     end
1318                 end
1319             end
1320         }
1321     end
1322
1323     #- write down to disk config if necessary
1324     if $config_writeto
1325         ios = File.open($config_writeto, "w")
1326         $xmldoc.write(ios, 0)
1327         ios.close
1328     end
1329
1330     if $forgui
1331         msg 3, _("completed necessary stuff for GUI, exiting.")
1332         return
1333     end
1334
1335     #- second pass to create index.html files and previous/next links
1336     info("creating index.html")
1337     msg 3, _("\trescanning directories to generate all 'index.html' files...")
1338
1339     #- recompute the memoization because elements mights have been removed (the ones with no element in them)
1340     optxpath = {}
1341     $xmldoc.elements.each('//dir') { |elem|
1342         optxpath[elem.attributes['path']] = elem
1343     }
1344
1345     examined_dirs.each { |dir|
1346         info("index.html: #{dir}|#{$source}")
1347
1348         xmldir = optxpath[utf8(dir)]
1349         if !xmldir || (xmldir.attributes['already-generated'] && !$force) || xmldir.attributes['deleted']
1350             next
1351         end
1352         dest_dir = make_dest_filename(dir.sub(/^#{Regexp.quote($source)}/, $dest))
1353
1354         previous_album = find_previous_album(xmldir)
1355         next_album = find_next_album(xmldir)
1356
1357         if xmldir.elements['dir']
1358             html = $html_index.collect { |l| l.clone }
1359             iterations = {}
1360             for i in html
1361                 caption = xmldir.attributes['subdirs-caption']
1362                 i.gsub!(/~~title~~/, caption)
1363                 substitute_navigation(i, xmldir)
1364                 substitute_pathtobase(i, xmldir)
1365                 discover_iterations(iterations, i)
1366             end
1367             
1368             html_index = ''
1369             reset_iterations(iterations)
1370             
1371             #- deal with "current" album (directs to "thumbnails" page)
1372             if xmldir.attributes['thumbnails-caption']
1373                 thumbnail = "#{dest_dir}/thumbnails-thumbnail.jpg"
1374                 gen_thumbnails_subdir(from_utf8(xmldir.attributes['thumbnails-captionfile']), xmldir, false,
1375                                       [ { 'filename' => thumbnail, 'size' => $albums_thumbnail_size } ], 'thumbnails')
1376                 html_index += run_iterations(iterations, 1)
1377                 html_index.gsub!(/~~image_iteration~~/, "<a href=\"thumbnails#{$htmlsuffix}\">" + img_element(thumbnail) + '</a>')
1378                 html_index.gsub!(/~~caption_iteration~~/, xmldir.attributes['thumbnails-caption'])
1379             end
1380
1381             #- deal with sub-albums (direct to subdirs/index.html pages)
1382             xmldir.elements.each('dir') { |child|
1383                 if child.attributes['deleted']
1384                     next
1385                 end
1386                 subdir = make_dest_filename(from_utf8(File.basename(child.attributes['path'])))
1387                 thumbnail = "#{dest_dir}/thumbnails-#{subdir}.jpg"
1388                 html_index += run_iterations(iterations, 1)
1389                 captionfile, caption = find_subalbum_caption_info(child)
1390                 gen_thumbnails_subdir(captionfile, child, false,
1391                                       [ { 'filename' => thumbnail, 'size' => $albums_thumbnail_size } ], find_subalbum_info_type(child))
1392                 html_index.gsub!(/~~caption_iteration~~/, caption)
1393                 html_index.gsub!(/~~image_iteration~~/, "<a href=\"#{subdir}/index#{$htmlsuffix}\">" + img_element(thumbnail) + '</a>')
1394             }
1395
1396             html_index += close_iterations(iterations)
1397
1398             for i in html
1399                 i.gsub!(/~~thumbnails~~/, html_index)
1400                 i.gsub!(/~~madewith~~/, $madewith || '')
1401                 i.gsub!(/~~indexlink~~/, $indexlink || '')
1402             end
1403             
1404         else
1405             html = html_reload_to_thumbnails
1406         end
1407
1408         save_html(html, "#{dest_dir}/index")
1409         if $multi_languages
1410             #- in case MultiViews will not work, generate some compat
1411             ios = File.open("#{dest_dir}/index.html", "w")
1412             ios.write("<html><head><meta http-equiv=\"refresh\" content=\"0.1;url=index.#{$multi_languages[1]}.html\"></head></html>")
1413             ios.close
1414         end
1415
1416         #- substitute multiple "return to albums", previous/next correctly
1417         #- the following two statements are dramatical optimizations to executing for each substInFile callback
1418         dirpresent = xmldir.elements['dir']
1419         parentname = xmldir.parent.name
1420         if xmldir.child_byname_notattr('image', 'deleted') || xmldir.child_byname_notattr('video', 'deleted')
1421             for suffix in [ '', '-nojs' ]
1422                 for sizeobj in $images_size
1423                     Dir.glob("#{dest_dir}/thumbnails-#{size2js(sizeobj['name'])}#{suffix}-*.html") do |file|
1424                         #- unroll translations, they are costly if rerun for each line of files
1425                         rta = find_translation_for_file(file, N_('return to albums'))
1426                         pa = find_translation_for_file(file, N_('previous album'))
1427                         na = find_translation_for_file(file, N_('next album'))
1428                         substInFile(file) { |line|
1429                             sub_previous_next_album(file, previous_album, next_album, line, pa, na)
1430                             if dirpresent
1431                                 line.sub!(/~~return_to_albums~~/, '<a href="index' + $htmlsuffix + '">' + rta + '</a>')
1432                             else
1433                                 if parentname == 'dir'
1434                                     line.sub!(/~~return_to_albums~~/, '<a href="../index' + $htmlsuffix + '">' + rta + '</a>')
1435                                 else
1436                                     line.sub!(/~~return_to_albums~~/, '')
1437                                 end
1438                             end
1439                             line
1440                         }
1441                     end
1442                     if suffix == '' && xmldir.child_byname_notattr('image', 'deleted')
1443                         Dir.glob("#{dest_dir}/image-#{size2js(sizeobj['name'])}*.html") do |file|
1444                             pa = find_translation_for_file(file, N_('previous album'))
1445                             na = find_translation_for_file(file, N_('next album'))
1446                             substInFile(file) { |line|
1447                                 sub_previous_next_album(file, previous_album, next_album, line, pa, na)
1448                             }
1449                         end
1450                     end
1451                 end
1452             end
1453         end
1454     }
1455
1456     msg 3, _(" all done.")
1457 end
1458
1459 handle_options
1460 read_config
1461 check_installation
1462
1463 build_html_skeletons
1464
1465 walk_source_dir