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