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