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