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