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