properly html-escape captions; use caption for alt text suggested by Warly
[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
443     else
444         return nil
445     end
446 end
447
448 def quote_caption(input)
449     return input.gsub('<', '&lt;').gsub('>', '&gt;').gsub('"', '&quot;').gsub("'", '&#39;').gsub("\n", '<br/>')
450 end
451
452 def find_captions(xmldir, images)
453     return images.collect { |img| find_caption_value(xmldir, img) }
454 end
455
456 #- stolen from CVSspam
457 def urlencode(text)
458   text.sub(/[^a-zA-Z0-9\-,.*_\/]/) do
459     "%#{sprintf('%2X', $&[0])}"
460   end
461 end
462
463 def all_images_sizes
464     return $limit_sizes =~ /original/ ? $images_size + [ { 'name' => 'original' } ] : $images_size
465 end
466
467 def html_reload_to_thumbnails
468     html_reload_to_thumbnails = $preferred_size_reloader.clone
469     html_reload_to_thumbnails.gsub!(/~~theme~~/, $theme)
470     html_reload_to_thumbnails.gsub!(/~~default_size~~/, size2js($default_size['name']))
471     html_reload_to_thumbnails.gsub!(/~~htmlsuffix~~/, $htmlsuffix)
472     html_reload_to_thumbnails.gsub!(/~~all_sizes~~/, all_images_sizes.collect { |s| "\"#{size2js(s['name'])}\"" }.join(', '))
473     size_auto_chooser = '';
474     all_images_sizes.find_all { |s| s.has_key?('optimizedforwidth') }.
475                      sort { |a,b| b['optimizedforwidth'].to_i <=> a['optimizedforwidth'].to_i }.
476                      each { |s| size_auto_chooser += "if (w + 50 > #{s['optimizedforwidth']}) { return 'thumbnails-#{size2js(s['name'])}-0#{$htmlsuffix}'; }\n" }
477     html_reload_to_thumbnails.gsub!(/~~size_auto_chooser~~/, size_auto_chooser)
478     return html_reload_to_thumbnails
479 end
480
481 def discover_iterations(iterations, line)
482     if line =~ /~~iterate(\d)_open(_max(\d+|N))?~~/
483         for iter in iterations.values
484             if iter['open']
485                 iter['open'] = false
486                 iter['close_wait'] = $1.to_i
487             end
488         end
489         max = $3 == 'N' ? ($N_per_row || $default_N) : $3
490         iterations[$1.to_i] = { 'open' => true, 'max' => max, 'opening' => '', 'closing' => '' }
491         if $1.to_i == 1
492             line.sub!(/.*/, '~~thumbnails~~')
493         else
494             line.sub!(/.*/, '')
495         end
496     elsif line =~ /~~iterate(\d)_close~~/
497         iterations[$1.to_i]['open']  = false;
498         iterations[$1.to_i]['close'] = true;
499         line.sub!(/.*/, '')
500     else
501         for iter in iterations.values
502             if iter['open']
503                 iter['opening'] += line
504                 line.sub!(/.*/, '')
505             end
506             if !iter['close'] && iter['close_wait'] && iterations[iter['close_wait']]['close']
507                 iter['closing'] += line
508                 line.sub!(/.*/, '')
509             end
510         end
511     end
512 end
513
514 def reset_iterations(iterations)
515     for iter in iterations.values
516         iter['value'] = 0
517     end
518 end
519
520 def run_iterations(iterations, amount)
521     html = ''
522     should_rerun = false
523     for level in iterations.keys.sort
524         if iterations[level]['value'] == 0
525             html += iterations[level]['opening']
526         elsif level == iterations.keys.max
527             if !iterations[level]['max'] || iterations[level]['max'] && iterations[level]['value'] + amount <= iterations[level]['max'].to_i
528                 html += iterations[level]['opening']
529             else
530                 should_rerun = true
531             end
532         end
533         iterations[level]['value'] += amount
534         if iterations[level]['max'] && iterations[level]['value'] > iterations[level]['max'].to_i
535             iterations[level]['value'] = 0
536             iterations[level-1]['value'] = 0
537             html += iterations[level-1]['closing']
538         end
539     end
540     if should_rerun
541         return html + run_iterations(iterations, amount)
542     else
543         return html
544     end
545 end
546
547 def close_iterations(iterations)
548     html = ''
549     for level in iterations.keys.sort.reverse
550         html += iterations[level]['closing']
551     end
552     return html
553 end
554
555 def img_element(fullpath, caption)
556     if size = get_image_size(fullpath)
557         sizespec = 'width="' + size[:x].to_s + '" height="' + size[:y].to_s + '"'
558     else
559         sizespec = ''
560     end
561     return '<img src="' + File.basename(fullpath) + '" ' + sizespec + ' alt="' + quote_caption(caption) + '"/>'
562 end
563
564 def size2js(name)
565     return name.gsub(/-/, '')
566 end
567
568 def substitute_html_sizes(html, sizeobj, type, suffix)
569     sizestrings = []
570     if $images_size.length > 1 || (type == 'image' && $limit_sizes =~ /original/)
571         for sizeobj2 in $images_size
572             sizejs = size2js(sizeobj2['name'])
573             sizen = defer_translation(sizename(sizeobj2['name'], false))
574             if sizeobj != sizeobj2
575                 if type == 'thumbnails'
576                     sizestrings << '<a href="thumbnails-' + sizejs + suffix + $htmlsuffix + '" onclick="set_preferred_size(\'' + sizejs + '\')">' + sizen + '</a>'
577                 else
578                     sizestrings << '<a id="link' + sizejs + '" onclick="set_preferred_size(\'' + sizejs + '\')">' + sizen + '</a>'
579                 end
580             else
581                 sizestrings << sizen
582             end
583         end
584         if type == 'image' && $limit_sizes =~ /original/
585             sizestrings << '<a id="linkoriginal" target="newframe">' + defer_translation(sizename('original', false)) + '</a>'
586         end
587     end
588     html.sub!(/~~sizes~~(.+)~~/) { sizestrings.join($1) }
589 end
590
591 def substitute_navigation(html, xmldir)
592     if xmldir.parent.name == 'dir'
593         nav = ''
594         path = '..'
595         parent = xmldir.parent
596         while parent.name == 'dir'
597             parentcaption = parent.attributes['subdirs-caption'] || File.basename(parent.attributes['path'])
598             nav = "<a href=\"#{path}/index#{$htmlsuffix}\">#{parentcaption}</a> #{defer_translation(N_(" > "))} #{nav}"
599             path += '/..'
600             parent = parent.parent
601         end
602         html.gsub!(/~~ifnavigation\?~~(.+?)~~fi~~/) { $1 }
603         html.gsub!(/~~navigation~~/, nav + (xmldir.attributes['subdirs-caption'] || File.basename(xmldir.attributes['path'])))
604     else
605         html.gsub!(/~~ifnavigation\?~~(.+?)~~fi~~/, '')
606     end
607 end
608
609 def substitute_pathtobase(html, xmldir)
610     path = ''
611     location = xmldir
612     while location.parent.name == 'dir'
613         path = '../' + path
614         location = location.parent
615     end
616     html.gsub!(/~~pathtobase~~/, path)
617 end
618
619 def xmldir2destdir(xmldir)
620     return make_dest_filename(from_utf8(File.basename(xmldir.attributes['path'])))
621 end
622
623 def find_previous_album(xmldir)
624     relative_pos = ''
625     begin
626         #- move to previous dir element if exists
627         if prevelem = xmldir.previous_element_byname_notattr('dir', 'deleted')
628             xmldir = prevelem
629             relative_pos += '../' + xmldir2destdir(xmldir) + '/'
630             child = nil
631             #- after having moved to previous dir, we need to go down last subdir until the last one
632             while child = xmldir.elements['dir']
633                 while nextchild = child.next_element_byname_notattr('dir', 'deleted')
634                     child = nextchild
635                 end
636                 relative_pos += xmldir2destdir(child) + '/'
637                 xmldir = child
638             end
639         else
640             #- previous dir doesn't exist, move to previous dir element if exists
641             xmldir = xmldir.parent
642             if xmldir.name == 'dir' && !xmldir.attributes['deleted']
643                 relative_pos += '../'
644             else
645                 return nil
646             end
647         end
648     end while !xmldir.child_byname_notattr('image', 'deleted') && !xmldir.child_byname_notattr('video', 'deleted')
649     return File.reduce_path(relative_pos)
650 end
651
652 def find_next_album(xmldir)
653     relative_pos = ''
654     begin
655         #- first child dir element (catches when initial xmldir has both thumbnails and subdirs)
656         if firstchild = xmldir.child_byname_notattr('dir', 'deleted')
657             xmldir = firstchild
658             relative_pos += xmldir2destdir(xmldir) + '/'
659         #- next brother
660         elsif nextbro = xmldir.next_element_byname_notattr('dir', 'deleted')
661             xmldir = nextbro
662             relative_pos += '../' + xmldir2destdir(xmldir) + '/'
663         else
664             #- go up until we have a next brother or we are finished
665             begin
666                 xmldir = xmldir.parent
667                 relative_pos += '../'
668             end while xmldir && !xmldir.next_element_byname_notattr('dir', 'deleted')
669             if xmldir
670                 xmldir = xmldir.next_element_byname('dir')
671                 relative_pos += '../' + xmldir2destdir(xmldir) + '/'
672             else
673                 return nil
674             end
675         end
676     end while !xmldir.child_byname_notattr('image', 'deleted') && !xmldir.child_byname_notattr('video', 'deleted')
677     return File.reduce_path(relative_pos)
678 end
679
680 def find_translation_for_file(file, msg)
681     if $multi_languages
682         if file =~ /\.(\w\w)\.html$/
683             bindtextdomain("booh", { :locale => "#{$1}.UTF-8" })
684             retval = _(msg)
685             Locale.set_current($default_locale)
686             return retval
687         else
688             die "Internal error: cannot find multi language suffix of file '#{file}'"
689         end
690     else
691         return utf8(_(msg))
692     end
693 end
694
695 def sub_previous_next_album(file, previous_album, next_album, html, previous_album_msg, next_album_msg)
696     if previous_album
697         html.gsub!(/~~previous_album~~/, '<a href="' + previous_album + 'thumbnails' + $htmlsuffix + '">' + previous_album_msg + '</a>')
698         html.gsub!(/~~ifprevious_album\?~~(.+?)~~fi~~/) { $1 }
699     else
700         html.gsub!(/~~previous_album~~/, '')
701         html.gsub!(/~~ifprevious_album\?~~(.+?)~~fi~~/, '')
702     end
703     if next_album
704         html.gsub!(/~~next_album~~/, '<a href="' + next_album + 'thumbnails' + $htmlsuffix + '">' + next_album_msg + '</a>')
705         html.gsub!(/~~ifnext_album\?~~(.+?)~~fi~~/) { $1 }
706     else
707         html.gsub!(/~~next_album~~/, '')
708         html.gsub!(/~~ifnext_album\?~~(.+?)~~fi~~/, '')
709     end
710     return html
711 end
712
713 def save_html(html, base_filename)
714     if html.class == Array
715         html = html.join('')
716     end
717     if $multi_languages
718         for language in ($multi_languages[0] + [ $multi_languages[1] ]).uniq
719             bindtextdomain("booh", { :locale => "#{language}.UTF-8" })
720             ios = File.open("#{base_filename}.#{language}.html", "w")
721             ios.write(html.gsub(/@@(.*?)@@/) { _($1) })
722             ios.close
723             Locale.set_current($default_locale)
724         end
725     else
726         ios = File.open("#{base_filename}.html", "w")
727         ios.write(html.gsub(/@@(.*?)@@/) { utf8(_($1)) })
728         ios.close
729     end
730 end
731
732 def walk_source_dir
733
734     #- preprocess the path->dir, rexml is very slow with that; we seem to improve speed by 7%
735     optxpath = {}
736     $xmldoc.elements.each('//dir') { |elem|
737         optxpath[elem.attributes['path']] = elem
738     }
739
740     examined_dirs = nil
741     if $mode == 'merge_config_onedir'
742         examined_dirs = [ $onedir ]
743     elsif $mode == 'merge_config_subdirs'
744         examined_dirs = `find '#{$onedir}' -type d -follow`.sort.collect { |v| v.chomp }.delete_if { |v| optxpath.has_key?(utf8(v)) }
745     else
746         examined_dirs = `find '#{$source}' -type d -follow`.sort.collect { |v| v.chomp }
747         if $mode == 'merge_config'
748             $xmldoc.elements.each('//dir') { |elem|
749                 if ! examined_dirs.include?(elem.attributes['path'])
750                     msg 2, _("Merging config: removing directory %s from config, isn't on filesystem anymore") % elem.attributes['path']
751                     elem.remove
752                 end
753             }
754         end
755     end
756     info("directories: #{examined_dirs.length}, sizes: #{$images_size.length}")
757
758     examined_dirs.each { |dir|
759         if dir =~ /'/
760             die _("Source directory or sub-directories can't contain a single-quote character, sorry: %s") % dir
761         end
762         if $mode !~ /^use_config/
763             Dir.entries(dir).each { |file|
764                 if file =~ /['"\[\]]/
765                     die _("Files can't contain any of the characters ', \", [ or ], sorry: %s") % "#{dir}/#{file}"
766                 end
767             }
768         end
769     }
770
771     examined_dirs.each { |dir|
772         if File.basename(dir) =~ /^\./
773             msg 1, _("Ignoring directory %s, begins with a dot (indicating a hidden directory)") % dir
774             next
775         end
776
777         dest_dir = make_dest_filename(dir.sub(/^#{Regexp.quote($source)}/, $dest))
778
779         #- place xml document on proper node if exists, else create
780         xmldir = optxpath[utf8(dir)]
781         if $mode == 'use_config' || $mode == 'use_config_changetheme'
782             if !xmldir || (xmldir.attributes['already-generated'] && !$force) || xmldir.attributes['deleted']
783                 info("walking: #{dir}|#{$source}, 0 elements")
784                 if xmldir && xmldir.attributes['deleted']
785                     system("rm -rf '#{dest_dir}'")
786                 end
787                 next
788             end
789         else
790             if $mode == 'gen_config' || (($mode == 'merge_config' || $mode == 'merge_config_subdirs') && !xmldir)
791                 #- add the <dir..> element if necessary
792                 parent = File.dirname(dir)
793                 xmldir = $xmldoc.elements["//dir[@path='#{utf8(parent)}']"]
794                 if !xmldir
795                     xmldir = $xmldoc.root
796                 end
797                 #- need to remove the already-generated mark of the parent because of the sub-albums page containing now one more element
798                 xmldir.delete_attribute('already-generated')
799                 xmldir = optxpath[utf8(dir)] = xmldir.add_element('dir', { 'path' => utf8(dir) })
800             end
801         end
802         xmldir.delete_attribute('already-generated')
803
804         #- preprocess all the existing elements, rexml is slow with that
805         optelements = {}
806         xmldir.elements.each { |elem|
807             if filename = elem.attributes['filename']
808                 optelements[elem.name + '|' + filename] = elem
809             end
810         }
811
812         #- read images/videos entries from config or from directories depending on mode
813         entries = []
814         if $mode == 'use_config' || $mode == 'use_config_changetheme'
815             msg 2, _("Handling %s from config list...") % dir
816             xmldir.elements.each { |element|
817                 if %w(image video).include?(element.name) && !element.attributes['deleted']
818                     entries << from_utf8(element.attributes['filename'])
819                 end
820             }
821         else
822             msg 2, _("Examining %s...") % dir
823             entries = Dir.entries(dir).sort
824             #- populate config in case of gen_config, add new files in case of merge_config
825             for file in entries
826                 if file =~ /['"\[\]]/
827                     msg 1, _("Ignoring %s, contains one of forbidden characters: '\"[]") % "#{dir}/#{file}"
828                 else
829                     type = entry2type(file)
830                     if type && ! optelements[type + '|' + utf8(file)]
831                         #- hack: don't run identify (which is slow) if format only contains default %t
832                         if $commentsformat && type == 'image' && $commentsformat != '%t'
833                             comment = utf8(`identify -format "#{$commentsformat}" '#{dir}/#{file}'`.chomp.sub(/\.$/, ''))
834                         else
835                             comment = utf8cut(file.sub(/\.[^\.]+$/, ''), 18)
836                         end
837                         optelements[type + '|' + utf8(file)] = xmldir.add_element(type, { "filename" => utf8(file), "caption" => comment })
838                     end
839                 end
840             end
841             if $mode != 'gen_config'
842                 #- cleanup removed files from config and reread entries from config to get proper ordering
843                 entries = []
844                 xmldir.elements.each { |element|
845                     fullpath = "#{dir}/#{from_utf8(element.attributes['filename'])}"
846                     if %w(image video).include?(element.name)
847                         if !File.readable?(fullpath)
848                             msg 1, _("Config merge: removing %s from config; use the backup file to retrieve caption info if this was a mistake") % fullpath
849                             xmldir.delete(element)
850                         elsif !element.attributes['deleted']
851                             entries << from_utf8(element.attributes['filename'])
852                         end
853                     end
854                 }
855                 #- if there is no more elements here, there is no album here anymore
856                 if !xmldir.child_byname_notattr('image', 'deleted') && !xmldir.child_byname_notattr('video', 'deleted')
857                     xmldir.delete_attribute('thumbnails-caption')
858                     xmldir.delete_attribute('thumbnails-captionfile')
859                 end
860             end
861         end
862         images = entries.find_all { |e| entry2type(e) == 'image' }
863         msg 3, _("\t%s photos") % images.length
864         videos = entries.find_all { |e| entry2type(e) == 'video' }
865         msg 3, _("\t%s videos") % videos.length
866         info("walking: #{dir}|#{$source}, #{images.length + videos.length} elements")
867
868         system("mkdir -p '#{dest_dir}'")
869
870         #- generate .htaccess file
871         if !$forgui
872             ios = File.open("#{dest_dir}/.htaccess", "w")
873             ios.write("AddCharset UTF-8 .html\n")
874             if auth_user_file = xmldir.attributes['password-protect']
875                 msg 3, _("\tgenerating password protection file #{dest_dir}/.htaccess")
876                 ios.write("AuthType Basic\nAuthName \"protected area\"\nAuthUserFile #{auth_user_file}\nrequire valid-user\n")
877             end
878             if $multi_languages
879                 ios.write("Options +Multiviews\n")
880                 ios.write("LanguagePriority #{$multi_languages[1]}\n")
881                 ios.write("ForceLanguagePriority Prefer Fallback\n")
882                 ios.write("DirectoryIndex index\n")
883             end
884             ios.close
885         end
886
887         #- pass through if there are no images and videos
888         if images.size == 0 && videos.size == 0
889             if !$forgui
890                 #- cleanup old images/videos, especially if this directory contained images/videos previously.
891                 if $mode != 'gen_config'
892                     rightful_images = [ '.htaccess' ]
893                     if xmldir.attributes['thumbnails-caption']
894                         rightful_images << 'thumbnails-thumbnail.jpg'
895                     end
896                     xmldir.elements.each('dir') { |child|
897                         if child.attributes['deleted']
898                             next
899                         end
900                         subdir = make_dest_filename(from_utf8(File.basename(child.attributes['path'])))
901                         rightful_images << "thumbnails-#{subdir}.jpg"
902                     }
903                     to_del = Dir.entries(dest_dir).find_all { |e| !File.directory?(File.join(dest_dir, e)) && !rightful_images.include?(e) }
904                     if to_del.size > 0
905                         File.delete(*to_del.collect { |e| File.join(dest_dir, e) })
906                     end
907                 end
908                 
909                 #- copy any resource file that goes with the theme (css, images..)
910                 themestuff = Dir.entries("#{$themedir}").
911                                  find_all { |e| !%w(. .. skeleton_image.html skeleton_thumbnails.html skeleton_index.html metadata root).include?(e) }
912                 themestuff.each { |entry|
913                     if !File.exists?(File.join(dest_dir, entry))
914                         psys("cp '#{$themedir}/#{entry}' '#{dest_dir}'")
915                     end
916                 }
917
918                 #- copy any root-only resource file that goes with the theme (css, images..)
919                 if xmldir.parent.name != 'dir'
920                     themestuff_root = Dir.entries("#{$themedir}/root").
921                                           find_all { |e| !%w(. ..).include?(e) }
922                     themestuff_root.each { |entry|
923                         if !File.exists?(File.join(dest_dir, entry))
924                             psys("cp '#{$themedir}/root/#{entry}' '#{dest_dir}'")
925                         end
926                     }
927                 end
928             end
929             next
930         end
931
932         msg 2, _("Outputting in %s...") % dest_dir
933
934         #- populate data structure with sizes from theme
935         for sizeobj in $images_size
936             fullscreen_images ||= {}
937             fullscreen_images[sizeobj['name']] = []
938             thumbnail_images ||= {}
939             thumbnail_images[sizeobj['name']] = []
940             thumbnail_videos ||= {}
941             thumbnail_videos[sizeobj['name']] = []
942         end
943         #- a special dummy size to keep 'references' to thumbnails in case of panorama, because the GUI will use the regular thumbnails
944         thumbnail_images['dont-delete-file-for-gui'] = []
945         if $limit_sizes =~ /original/
946             fullscreen_images['original'] = []
947         end
948
949         images.size >= 1 and msg 3, _("\tcreating photos thumbnails...")
950
951         #- create thumbnails for images
952         images.each { |img|
953             info("processing element")
954             base_dest_img = dest_dir + '/' + make_dest_filename(img.sub(/\.[^\.]+$/, ''))
955             elem = optelements['image|' + utf8(img)]
956             if $forgui
957                 thumbnail_dest_img = base_dest_img + "-#{$default_size['thumbnails']}.jpg"
958                 gen_thumbnails_element("#{dir}/#{img}", elem, true, [ { 'filename' => thumbnail_dest_img, 'size' => $default_size['thumbnails'] } ])
959             else
960                 todo = []
961                 for sizeobj in $images_size
962                     size_fullscreen = sizeobj['fullscreen']
963                     size_thumbnails = sizeobj['thumbnails']
964                     fullscreen_dest_img = base_dest_img + "-#{size_fullscreen}.jpg"
965                     fullscreen_images[sizeobj['name']] << File.basename(fullscreen_dest_img)
966                     todo << { 'filename' => fullscreen_dest_img, 'size' => size_fullscreen }
967                     if pano = pano_amount(elem)
968                         thumbnail_images['dont-delete-file-for-gui'] << File.basename(base_dest_img + "-#{size_thumbnails}.jpg")
969                         size_thumbnails = size_thumbnails.sub(/(\d+)/) { ($1.to_i * pano).to_i }
970                     end
971                     thumbnail_dest_img = base_dest_img + "-#{size_thumbnails}.jpg"
972                     thumbnail_images[sizeobj['name']] << File.basename(thumbnail_dest_img)
973                     todo << { 'filename' => thumbnail_dest_img,  'size' => size_thumbnails }
974                 end
975                 gen_thumbnails_element("#{dir}/#{img}", elem, true, todo)
976                 if $limit_sizes =~ /original/
977                     fullscreen_images['original'] << img
978                 end
979                 destimg = "#{dest_dir}/#{img}"
980                 if $limit_sizes =~ /original/ && !File.exists?(destimg)
981                     if $hardlinks_ok
982                         if ! sys("ln '#{dir}/#{img}' '#{destimg}'")
983                             $hardlinks_ok = false
984                         end
985                     end
986                     if ! $hardlinks_ok
987                         psys("cp '#{dir}/#{img}' '#{destimg}'")
988                     end
989                 end
990             end
991         }
992
993         videos.size >= 1 and msg 3, _("\tcreating videos thumbnails...")
994         transcoded_videos = {}
995
996         #- create thumbnails for videos
997         videos.each { |video|
998             info("processing element")
999             elem = optelements['video|' + utf8(video)]
1000             if $forgui
1001                 thumbnail_dest_img = dest_dir + '/' + make_dest_filename(video.sub(/\.[^\.]+$/, '')) + "-#{$default_size['thumbnails']}.jpg"
1002                 gen_thumbnails_element("#{dir}/#{video}", elem, true, [ { 'filename' => thumbnail_dest_img, 'size' => $default_size['thumbnails'] } ])
1003             else
1004                 todo = []
1005                 for sizeobj in $images_size
1006                     size_thumbnails = sizeobj['thumbnails']
1007                     thumbnail_dest_img = dest_dir + '/' + make_dest_filename(video.sub(/\.[^\.]+$/, '')) + "-#{size_thumbnails}.jpg"
1008                     thumbnail_videos[sizeobj['name']] << File.basename(thumbnail_dest_img)
1009                     todo << { 'filename' => thumbnail_dest_img, 'size' => size_thumbnails }
1010                 end
1011                 gen_thumbnails_element("#{dir}/#{video}", elem, true, todo)
1012
1013                 if $transcode_videos
1014                     parts = $transcode_videos.split(':', 2)
1015                     basedestvideo = video.sub(/\.\w+/, '') + '.' + parts[0]
1016                     transcoded_videos[video] = basedestvideo
1017                     destvideo = "#{dest_dir}/#{basedestvideo}"
1018                     if ! File.exists?(destvideo)
1019                         psys(parts[1].gsub(/%f/, "'#{dir}/#{video}'").gsub(/%o/, "'#{destvideo}'"))
1020                     end
1021                 else
1022                     destvideo = "#{dest_dir}/#{video}"
1023                     if ! File.exists?(destvideo)
1024                         if $hardlinks_ok
1025                             if ! sys("ln '#{dir}/#{video}' '#{destvideo}'")
1026                                 $hardlinks_ok = false
1027                             end
1028                         end
1029                         if ! $hardlinks_ok
1030                             psys("cp '#{dir}/#{video}' '#{destvideo}'")
1031                         end
1032                     end
1033                 end
1034             end
1035         }
1036
1037         if !$forgui            
1038             #- cleanup old images/videos (for when removing elements or sizes)
1039             all_elements = fullscreen_images.collect { |e| e[1] }.flatten.
1040                      concat(thumbnail_images.collect { |e| e[1] }.flatten).
1041                      concat(thumbnail_videos.collect { |e| e[1] }.flatten).
1042                      concat($transcode_videos ? transcoded_videos.values : videos).
1043                      push('.htaccess')
1044             to_del = Dir.entries(dest_dir).find_all { |e| !File.directory?(File.join(dest_dir, e)) && !all_elements.include?(e) && e !~ /^thumbnails-\w+\.jpg/ }
1045             if to_del.size > 0
1046                 msg 3, _("\tcleaning up: #{to_del.join(', ')}")
1047                 File.delete(*to_del.collect { |e| File.join(dest_dir, e) })
1048             end
1049
1050             #- copy any resource file that goes with the theme (css, images..)
1051             themestuff = Dir.entries("#{$themedir}").
1052                              find_all { |e| !%w(. .. skeleton_image.html skeleton_thumbnails.html skeleton_index.html metadata root).include?(e) }
1053             themestuff.each { |entry|
1054                 if !File.exists?(File.join(dest_dir, entry))
1055                     psys("cp '#{$themedir}/#{entry}' '#{dest_dir}'")
1056                 end
1057             }
1058             
1059             #- copy any root-only resource file that goes with the theme (css, images..)
1060             if xmldir.parent.name != 'dir'
1061                 themestuff_root = Dir.entries("#{$themedir}/root").
1062                                       find_all { |e| !%w(. ..).include?(e) }
1063                 themestuff_root.each { |entry|
1064                     if !File.exists?(File.join(dest_dir, entry))
1065                         psys("cp '#{$themedir}/root/#{entry}' '#{dest_dir}'")
1066                     end
1067                 }
1068             end
1069             
1070             msg 3, _("\tgenerating HTML pages...")
1071             #- fixup max per page
1072             if $N_per_page
1073                 $N_per_page = $N_per_page.to_i / ($N_per_row || $default_N).to_i * ($N_per_row || $default_N).to_i
1074             end
1075
1076             #- generate thumbnails*.html (page with thumbnails)
1077             image2thumbnailpage4js = []
1078             for sizeobj in $images_size
1079                 info("processing size")
1080                 html = $html_thumbnails.collect { |l| l.clone }
1081                 iterations = {}
1082                 for i in html
1083                     i.sub!(/~~title~~/, quote_caption(xmldir.attributes['thumbnails-caption'] || utf8(File.basename(dir))))
1084                     discover_iterations(iterations, i)
1085                 end
1086                 all_pages = []
1087                 html_thumbnails = ''
1088                 html_thumbnails_nojs = ''
1089                 counter = 0
1090                 pagecount = 0
1091                 reset_iterations(iterations)
1092                 #- preprocess the @filename->elem, rexml is very slow with that; we dramatically improve this part of the processing
1093                 optfilename = {}
1094                 xmldir.elements.each('image') { |elem|
1095                     optfilename[elem.attributes['filename']] = elem
1096                 }
1097                 for file in entries
1098                     type = images.include?(file) ? 'image' : videos.include?(file) ? 'video' : nil
1099                     if type
1100                         homogeinize_width = 100 / ($N_per_row || $default_N).to_i
1101                         if type == 'image' && elem = optfilename[utf8(file)]
1102                             if pano = pano_amount(elem)
1103                                 html_elem = run_iterations(iterations, pano)
1104                                 counter += count = pano.ceil
1105                                 html_elem.gsub!(/~~colspan~~/) { "colspan=\"#{count}\"" }
1106                                 homogeinize_width *= count
1107                             else
1108                                 html_elem = run_iterations(iterations, 1)
1109                                 counter += 1
1110                                 html_elem.gsub!(/~~colspan~~/, '')
1111                             end
1112                         else 
1113                             html_elem = run_iterations(iterations, 1)
1114                             counter += 1
1115                             html_elem.gsub!(/~~colspan~~/, '')
1116                         end
1117                         html_elem.gsub!(/~~homogeinize_width~~/) { "width=\"#{homogeinize_width}%\"" }
1118                         if type == 'image'
1119                             index = images.index(file)
1120                             caption = find_caption_value(xmldir, images[index]) || utf8(images[index])
1121                             html_elem.gsub!(/~~caption_iteration~~/, quote_caption(caption))
1122                             html_elem.gsub!(/~~ifimage\?~~(.+?)~~fi~~/) { $1 }
1123                             html_elem.gsub!(/~~ifvideo\?~~(.+?)~~fi~~/, '')
1124                         elsif type == 'video'
1125                             index = videos.index(file)
1126                             caption = find_caption_value(xmldir, videos[index]) || utf8(videos[index])
1127                             if File.exists?("#{dest_dir}/#{thumbnail_videos[sizeobj['name']][index]}")
1128                                 html_elem.gsub!(/~~image_iteration~~/,
1129                                                 '<a href="' + ( $transcode_videos ? transcoded_videos[videos[index]] : videos[index] ) + '">' +
1130                                                     img_element("#{dest_dir}/#{thumbnail_videos[sizeobj['name']][index]}", caption) + '</a>')
1131                             else
1132                                 html_elem.gsub!(/~~image_iteration~~/,
1133                                                 '<a href="' + ( $transcode_videos ? transcoded_videos[videos[index]] : videos[index] ) + '">' +
1134                                                     defer_translation(N_("(no preview)")) + '</a>')
1135                             end
1136                             html_elem.gsub!(/~~caption_iteration~~/, quote_caption(caption))
1137                             html_elem.gsub!(/~~ifimage\?~~(.+?)~~fi~~/, '')
1138                             html_elem.gsub!(/~~ifvideo\?~~(.+?)~~fi~~/) { $1 }
1139                         end
1140                         html_thumbnails += html_elem
1141                         html_thumbnails_nojs += html_elem
1142                         if type == 'image'
1143                             html_thumbnails.gsub!(/~~image_iteration~~/,
1144                                                   '<a href="image-' + size2js(sizeobj['name']) + $htmlsuffix + '#current=' + fullscreen_images[sizeobj['name']][index] +
1145                                                       '" name="' + fullscreen_images[sizeobj['name']][index] + '">' +
1146                                                       img_element("#{dest_dir}/#{thumbnail_images[sizeobj['name']][index]}", caption) + '</a>')
1147                             html_thumbnails_nojs.gsub!(/~~image_iteration~~/, 
1148                                                        '<a href="' + fullscreen_images[sizeobj['name']][index] + '" name="' + fullscreen_images[sizeobj['name']][index] + '">' +
1149                                                        img_element("#{dest_dir}/#{thumbnail_images[sizeobj['name']][index]}", caption) + '</a>')
1150                             #- remember in which thumbnails page is this element, for image->thumbnail link
1151                             if sizeobj == $images_size[0]
1152                                 image2thumbnailpage4js << pagecount
1153                             end
1154                         end
1155
1156                         if counter == $N_per_page
1157                             html_thumbnails      += close_iterations(iterations)
1158                             html_thumbnails_nojs += close_iterations(iterations)
1159                             all_pages << [ html_thumbnails, html_thumbnails_nojs ]
1160                             html_thumbnails = ''
1161                             html_thumbnails_nojs = ''
1162                             counter = 0
1163                             reset_iterations(iterations)
1164                             pagecount += 1
1165                         end
1166                     end
1167                 end
1168                 if counter > 0
1169                     html_thumbnails      += close_iterations(iterations)
1170                     html_thumbnails_nojs += close_iterations(iterations)
1171                     all_pages << [ html_thumbnails, html_thumbnails_nojs ]
1172                 end
1173                 for i in html
1174                     i.gsub!(/~~theme~~/, $theme)
1175                     i.gsub!(/~~current_size~~/, sizeobj['name'])
1176                     i.gsub!(/~~htmlsuffix~~/, $htmlsuffix)
1177                     i.gsub!(/~~current_size_js~~/, size2js(sizeobj['name']))
1178                     i.gsub!(/~~madewith~~/, $madewith || '')
1179                     i.gsub!(/~~indexlink~~/, $indexlink || '')
1180                     substitute_navigation(i, xmldir)
1181                 end
1182                 html_nojs = html.collect { |l| l.clone }
1183                 pagecount = 0
1184                 for page in all_pages
1185                     html_thumbnails, html_thumbnails_nojs = page
1186                     final_html = html.collect { |l| l.clone }
1187                     mstuff = defer_translation(N_("Pages: ")) +
1188                              (pagecount > 0 ? "<a href=\"thumbnails-#{size2js(sizeobj['name'])}%nojs-#{pagecount - 1}#{$htmlsuffix}\">" + defer_translation(N_("<- Previous")) + "</a> " : '') +
1189                              all_pages.collect_with_index { |p,idx| page == p ? idx + 1 : "<a href=\"thumbnails-#{size2js(sizeobj['name'])}%nojs-#{idx}#{$htmlsuffix}\">#{idx + 1}</a>" }.join(', ') +
1190                              (pagecount < all_pages.size - 1 ? " <a href=\"thumbnails-#{size2js(sizeobj['name'])}%nojs-#{pagecount + 1}#{$htmlsuffix}\">" + defer_translation(N_("Next ->")) + "</a> " : '')
1191                     for i in final_html
1192                         i.sub!(/~~run_slideshow~~/,
1193                                images.size <= 1 ? '' : '<a href="image-' + size2js(sizeobj['name']) + $htmlsuffix + '#run_slideshow=1">' + defer_translation(N_("Run slideshow!"))+'</a>')
1194                         i.sub!(/~~thumbnails~~/, html_thumbnails)
1195                         if all_pages.size == 1
1196                             i.gsub!(/~~ifmultiplepages\?~~.*~~fi~~/, '')
1197                         else
1198                             i.gsub!(/~~ifmultiplepages\?~~(.+?)~~fi~~/) { $1 }
1199                             i.gsub!(/~~multiplepagesstuff~~/, mstuff.gsub('%nojs', ''))
1200                         end
1201                         substitute_html_sizes(i, sizeobj, 'thumbnails', "-#{pagecount}")
1202                         substitute_pathtobase(i, xmldir)
1203                     end
1204                     save_html(final_html, "#{dest_dir}/thumbnails-#{size2js(sizeobj['name'])}-#{pagecount}")
1205                     final_html_nojs = html_nojs.collect { |l| l.clone }
1206                     for i in final_html_nojs
1207                         i.sub!(/~~run_slideshow~~/, defer_translation(N_("<i>Click on an image to view it larger</i>")))
1208                         i.sub!(/~~thumbnails~~/, html_thumbnails_nojs)
1209                         if all_pages.size == 1
1210                             i.gsub!(/~~ifmultiplepages\?~~.*~~fi~~/, '')
1211                         else
1212                             i.gsub!(/~~ifmultiplepages\?~~(.+?)~~fi~~/) { $1 }
1213                             i.gsub!(/~~multiplepagesstuff~~/, mstuff.gsub('%nojs', '-nojs'))
1214                         end
1215                         substitute_html_sizes(i, sizeobj, 'thumbnails', "-nojs-#{pagecount}")
1216                         substitute_pathtobase(i, xmldir)
1217                     end
1218                     save_html(final_html_nojs, "#{dest_dir}/thumbnails-#{size2js(sizeobj['name'])}-nojs-#{pagecount}")
1219                     pagecount += 1
1220                 end
1221             end
1222
1223             info("finished processing sizes")
1224
1225             #- generate "main" thumbnails.html page that will reload to correct size thanks to cookie
1226             save_html(html_reload_to_thumbnails, "#{dest_dir}/thumbnails")
1227
1228             #- generate image.html (page with fullscreen images)
1229             if images.size > 0
1230                 captions4js = find_captions(xmldir, images).collect { |e| e ? '"' + e.gsub('"', '\"') + '"' : '""' }.join(', ')
1231                 thumbnailspage4js = image2thumbnailpage4js.collect { |e| "\"#{e}\"" }.join(', ')
1232                 for sizeobj in $images_size
1233                     html = $html_images.collect { |l| l.clone }
1234                     images4js = fullscreen_images[sizeobj['name']].collect { |e| "\"#{e}\"" }.join(', ')
1235                     otherimages4js = ''
1236                     othersizes = []
1237                     for sizeobj2 in all_images_sizes
1238                         if sizeobj != sizeobj2
1239                             otherimages4js += "var images_#{size2js(sizeobj2['name'])} = new Array(" + fullscreen_images[sizeobj2['name']].collect { |e| "\"#{e}\"" }.join(', ') + ")\n"
1240                             othersizes << "\"#{size2js(sizeobj2['name'])}\""
1241                         end
1242                     end
1243                     for i in html
1244                         i.gsub!(/~~images~~/, images4js)
1245                         i.gsub!(/~~other_images~~/, otherimages4js)
1246                         i.gsub!(/~~thumbnailspages~~/, thumbnailspage4js)
1247                         i.gsub!(/~~other_sizes~~/, othersizes.join(', '))
1248                         i.gsub!(/~~captions~~/, captions4js)
1249                         i.gsub!(/~~title~~/, quote_caption(xmldir.attributes['thumbnails-caption'] || utf8(File.basename(dir))))
1250                         i.gsub!(/~~thumbnails~~/, '<a href="thumbnails-' + size2js(sizeobj['name']) + $htmlsuffix + '" id="thumbnails">' + defer_translation(N_('return to thumbnails')) + '</a>')
1251                         i.gsub!(/~~theme~~/, $theme)
1252                         i.gsub!(/~~current_size~~/, size2js(sizeobj['name']))
1253                         i.gsub!(/~~htmlsuffix~~/, $htmlsuffix)
1254                         i.gsub!(/~~madewith~~/, $madewith || '')
1255                         i.gsub!(/~~indexlink~~/, $indexlink || '')
1256                         substitute_html_sizes(i, sizeobj, 'image', '')
1257                         substitute_navigation(i, xmldir)
1258                         substitute_pathtobase(i, xmldir)
1259                     end
1260                     save_html(html, "#{dest_dir}/image-#{size2js(sizeobj['name'])}")
1261                 end
1262             end
1263         end
1264     }
1265
1266     msg 3, ''
1267
1268     #- add attributes to <dir..> elements needing so
1269     if $mode != 'use_config'
1270         msg 3, _("\tfixating configuration file...")
1271         $xmldoc.elements.each('//dir') { |element|
1272             path = captionpath = element.attributes['path']
1273             descendant_element = nil
1274             #- search if there is at least one image or video down that dir; use workarounds to rexml slowness
1275             #- first, look down only one level, very fast
1276             first_desc = nil
1277             element.elements.each { |elem|
1278                 if first_desc.nil?
1279                     first_desc = elem
1280                 end
1281                 if elem.name == 'image' || elem.name == 'video'
1282                     descendant_element = elem
1283                     break
1284                 end
1285             }
1286             if descendant_element.nil?
1287                 #- if there was nothing or only directory down one level, look down the first subdir
1288                 if ! first_desc.nil?
1289                     first_desc.elements.each { |elem|
1290                         if elem.name == 'image' || elem.name == 'video'
1291                             descendant_element = elem
1292                             break
1293                         end
1294                     }
1295                 end
1296                 if descendant_element.nil?
1297                     #- if there was still nothing found, use complete albeit slow method
1298                     descendant_element = element.elements['descendant::image'] || element.elements['descendant::video']
1299                 end
1300             end
1301             if !descendant_element
1302                 msg 3, _("\t\tremoving %s, no element in it") % path
1303                 element.remove  #- means we have a directory with nothing interesting in it
1304             else
1305                 captionfile = "#{descendant_element.parent.attributes['path']}/#{descendant_element.attributes['filename']}"
1306                 basename = File.basename(path)
1307                 if element.elements['dir']
1308                     if !element.attributes['subdirs-caption']
1309                         element.add_attribute('subdirs-caption', basename)
1310                     end
1311                     if !element.attributes['subdirs-captionfile']
1312                         element.add_attribute('subdirs-captionfile', captionfile)
1313                     end
1314                 end
1315                 if element.child_byname_notattr('image', 'deleted') || element.child_byname_notattr('video', 'deleted')
1316                     if !element.attributes['thumbnails-caption']
1317                         element.add_attribute('thumbnails-caption', basename)
1318                     end
1319                     if !element.attributes['thumbnails-captionfile']
1320                         element.add_attribute('thumbnails-captionfile', captionfile)
1321                     end
1322                 end
1323             end
1324         }
1325     end
1326
1327     #- write down to disk config if necessary
1328     if $config_writeto
1329         ios = File.open($config_writeto, "w")
1330         $xmldoc.write(ios, 0)
1331         ios.close
1332     end
1333
1334     if $forgui
1335         msg 3, _("completed necessary stuff for GUI, exiting.")
1336         return
1337     end
1338
1339     #- second pass to create index.html files and previous/next links
1340     info("creating index.html")
1341     msg 3, _("\trescanning directories to generate all 'index.html' files...")
1342
1343     #- recompute the memoization because elements mights have been removed (the ones with no element in them)
1344     optxpath = {}
1345     $xmldoc.elements.each('//dir') { |elem|
1346         optxpath[elem.attributes['path']] = elem
1347     }
1348
1349     examined_dirs.each { |dir|
1350         info("index.html: #{dir}|#{$source}")
1351
1352         xmldir = optxpath[utf8(dir)]
1353         if !xmldir || (xmldir.attributes['already-generated'] && !$force) || xmldir.attributes['deleted']
1354             next
1355         end
1356         dest_dir = make_dest_filename(dir.sub(/^#{Regexp.quote($source)}/, $dest))
1357
1358         previous_album = find_previous_album(xmldir)
1359         next_album = find_next_album(xmldir)
1360
1361         if xmldir.elements['dir']
1362             html = $html_index.collect { |l| l.clone }
1363             iterations = {}
1364             for i in html
1365                 caption = xmldir.attributes['subdirs-caption']
1366                 i.gsub!(/~~title~~/, quote_caption(caption))
1367                 substitute_navigation(i, xmldir)
1368                 substitute_pathtobase(i, xmldir)
1369                 discover_iterations(iterations, i)
1370             end
1371             
1372             html_index = ''
1373             reset_iterations(iterations)
1374             
1375             #- deal with "current" album (directs to "thumbnails" page)
1376             if xmldir.attributes['thumbnails-caption']
1377                 thumbnail = "#{dest_dir}/thumbnails-thumbnail.jpg"
1378                 gen_thumbnails_subdir(from_utf8(xmldir.attributes['thumbnails-captionfile']), xmldir, false,
1379                                       [ { 'filename' => thumbnail, 'size' => $albums_thumbnail_size } ], 'thumbnails')
1380                 html_index += run_iterations(iterations, 1)
1381                 caption = xmldir.attributes['thumbnails-caption']
1382                 html_index.gsub!(/~~image_iteration~~/, "<a href=\"thumbnails#{$htmlsuffix}\">" + img_element(thumbnail, caption) + '</a>')
1383                 html_index.gsub!(/~~caption_iteration~~/, quote_caption(caption))
1384             end
1385
1386             #- deal with sub-albums (direct to subdirs/index.html pages)
1387             xmldir.elements.each('dir') { |child|
1388                 if child.attributes['deleted']
1389                     next
1390                 end
1391                 subdir = make_dest_filename(from_utf8(File.basename(child.attributes['path'])))
1392                 thumbnail = "#{dest_dir}/thumbnails-#{subdir}.jpg"
1393                 html_index += run_iterations(iterations, 1)
1394                 captionfile, caption = find_subalbum_caption_info(child)
1395                 gen_thumbnails_subdir(captionfile, child, false,
1396                                       [ { 'filename' => thumbnail, 'size' => $albums_thumbnail_size } ], find_subalbum_info_type(child))
1397                 html_index.gsub!(/~~caption_iteration~~/, quote_caption(caption))
1398                 html_index.gsub!(/~~image_iteration~~/, "<a href=\"#{subdir}/index#{$htmlsuffix}\">" + img_element(thumbnail, caption) + '</a>')
1399             }
1400
1401             html_index += close_iterations(iterations)
1402
1403             for i in html
1404                 i.gsub!(/~~thumbnails~~/, html_index)
1405                 i.gsub!(/~~madewith~~/, $madewith || '')
1406                 i.gsub!(/~~indexlink~~/, $indexlink || '')
1407             end
1408             
1409         else
1410             html = html_reload_to_thumbnails
1411         end
1412
1413         save_html(html, "#{dest_dir}/index")
1414         if $multi_languages
1415             #- in case MultiViews will not work, generate some compat
1416             ios = File.open("#{dest_dir}/index.html", "w")
1417             ios.write("<html><head><meta http-equiv=\"refresh\" content=\"0.1;url=index.#{$multi_languages[1]}.html\"></head></html>")
1418             ios.close
1419         end
1420
1421         #- substitute multiple "return to albums", previous/next correctly
1422         #- the following two statements are dramatical optimizations to executing for each substInFile callback
1423         dirpresent = xmldir.elements['dir']
1424         parentname = xmldir.parent.name
1425         if xmldir.child_byname_notattr('image', 'deleted') || xmldir.child_byname_notattr('video', 'deleted')
1426             for suffix in [ '', '-nojs' ]
1427                 for sizeobj in $images_size
1428                     Dir.glob("#{dest_dir}/thumbnails-#{size2js(sizeobj['name'])}#{suffix}-*.html") do |file|
1429                         #- unroll translations, they are costly if rerun for each line of files
1430                         rta = find_translation_for_file(file, N_('return to albums'))
1431                         pa = find_translation_for_file(file, N_('previous album'))
1432                         na = find_translation_for_file(file, N_('next album'))
1433                         substInFile(file) { |line|
1434                             sub_previous_next_album(file, previous_album, next_album, line, pa, na)
1435                             if dirpresent
1436                                 line.sub!(/~~return_to_albums~~/, '<a href="index' + $htmlsuffix + '">' + rta + '</a>')
1437                             else
1438                                 if parentname == 'dir'
1439                                     line.sub!(/~~return_to_albums~~/, '<a href="../index' + $htmlsuffix + '">' + rta + '</a>')
1440                                 else
1441                                     line.sub!(/~~return_to_albums~~/, '')
1442                                 end
1443                             end
1444                             line
1445                         }
1446                     end
1447                     if suffix == '' && xmldir.child_byname_notattr('image', 'deleted')
1448                         Dir.glob("#{dest_dir}/image-#{size2js(sizeobj['name'])}*.html") do |file|
1449                             pa = find_translation_for_file(file, N_('previous album'))
1450                             na = find_translation_for_file(file, N_('next album'))
1451                             substInFile(file) { |line|
1452                                 sub_previous_next_album(file, previous_album, next_album, line, pa, na)
1453                             }
1454                         end
1455                     end
1456                 end
1457             end
1458         end
1459     }
1460
1461     msg 3, _(" all done.")
1462 end
1463
1464 handle_options
1465 read_config
1466 check_installation
1467
1468 build_html_skeletons
1469
1470 walk_source_dir