5 # A.k.a 'Best web-album Of the world, Or your money back, Humerus'.
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.
13 # Copyright (c) 2004-2009 Guillaume Cottenceau <http://zarb.org/~gc/resource/gc_mail.png>
15 # This software may be freely redistributed under the terms of the GNU
16 # public license version 2.
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
24 #- new style (ruby-locale separated package)
27 #- old style (locale stuff in ruby-gettext package)
28 require 'gettext/locale'
32 require 'booh/rexml/document'
35 require 'booh/booh-lib'
36 require 'booh/html-merges'
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
45 [ '--help', '-h', GetoptLong::NO_ARGUMENT, _("Get help message") ],
46 [ '--version', '-V', GetoptLong::NO_ARGUMENT, _("Print version and exit") ],
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!") ],
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") ],
61 [ '--sizes', '-S', GetoptLong::REQUIRED_ARGUMENT, _("Specify the list of images sizes to use instead of all specified in the theme (this is a comma-separated list)") ],
62 [ '--multi-languages', '-L', GetoptLong::REQUIRED_ARGUMENT, _("Specify the list of languages to support (uses Apache MultiViews); this is a comma-separated list of supported languages, with last element used as the fallback language; for example: 'fr,eo,en,en'; supported languages: %s") % SUPPORTED_LANGUAGES.join(', ') ],
63 [ '--thumbnails-per-row', '-T', GetoptLong::REQUIRED_ARGUMENT, _("Specify the amount of thumbnails per row in the thumbnails page (if applicable in theme)") ],
64 [ '--thumbnails-per-page', '-p', GetoptLong::REQUIRED_ARGUMENT, _("Specify the amount of thumbnails per page in the thumbnails page, after which split occurs") ],
65 [ '--optimize-for-32', '-o', GetoptLong::NO_ARGUMENT, _("Resize images with optimized sizes for 3/2 aspect ratio rather than 4/3 (typical aspect ratio of photos from point-and-shoot cameras - also called compact cameras - is 4/3, whereas photos from SLR cameras - also called reflex cameras - is 3/2)") ],
66 [ '--transcode-videos', '-r', GetoptLong::REQUIRED_ARGUMENT, _("Transcode videos with given external program; %f is the placeholder for the input video, %o for the output video; before the external program, the output video extension should be given followed by a colon") ],
67 [ '--index-link', '-l', GetoptLong::REQUIRED_ARGUMENT, _("Specify the HTML markup to use on the bottom of pages for a small link returning to wherever you see fit in your website (or somewhere else)") ],
68 [ '--made-with', '-n', GetoptLong::REQUIRED_ARGUMENT, _("Specify the HTML markup to use on the bottom of pages for a small 'made with' message") ],
69 [ '--comments-format','-c', GetoptLong::REQUIRED_ARGUMENT, _("Specify comments format to use for images instead of only filename when creating new albums; use ImageMagick's format") ],
71 [ '--mproc', '-m', GetoptLong::REQUIRED_ARGUMENT, _("Specify the number of processors for multi-processors machines") ],
73 [ '--for-gui', '-g', GetoptLong::NO_ARGUMENT, _("Do the minimum work to be able to see the album under the GUI (don't generate all thumbnails)") ],
75 [ '--verbose-level', '-v', GetoptLong::REQUIRED_ARGUMENT, _("Set max verbosity level (0: errors, 1: warnings, 2: important messages, 3: other messages)") ],
76 [ '--info-pipe', '-i', GetoptLong::REQUIRED_ARGUMENT, _("Name a file where to write information about what's going on (used by the GUI)") ],
79 #- default values for some globals
83 $ignore_videos = false
88 puts _("Usage: %s [OPTION]...") % File.basename($0)
90 printf " %3s, %-18s %s\n", ary[1], ary[0], ary[3]
95 parser = GetoptLong.new
96 parser.set_options(*$options.collect { |ary| ary[0..2] })
98 parser.each_option do |name, arg|
105 puts _("Booh version %s
107 Copyright (c) 2005-2009 Guillaume Cottenceau.
108 This is free software; see the source for copying conditions. There is NO
109 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.") % $VERSION
114 $source = File.expand_path(arg.sub(%r|/$|, ''))
115 if !File.directory?($source)
116 die _("Argument to --source must be a directory")
119 $dest = File.expand_path(arg.sub(%r|/$|, ''))
120 if File.exists?($dest) && !File.directory?($dest)
121 die _("If --destination exists, it must be a directory")
123 if $dest != make_dest_filename($dest)
124 die _("Sorry, destination directory can't contain non simple alphanumeric characters.")
127 # system("rm -rf #{$dest}")
132 arg = File.expand_path(arg)
133 if File.readable?(arg)
134 $xmldoc = REXML::Document.new File.new(arg)
137 die _('Config file does not exist or is unreadable.')
140 arg = File.expand_path(arg)
142 if File.directory?(arg)
143 die _("Config skeleton file (%s) already exists and is a directory! Please change the filename.") % arg
145 msg 1, _("Config skeleton file already exists, backuping to %s.backup") % arg
146 File.rename(arg, "#{arg}.backup")
149 $config_writeto = arg
151 when '--merge-config'
152 arg = File.expand_path(arg)
153 if File.readable?(arg)
154 msg 2, _("Merge config notice: backuping current config file to %s.backup") % arg
155 $xmldoc = REXML::Document.new File.new(arg)
156 File.rename(arg, "#{arg}.backup")
157 $config_writeto = arg
158 $mode = 'merge_config'
160 die _('Config file does not exist or is unreadable.')
162 when '--merge-config-onedir'
163 arg = File.expand_path(arg)
164 if File.readable?(arg)
165 msg 2, _("Merge config notice: backuping current config file to %s.backup") % arg
166 $xmldoc = REXML::Document.new File.new(arg)
167 File.rename(arg, "#{arg}.backup")
168 $config_writeto = arg
169 $mode = 'merge_config_onedir'
171 die _('Config file does not exist or is unreadable.')
173 when '--merge-config-subdirs'
174 arg = File.expand_path(arg)
175 if File.readable?(arg)
176 msg 2, _("Merge config notice: backuping current config file to %s.backup") % arg
177 $xmldoc = REXML::Document.new File.new(arg)
178 File.rename(arg, "#{arg}.backup")
179 $config_writeto = arg
180 $mode = 'merge_config_subdirs'
182 die _('Config file does not exist or is unreadable.')
185 arg = File.expand_path(arg)
186 if !File.readable?(arg)
187 die _('Specified directory to merge with --dir is not readable')
192 arg = File.expand_path(arg)
193 if File.readable?(arg)
194 msg 2, _("Use config notice: backuping current config file to %s.backup") % arg
195 $xmldoc = REXML::Document.new File.new(arg)
196 File.rename(arg, "#{arg}.backup")
197 $config_writeto = arg
198 $mode = 'use_config_changetheme'
200 die _('Config file does not exist or is unreadable.')
206 when '--multi-languages'
207 parts = arg.split(',')
208 if parts.size == 0 || parts.find_all { |e| ! SUPPORTED_LANGUAGES.include?(e.strip) }.size > 0
209 die _("--multi-languages: argument must be a comma-separated list of supported languages, with last element used as the fallback language; for example: 'fr,eo,en,en'; supported languages: %s") % SUPPORTED_LANGUAGES.join(', ')
211 $multi_languages = [ parts[0..-2], parts[-1] ]
213 when '--thumbnails-per-row'
216 when '--thumbnails-per-page'
219 when '--optimize-for-32'
220 $optimize_for_32 = true
222 when '--transcode-videos'
223 parts = arg.split(':', 2)
224 if parts.size != 2 || parts[0] =~ / / || arg !~ /%f/ || arg !~ /%o/
225 die _("--transcode-videos: argument must be the external program for transcoding, and contain %f for the input video, %o for the output video, and before the external program, the output video extension should be given followed by a colon")
227 $transcode_videos = arg
235 when '--comments-format'
236 $commentsformat = arg
248 when '--verbose-level'
249 $verbose_level = arg.to_i
252 $info_pipe = File.open(arg, File::WRONLY)
253 $info_pipe.sync = true
262 if !$source && $xmldoc
263 $source = from_utf8($xmldoc.root.attributes['source']).sub(%r|/$|, '')
264 $dest = from_utf8($xmldoc.root.attributes['destination']).sub(%r|/$|, '')
265 $theme ||= $xmldoc.root.attributes['theme']
266 $limit_sizes ||= $xmldoc.root.attributes['limit-sizes']
267 if $mode == 'use_config' || $mode =~ /^merge_config/
268 $optimize_for_32 = !$xmldoc.root.attributes['optimize-for-32'].nil?
269 $N_per_row = $xmldoc.root.attributes['thumbnails-per-row']
270 languages = $xmldoc.root.attributes['multi-languages']
272 languages = languages.split(',')
273 $multi_languages = [ languages[0..-2], languages[-1] ]
275 $N_per_page = $xmldoc.root.attributes['thumbnails-per-page']
276 $madewith = $xmldoc.root.attributes['made-with']
277 $indexlink = $xmldoc.root.attributes['index-link']
281 if $mode == 'merge_config_onedir' && !$onedir
282 die _("Missing --dir for --merge-config-onedir")
284 if $mode == 'merge_config_subdirs' && !$onedir
285 die _("Missing --dir for --merge-config-subdirs")
293 die _("Missing --destination parameter.")
299 select_theme($theme, $limit_sizes, $optimize_for_32, $N_per_row)
302 $xmldoc = Document.new "<booh/>"
303 $xmldoc << XMLDecl.new(XMLDecl::DEFAULT_VERSION, $CURRENT_CHARSET)
304 $xmldoc.root.add_attribute('version', $VERSION)
305 $xmldoc.root.add_attribute('source', $source)
306 $xmldoc.root.add_attribute('destination', $dest)
307 $xmldoc.root.add_attribute('theme', $theme)
309 $xmldoc.root.add_attribute('limit-sizes', $limit_sizes)
312 $xmldoc.root.add_attribute('multi-languages', $multi_languages[0].join(',') + ',' + $multi_languages[1])
315 $xmldoc.root.add_attribute('optimize-for-32', 'true')
318 $xmldoc.root.add_attribute('thumbnails-per-row', $N_per_row)
321 $xmldoc.root.add_attribute('thumbnails-per-page', $N_per_page)
324 $xmldoc.root.add_attribute('made-with', $madewith)
327 $xmldoc.root.add_attribute('index-link', $indexlink)
332 if $mode == 'merge_config' || $mode == 'use_config_changetheme'
333 $xmldoc.root.add_attribute('theme', $theme)
334 $xmldoc.root.add_attribute('version', $VERSION)
336 $xmldoc.root.add_attribute('limit-sizes', $limit_sizes)
338 $xmldoc.root.delete_attribute('limit-sizes')
341 $xmldoc.root.add_attribute('multi-languages', $multi_languages[0].join(',') + ',' + $multi_languages[1])
343 $xmldoc.root.delete_attribute('multi-languages')
347 $xmldoc.root.add_attribute('optimize-for-32', 'true')
349 $xmldoc.root.delete_attribute('optimize-for-32')
352 $xmldoc.root.add_attribute('thumbnails-per-row', $N_per_row)
354 $xmldoc.root.delete_attribute('thumbnails-per-row')
357 $xmldoc.root.add_attribute('thumbnails-per-page', $N_per_page)
359 $xmldoc.root.delete_attribute('thumbnails-per-page')
362 $xmldoc.root.add_attribute('made-with', $madewith)
364 $xmldoc.root.delete_attribute('made-with')
367 $xmldoc.root.add_attribute('index-link', $indexlink)
369 $xmldoc.root.delete_attribute('index-link')
374 $xmldoc.root.add_attribute('transcode-videos', $transcode_videos)
376 $xmldoc.root.delete_attribute('transcode-videos')
380 $madewith = $madewith.gsub('%booh', '"http://booh.org/"')
385 $htmlsuffix = '.html'
398 $info_pipe.puts(value)
404 $info_pipe.puts("die: " + value)
409 def check_installation
410 if !system("which convert >/dev/null 2>/dev/null")
411 die _("The program 'convert' is needed. Please install it.
412 It is generally available with the 'ImageMagick' software package.")
414 if !system("which identify >/dev/null 2>/dev/null")
415 msg 1, _("the program 'identify' is needed to get images sizes and EXIF data. Please install it.
416 It is generally available with the 'ImageMagick' software package.")
419 missing = %w(mplayer).delete_if { |prg| system("which #{prg} >/dev/null 2>/dev/null") }
421 msg 1, _("the following program(s) are needed to handle videos: '%s'. Videos will be ignored.") % missing.join(', ')
422 $ignore_videos = true
426 def replace_line(surround, keyword, line)
428 contents = eval "$#{keyword}"
429 line.sub!(/#{surround}#{keyword}#{surround}/, contents)
431 die _("No '%s' found for substitution") % keyword
435 def build_html_skeletons
436 $html_images = File.open("#{$themedir}/skeleton_image.html").readlines
437 $html_thumbnails = File.open("#{$themedir}/skeleton_thumbnails.html").readlines
438 $html_index = File.open("#{$themedir}/skeleton_index.html").readlines
439 for line in $html_images + $html_thumbnails + $html_index
440 while line =~ /~~~(\w+)~~~/
441 replace_line('~~~', $1, line)
446 def find_caption_value(xmldir, filename)
447 if cap = xmldir.elements["*[@filename='#{utf8(filename)}']"].attributes['caption']
454 def quote_caption(input)
455 return input.gsub('<', '<').gsub('>', '>').gsub('"', '"').gsub("'", ''').gsub("\n", '<br/>')
458 def find_captions(xmldir, images)
459 return images.collect { |img| find_caption_value(xmldir, img) }
462 #- stolen from CVSspam
464 text.sub(/[^a-zA-Z0-9\-,.*_\/]/) do
465 "%#{sprintf('%2X', $&[0])}"
470 return $limit_sizes =~ /original/ ? $images_size + [ { 'name' => 'original' } ] : $images_size
473 def html_reload_to_thumbnails
474 html_reload_to_thumbnails = $preferred_size_reloader.clone
475 html_reload_to_thumbnails.gsub!(/~~theme~~/, $theme)
476 html_reload_to_thumbnails.gsub!(/~~default_size~~/, size2js($default_size['name']))
477 html_reload_to_thumbnails.gsub!(/~~htmlsuffix~~/, $htmlsuffix)
478 html_reload_to_thumbnails.gsub!(/~~all_sizes~~/, all_images_sizes.collect { |s| "\"#{size2js(s['name'])}\"" }.join(', '))
479 size_auto_chooser = '';
480 all_images_sizes.find_all { |s| s.has_key?('optimizedforwidth') }.
481 sort { |a,b| b['optimizedforwidth'].to_i <=> a['optimizedforwidth'].to_i }.
482 each { |s| size_auto_chooser += "if (w + 50 > #{s['optimizedforwidth']}) { return 'thumbnails-#{size2js(s['name'])}-0#{$htmlsuffix}'; }\n" }
483 html_reload_to_thumbnails.gsub!(/~~size_auto_chooser~~/, size_auto_chooser)
484 return html_reload_to_thumbnails
487 def discover_iterations(iterations, line)
488 if line =~ /~~iterate(\d)_open(_max(\d+|N))?~~/
489 for iter in iterations.values
492 iter['close_wait'] = $1.to_i
495 max = $3 == 'N' ? ($N_per_row || $default_N) : $3
496 iterations[$1.to_i] = { 'open' => true, 'max' => max, 'opening' => '', 'closing' => '' }
498 line.sub!(/.*/, '~~thumbnails~~')
502 elsif line =~ /~~iterate(\d)_close~~/
503 iterations[$1.to_i]['open'] = false;
504 iterations[$1.to_i]['close'] = true;
507 for iter in iterations.values
509 iter['opening'] += line
512 if !iter['close'] && iter['close_wait'] && iterations[iter['close_wait']]['close']
513 iter['closing'] += line
520 def reset_iterations(iterations)
521 for iter in iterations.values
526 def run_iterations(iterations, amount)
529 for level in iterations.keys.sort
530 if iterations[level]['value'] == 0
531 html += iterations[level]['opening']
532 elsif level == iterations.keys.max
533 if !iterations[level]['max'] || iterations[level]['max'] && iterations[level]['value'] + amount <= iterations[level]['max'].to_i
534 html += iterations[level]['opening']
539 iterations[level]['value'] += amount
540 if iterations[level]['max'] && iterations[level]['value'] > iterations[level]['max'].to_i
541 iterations[level]['value'] = 0
542 iterations[level-1]['value'] = 0
543 html += iterations[level-1]['closing']
547 return html + run_iterations(iterations, amount)
553 def close_iterations(iterations)
555 for level in iterations.keys.sort.reverse
556 html += iterations[level]['closing']
561 def img_element(fullpath, caption)
562 if size = get_image_size(fullpath)
563 sizespec = 'width="' + size[:x].to_s + '" height="' + size[:y].to_s + '"'
567 return '<img src="' + File.basename(fullpath) + '" ' + sizespec + ' alt="' + quote_caption(caption) + '"/>'
571 return name.gsub(/-/, '')
574 def substitute_html_sizes(html, sizeobj, type, suffix)
576 if $images_size.length > 1 || (type == 'image' && $limit_sizes =~ /original/)
577 for sizeobj2 in $images_size
578 sizejs = size2js(sizeobj2['name'])
579 sizen = defer_translation(sizename(sizeobj2['name'], false))
580 if sizeobj != sizeobj2
581 if type == 'thumbnails'
582 sizestrings << '<a href="thumbnails-' + sizejs + suffix + $htmlsuffix + '" onclick="set_preferred_size(\'' + sizejs + '\')">' + sizen + '</a>'
584 sizestrings << '<a id="link' + sizejs + '" onclick="set_preferred_size(\'' + sizejs + '\')">' + sizen + '</a>'
590 if type == 'image' && $limit_sizes =~ /original/
591 sizestrings << '<a id="linkoriginal" target="newframe">' + defer_translation(sizename('original', false)) + '</a>'
594 html.sub!(/~~sizes~~(.+)~~/) { sizestrings.join($1) }
597 def substitute_navigation(html, xmldir)
598 if xmldir.parent.name == 'dir'
601 parent = xmldir.parent
602 while parent.name == 'dir'
603 parentcaption = parent.attributes['subdirs-caption'] || File.basename(parent.attributes['path'])
604 nav = "<a href=\"#{path}/index#{$htmlsuffix}\">#{parentcaption}</a> #{defer_translation(N_(" > "))} #{nav}"
606 parent = parent.parent
608 html.gsub!(/~~ifnavigation\?~~(.+?)~~fi~~/) { $1 }
609 html.gsub!(/~~navigation~~/, nav + (xmldir.attributes['subdirs-caption'] || File.basename(xmldir.attributes['path'])))
611 html.gsub!(/~~ifnavigation\?~~(.+?)~~fi~~/, '')
615 def substitute_pathtobase(html, xmldir)
618 while location.parent.name == 'dir'
620 location = location.parent
622 html.gsub!(/~~pathtobase~~/, path)
625 def xmldir2destdir(xmldir)
626 return make_dest_filename(from_utf8(File.basename(xmldir.attributes['path'])))
629 def find_previous_album(xmldir)
632 #- move to previous dir element if exists
633 if prevelem = xmldir.previous_element_byname_notattr('dir', 'deleted')
635 relative_pos += '../' + xmldir2destdir(xmldir) + '/'
637 #- after having moved to previous dir, we need to go down last subdir until the last one
638 while child = xmldir.elements['dir']
639 while nextchild = child.next_element_byname_notattr('dir', 'deleted')
642 relative_pos += xmldir2destdir(child) + '/'
646 #- previous dir doesn't exist, move to previous dir element if exists
647 xmldir = xmldir.parent
648 if xmldir.name == 'dir' && !xmldir.attributes['deleted']
649 relative_pos += '../'
654 end while !xmldir.child_byname_notattr('image', 'deleted') && !xmldir.child_byname_notattr('video', 'deleted')
655 return File.reduce_path(relative_pos)
658 def find_next_album(xmldir)
661 #- first child dir element (catches when initial xmldir has both thumbnails and subdirs)
662 if firstchild = xmldir.child_byname_notattr('dir', 'deleted')
664 relative_pos += xmldir2destdir(xmldir) + '/'
666 elsif nextbro = xmldir.next_element_byname_notattr('dir', 'deleted')
668 relative_pos += '../' + xmldir2destdir(xmldir) + '/'
670 #- go up until we have a next brother or we are finished
672 xmldir = xmldir.parent
673 relative_pos += '../'
674 end while xmldir && !xmldir.next_element_byname_notattr('dir', 'deleted')
676 xmldir = xmldir.next_element_byname('dir')
677 relative_pos += '../' + xmldir2destdir(xmldir) + '/'
682 end while !xmldir.child_byname_notattr('image', 'deleted') && !xmldir.child_byname_notattr('video', 'deleted')
683 return File.reduce_path(relative_pos)
686 def find_translation_for_file(file, msg)
688 if file =~ /\.(\w\w)\.html$/
689 bindtextdomain("booh", { :locale => "#{$1}.UTF-8" })
691 Locale.set_current($default_locale)
694 die "Internal error: cannot find multi language suffix of file '#{file}'"
701 def sub_previous_next_album(file, previous_album, next_album, html, previous_album_msg, next_album_msg)
703 html.gsub!(/~~previous_album~~/, '<a href="' + previous_album + 'thumbnails' + $htmlsuffix + '">' + previous_album_msg + '</a>')
704 html.gsub!(/~~ifprevious_album\?~~(.+?)~~fi~~/) { $1 }
706 html.gsub!(/~~previous_album~~/, '')
707 html.gsub!(/~~ifprevious_album\?~~(.+?)~~fi~~/, '')
710 html.gsub!(/~~next_album~~/, '<a href="' + next_album + 'thumbnails' + $htmlsuffix + '">' + next_album_msg + '</a>')
711 html.gsub!(/~~ifnext_album\?~~(.+?)~~fi~~/) { $1 }
713 html.gsub!(/~~next_album~~/, '')
714 html.gsub!(/~~ifnext_album\?~~(.+?)~~fi~~/, '')
719 def save_html(html, base_filename)
720 if html.class == Array
724 for language in ($multi_languages[0] + [ $multi_languages[1] ]).uniq
725 bindtextdomain("booh", { :locale => "#{language}.UTF-8" })
726 ios = File.open("#{base_filename}.#{language}.html", "w")
727 ios.write(html.gsub(/@@(.*?)@@/) { _($1) })
729 Locale.set_current($default_locale)
732 ios = File.open("#{base_filename}.html", "w")
733 ios.write(html.gsub(/@@(.*?)@@/) { utf8(_($1)) })
740 #- preprocess the path->dir, rexml is very slow with that; we seem to improve speed by 7%
742 $xmldoc.elements.each('//dir') { |elem|
743 optxpath[elem.attributes['path']] = elem
747 if $mode == 'merge_config_onedir'
748 examined_dirs = [ $onedir ]
749 elsif $mode == 'merge_config_subdirs'
750 examined_dirs = `find '#{$onedir}' -type d -follow`.sort.collect { |v| v.chomp }.delete_if { |v| optxpath.has_key?(utf8(v)) }
752 examined_dirs = `find '#{$source}' -type d -follow`.sort.collect { |v| v.chomp }
753 if $mode == 'merge_config'
754 $xmldoc.elements.each('//dir') { |elem|
755 if ! examined_dirs.include?(elem.attributes['path'])
756 msg 2, _("Merging config: removing directory %s from config, isn't on filesystem anymore") % elem.attributes['path']
762 info("directories: #{examined_dirs.length}, sizes: #{$images_size.length}")
764 examined_dirs.each { |dir|
766 die _("Source directory or sub-directories can't contain a single-quote character, sorry: %s") % dir
768 if $mode !~ /^use_config/
769 Dir.entries(dir).each { |file|
770 if file =~ /['"\[\]]/
771 die _("Files can't contain any of the characters ', \", [ or ], sorry: %s") % "#{dir}/#{file}"
777 examined_dirs.each { |dir|
778 if File.basename(dir) =~ /^\./
779 msg 1, _("Ignoring directory %s, begins with a dot (indicating a hidden directory)") % dir
783 dest_dir = make_dest_filename(dir.sub(/^#{Regexp.quote($source)}/, $dest))
785 #- place xml document on proper node if exists, else create
786 xmldir = optxpath[utf8(dir)]
787 if $mode == 'use_config' || $mode == 'use_config_changetheme'
788 if !xmldir || (xmldir.attributes['already-generated'] && !$force) || xmldir.attributes['deleted']
789 info("walking: #{dir}|#{$source}, 0 elements")
790 if xmldir && xmldir.attributes['deleted']
791 system("rm -rf '#{dest_dir}'")
796 if $mode == 'gen_config' || (($mode == 'merge_config' || $mode == 'merge_config_subdirs') && !xmldir)
797 #- add the <dir..> element if necessary
798 parent = File.dirname(dir)
799 xmldir = $xmldoc.elements["//dir[@path='#{utf8(parent)}']"]
801 xmldir = $xmldoc.root
803 #- need to remove the already-generated mark of the parent because of the sub-albums page containing now one more element
804 xmldir.delete_attribute('already-generated')
805 xmldir = optxpath[utf8(dir)] = xmldir.add_element('dir', { 'path' => utf8(dir) })
808 xmldir.delete_attribute('already-generated')
810 #- preprocess all the existing elements, rexml is slow with that
812 xmldir.elements.each { |elem|
813 if filename = elem.attributes['filename']
814 optelements[elem.name + '|' + filename] = elem
818 #- read images/videos entries from config or from directories depending on mode
820 if $mode == 'use_config' || $mode == 'use_config_changetheme'
821 msg 2, _("Handling %s from config list...") % dir
822 xmldir.elements.each { |element|
823 if %w(image video).include?(element.name) && !element.attributes['deleted']
824 entries << from_utf8(element.attributes['filename'])
828 msg 2, _("Examining %s...") % dir
829 entries = Dir.entries(dir).sort
830 #- populate config in case of gen_config, add new files in case of merge_config
832 if file =~ /['"\[\]]/
833 msg 1, _("Ignoring %s, contains one of forbidden characters: '\"[]") % "#{dir}/#{file}"
835 type = entry2type(file)
836 if type && ! optelements[type + '|' + utf8(file)]
837 #- hack: don't run identify (which is slow) if format only contains default %t
838 if $commentsformat && type == 'image' && $commentsformat != '%t'
839 comment = utf8(`identify -format "#{$commentsformat}" '#{dir}/#{file}'`.chomp.sub(/\.$/, ''))
841 comment = utf8cut(file.sub(/\.[^\.]+$/, ''), 18)
843 optelements[type + '|' + utf8(file)] = xmldir.add_element(type, { "filename" => utf8(file), "caption" => comment })
847 if $mode != 'gen_config'
848 #- cleanup removed files from config and reread entries from config to get proper ordering
850 xmldir.elements.each { |element|
851 fullpath = "#{dir}/#{from_utf8(element.attributes['filename'])}"
852 if %w(image video).include?(element.name)
853 if !File.readable?(fullpath)
854 msg 1, _("Config merge: removing %s from config; use the backup file to retrieve caption info if this was a mistake") % fullpath
855 xmldir.delete(element)
856 elsif !element.attributes['deleted']
857 entries << from_utf8(element.attributes['filename'])
861 #- if there is no more elements here, there is no album here anymore
862 if !xmldir.child_byname_notattr('image', 'deleted') && !xmldir.child_byname_notattr('video', 'deleted')
863 xmldir.delete_attribute('thumbnails-caption')
864 xmldir.delete_attribute('thumbnails-captionfile')
868 images = entries.find_all { |e| entry2type(e) == 'image' }
869 msg 3, _("\t%s photos") % images.length
870 videos = entries.find_all { |e| entry2type(e) == 'video' }
871 msg 3, _("\t%s videos") % videos.length
872 info("walking: #{dir}|#{$source}, #{images.length + videos.length} elements")
874 system("mkdir -p '#{dest_dir}'")
876 #- generate .htaccess file
878 ios = File.open("#{dest_dir}/.htaccess", "w")
879 ios.write("AddCharset UTF-8 .html\n")
880 if auth_user_file = xmldir.attributes['password-protect']
881 msg 3, _("\tgenerating password protection file #{dest_dir}/.htaccess")
882 ios.write("AuthType Basic\nAuthName \"protected area\"\nAuthUserFile #{auth_user_file}\nrequire valid-user\n")
885 ios.write("Options +Multiviews\n")
886 ios.write("LanguagePriority #{$multi_languages[1]}\n")
887 ios.write("ForceLanguagePriority Prefer Fallback\n")
888 ios.write("DirectoryIndex index\n")
893 #- pass through if there are no images and videos
894 if images.size == 0 && videos.size == 0
896 #- cleanup old images/videos, especially if this directory contained images/videos previously.
897 if $mode != 'gen_config'
898 rightful_images = [ '.htaccess' ]
899 if xmldir.attributes['thumbnails-caption']
900 rightful_images << 'thumbnails-thumbnail.jpg'
902 xmldir.elements.each('dir') { |child|
903 if child.attributes['deleted']
906 subdir = make_dest_filename(from_utf8(File.basename(child.attributes['path'])))
907 rightful_images << "thumbnails-#{subdir}.jpg"
909 to_del = Dir.entries(dest_dir).find_all { |e| !File.directory?(File.join(dest_dir, e)) && !rightful_images.include?(e) }
911 File.delete(*to_del.collect { |e| File.join(dest_dir, e) })
915 #- copy any resource file that goes with the theme (css, images..)
916 themestuff = Dir.entries("#{$themedir}").
917 find_all { |e| !%w(. .. skeleton_image.html skeleton_thumbnails.html skeleton_index.html metadata root).include?(e) }
918 themestuff.each { |entry|
919 if !File.exists?(File.join(dest_dir, entry))
920 psys("cp '#{$themedir}/#{entry}' '#{dest_dir}'")
924 #- copy any root-only resource file that goes with the theme (css, images..)
925 if xmldir.parent.name != 'dir'
926 themestuff_root = Dir.entries("#{$themedir}/root").
927 find_all { |e| !%w(. ..).include?(e) }
928 themestuff_root.each { |entry|
929 if !File.exists?(File.join(dest_dir, entry))
930 psys("cp '#{$themedir}/root/#{entry}' '#{dest_dir}'")
938 msg 2, _("Outputting in %s...") % dest_dir
940 #- populate data structure with sizes from theme
941 for sizeobj in $images_size
942 fullscreen_images ||= {}
943 fullscreen_images[sizeobj['name']] = []
944 thumbnail_images ||= {}
945 thumbnail_images[sizeobj['name']] = []
946 thumbnail_videos ||= {}
947 thumbnail_videos[sizeobj['name']] = []
949 #- a special dummy size to keep 'references' to thumbnails in case of panorama, because the GUI will use the regular thumbnails
950 thumbnail_images['dont-delete-file-for-gui'] = []
951 if $limit_sizes =~ /original/
952 fullscreen_images['original'] = []
955 images.size >= 1 and msg 3, _("\tcreating photos thumbnails...")
957 #- create thumbnails for images
959 info("processing element")
960 base_dest_img = dest_dir + '/' + make_dest_filename(img.sub(/\.[^\.]+$/, ''))
961 elem = optelements['image|' + utf8(img)]
963 thumbnail_dest_img = base_dest_img + "-#{$default_size['thumbnails']}.jpg"
964 gen_thumbnails_element("#{dir}/#{img}", elem, true, [ { 'filename' => thumbnail_dest_img, 'size' => $default_size['thumbnails'] } ])
967 for sizeobj in $images_size
968 size_fullscreen = sizeobj['fullscreen']
969 size_thumbnails = sizeobj['thumbnails']
970 fullscreen_dest_img = base_dest_img + "-#{size_fullscreen}.jpg"
971 fullscreen_images[sizeobj['name']] << File.basename(fullscreen_dest_img)
972 todo << { 'filename' => fullscreen_dest_img, 'size' => size_fullscreen }
973 if pano = pano_amount(elem)
974 thumbnail_images['dont-delete-file-for-gui'] << File.basename(base_dest_img + "-#{size_thumbnails}.jpg")
975 size_thumbnails = size_thumbnails.sub(/(\d+)/) { ($1.to_i * pano).to_i }
977 thumbnail_dest_img = base_dest_img + "-#{size_thumbnails}.jpg"
978 thumbnail_images[sizeobj['name']] << File.basename(thumbnail_dest_img)
979 todo << { 'filename' => thumbnail_dest_img, 'size' => size_thumbnails }
981 gen_thumbnails_element("#{dir}/#{img}", elem, true, todo)
982 if $limit_sizes =~ /original/
983 fullscreen_images['original'] << img
985 destimg = "#{dest_dir}/#{img}"
986 if $limit_sizes =~ /original/ && !File.exists?(destimg)
988 if ! sys("ln '#{dir}/#{img}' '#{destimg}'")
989 $hardlinks_ok = false
993 psys("cp '#{dir}/#{img}' '#{destimg}'")
999 videos.size >= 1 and msg 3, _("\tcreating videos thumbnails...")
1000 transcoded_videos = {}
1002 #- create thumbnails for videos
1003 videos.each { |video|
1004 info("processing element")
1005 elem = optelements['video|' + utf8(video)]
1007 thumbnail_dest_img = dest_dir + '/' + make_dest_filename(video.sub(/\.[^\.]+$/, '')) + "-#{$default_size['thumbnails']}.jpg"
1008 gen_thumbnails_element("#{dir}/#{video}", elem, true, [ { 'filename' => thumbnail_dest_img, 'size' => $default_size['thumbnails'] } ])
1011 for sizeobj in $images_size
1012 size_thumbnails = sizeobj['thumbnails']
1013 thumbnail_dest_img = dest_dir + '/' + make_dest_filename(video.sub(/\.[^\.]+$/, '')) + "-#{size_thumbnails}.jpg"
1014 thumbnail_videos[sizeobj['name']] << File.basename(thumbnail_dest_img)
1015 todo << { 'filename' => thumbnail_dest_img, 'size' => size_thumbnails }
1017 gen_thumbnails_element("#{dir}/#{video}", elem, true, todo)
1019 if $transcode_videos
1020 parts = $transcode_videos.split(':', 2)
1021 basedestvideo = video.sub(/\.\w+/, '') + '.' + parts[0]
1022 transcoded_videos[video] = basedestvideo
1023 destvideo = "#{dest_dir}/#{basedestvideo}"
1024 if ! File.exists?(destvideo)
1025 psys(parts[1].gsub(/%f/, "'#{dir}/#{video}'").gsub(/%o/, "'#{destvideo}'"))
1028 destvideo = "#{dest_dir}/#{video}"
1029 if ! File.exists?(destvideo)
1031 if ! sys("ln '#{dir}/#{video}' '#{destvideo}'")
1032 $hardlinks_ok = false
1036 psys("cp '#{dir}/#{video}' '#{destvideo}'")
1044 #- cleanup old images/videos (for when removing elements or sizes)
1045 all_elements = fullscreen_images.collect { |e| e[1] }.flatten.
1046 concat(thumbnail_images.collect { |e| e[1] }.flatten).
1047 concat(thumbnail_videos.collect { |e| e[1] }.flatten).
1048 concat($transcode_videos ? transcoded_videos.values : videos).
1050 to_del = Dir.entries(dest_dir).find_all { |e| !File.directory?(File.join(dest_dir, e)) && !all_elements.include?(e) && e !~ /^thumbnails-\w+\.jpg/ }
1052 msg 3, _("\tcleaning up: #{to_del.join(', ')}")
1053 File.delete(*to_del.collect { |e| File.join(dest_dir, e) })
1056 #- copy any resource file that goes with the theme (css, images..)
1057 themestuff = Dir.entries("#{$themedir}").
1058 find_all { |e| !%w(. .. skeleton_image.html skeleton_thumbnails.html skeleton_index.html metadata root).include?(e) }
1059 themestuff.each { |entry|
1060 if !File.exists?(File.join(dest_dir, entry))
1061 psys("cp '#{$themedir}/#{entry}' '#{dest_dir}'")
1065 #- copy any root-only resource file that goes with the theme (css, images..)
1066 if xmldir.parent.name != 'dir'
1067 themestuff_root = Dir.entries("#{$themedir}/root").
1068 find_all { |e| !%w(. ..).include?(e) }
1069 themestuff_root.each { |entry|
1070 if !File.exists?(File.join(dest_dir, entry))
1071 psys("cp '#{$themedir}/root/#{entry}' '#{dest_dir}'")
1076 msg 3, _("\tgenerating HTML pages...")
1077 #- fixup max per page
1079 $N_per_page = $N_per_page.to_i / ($N_per_row || $default_N).to_i * ($N_per_row || $default_N).to_i
1082 #- generate thumbnails*.html (page with thumbnails)
1083 image2thumbnailpage4js = []
1084 for sizeobj in $images_size
1085 info("processing size")
1086 html = $html_thumbnails.collect { |l| l.clone }
1089 i.sub!(/~~title~~/, quote_caption(xmldir.attributes['thumbnails-caption'] || utf8(File.basename(dir))))
1090 discover_iterations(iterations, i)
1093 html_thumbnails = ''
1094 html_thumbnails_nojs = ''
1097 reset_iterations(iterations)
1098 #- preprocess the @filename->elem, rexml is very slow with that; we dramatically improve this part of the processing
1100 xmldir.elements.each('image') { |elem|
1101 optfilename[elem.attributes['filename']] = elem
1104 type = images.include?(file) ? 'image' : videos.include?(file) ? 'video' : nil
1106 homogeinize_width = 100 / ($N_per_row || $default_N).to_i
1107 if type == 'image' && elem = optfilename[utf8(file)]
1108 if pano = pano_amount(elem)
1109 html_elem = run_iterations(iterations, pano)
1110 counter += count = pano.ceil
1111 html_elem.gsub!(/~~colspan~~/) { "colspan=\"#{count}\"" }
1112 homogeinize_width *= count
1114 html_elem = run_iterations(iterations, 1)
1116 html_elem.gsub!(/~~colspan~~/, '')
1119 html_elem = run_iterations(iterations, 1)
1121 html_elem.gsub!(/~~colspan~~/, '')
1123 html_elem.gsub!(/~~homogeinize_width~~/) { "width=\"#{homogeinize_width}%\"" }
1125 index = images.index(file)
1126 caption = find_caption_value(xmldir, images[index]) || utf8(images[index])
1127 html_elem.gsub!(/~~caption_iteration~~/, quote_caption(caption))
1128 html_elem.gsub!(/~~ifimage\?~~(.+?)~~fi~~/) { $1 }
1129 html_elem.gsub!(/~~ifvideo\?~~(.+?)~~fi~~/, '')
1130 elsif type == 'video'
1131 index = videos.index(file)
1132 caption = find_caption_value(xmldir, videos[index]) || utf8(videos[index])
1133 if File.exists?("#{dest_dir}/#{thumbnail_videos[sizeobj['name']][index]}")
1134 html_elem.gsub!(/~~image_iteration~~/,
1135 '<a href="' + ( $transcode_videos ? transcoded_videos[videos[index]] : videos[index] ) + '">' +
1136 img_element("#{dest_dir}/#{thumbnail_videos[sizeobj['name']][index]}", caption) + '</a>')
1138 html_elem.gsub!(/~~image_iteration~~/,
1139 '<a href="' + ( $transcode_videos ? transcoded_videos[videos[index]] : videos[index] ) + '">' +
1140 defer_translation(N_("(no preview)")) + '</a>')
1142 html_elem.gsub!(/~~caption_iteration~~/, quote_caption(caption))
1143 html_elem.gsub!(/~~ifimage\?~~(.+?)~~fi~~/, '')
1144 html_elem.gsub!(/~~ifvideo\?~~(.+?)~~fi~~/) { $1 }
1146 html_thumbnails += html_elem
1147 html_thumbnails_nojs += html_elem
1149 html_thumbnails.gsub!(/~~image_iteration~~/,
1150 '<a href="image-' + size2js(sizeobj['name']) + $htmlsuffix + '#current=' + fullscreen_images[sizeobj['name']][index] +
1151 '" name="' + fullscreen_images[sizeobj['name']][index] + '">' +
1152 img_element("#{dest_dir}/#{thumbnail_images[sizeobj['name']][index]}", caption) + '</a>')
1153 html_thumbnails_nojs.gsub!(/~~image_iteration~~/,
1154 '<a href="' + fullscreen_images[sizeobj['name']][index] + '" name="' + fullscreen_images[sizeobj['name']][index] + '">' +
1155 img_element("#{dest_dir}/#{thumbnail_images[sizeobj['name']][index]}", caption) + '</a>')
1156 #- remember in which thumbnails page is this element, for image->thumbnail link
1157 if sizeobj == $images_size[0]
1158 image2thumbnailpage4js << pagecount
1162 if counter == $N_per_page
1163 html_thumbnails += close_iterations(iterations)
1164 html_thumbnails_nojs += close_iterations(iterations)
1165 all_pages << [ html_thumbnails, html_thumbnails_nojs ]
1166 html_thumbnails = ''
1167 html_thumbnails_nojs = ''
1169 reset_iterations(iterations)
1175 html_thumbnails += close_iterations(iterations)
1176 html_thumbnails_nojs += close_iterations(iterations)
1177 all_pages << [ html_thumbnails, html_thumbnails_nojs ]
1180 i.gsub!(/~~theme~~/, $theme)
1181 i.gsub!(/~~current_size~~/, sizeobj['name'])
1182 i.gsub!(/~~htmlsuffix~~/, $htmlsuffix)
1183 i.gsub!(/~~current_size_js~~/, size2js(sizeobj['name']))
1184 i.gsub!(/~~madewith~~/, $madewith || '')
1185 i.gsub!(/~~indexlink~~/, $indexlink || '')
1186 substitute_navigation(i, xmldir)
1188 html_nojs = html.collect { |l| l.clone }
1190 for page in all_pages
1191 html_thumbnails, html_thumbnails_nojs = page
1192 final_html = html.collect { |l| l.clone }
1193 mstuff = defer_translation(N_("Pages: ")) +
1194 (pagecount > 0 ? "<a href=\"thumbnails-#{size2js(sizeobj['name'])}%nojs-#{pagecount - 1}#{$htmlsuffix}\">" + defer_translation(N_("<- Previous")) + "</a> " : '') +
1195 all_pages.collect_with_index { |p,idx| page == p ? idx + 1 : "<a href=\"thumbnails-#{size2js(sizeobj['name'])}%nojs-#{idx}#{$htmlsuffix}\">#{idx + 1}</a>" }.join(', ') +
1196 (pagecount < all_pages.size - 1 ? " <a href=\"thumbnails-#{size2js(sizeobj['name'])}%nojs-#{pagecount + 1}#{$htmlsuffix}\">" + defer_translation(N_("Next ->")) + "</a> " : '')
1198 i.sub!(/~~run_slideshow~~/,
1199 images.size <= 1 ? '' : '<a href="image-' + size2js(sizeobj['name']) + $htmlsuffix + '#run_slideshow=1">' + defer_translation(N_("Run slideshow!"))+'</a>')
1200 i.sub!(/~~thumbnails~~/, html_thumbnails)
1201 if all_pages.size == 1
1202 i.gsub!(/~~ifmultiplepages\?~~.*~~fi~~/, '')
1204 i.gsub!(/~~ifmultiplepages\?~~(.+?)~~fi~~/) { $1 }
1205 i.gsub!(/~~multiplepagesstuff~~/, mstuff.gsub('%nojs', ''))
1207 substitute_html_sizes(i, sizeobj, 'thumbnails', "-#{pagecount}")
1208 substitute_pathtobase(i, xmldir)
1210 save_html(final_html, "#{dest_dir}/thumbnails-#{size2js(sizeobj['name'])}-#{pagecount}")
1211 final_html_nojs = html_nojs.collect { |l| l.clone }
1212 for i in final_html_nojs
1213 i.sub!(/~~run_slideshow~~/, defer_translation(N_("<i>Click on an image to view it larger</i>")))
1214 i.sub!(/~~thumbnails~~/, html_thumbnails_nojs)
1215 if all_pages.size == 1
1216 i.gsub!(/~~ifmultiplepages\?~~.*~~fi~~/, '')
1218 i.gsub!(/~~ifmultiplepages\?~~(.+?)~~fi~~/) { $1 }
1219 i.gsub!(/~~multiplepagesstuff~~/, mstuff.gsub('%nojs', '-nojs'))
1221 substitute_html_sizes(i, sizeobj, 'thumbnails', "-nojs-#{pagecount}")
1222 substitute_pathtobase(i, xmldir)
1224 save_html(final_html_nojs, "#{dest_dir}/thumbnails-#{size2js(sizeobj['name'])}-nojs-#{pagecount}")
1229 info("finished processing sizes")
1231 #- generate "main" thumbnails.html page that will reload to correct size thanks to cookie
1232 save_html(html_reload_to_thumbnails, "#{dest_dir}/thumbnails")
1234 #- generate image.html (page with fullscreen images)
1236 captions4js = find_captions(xmldir, images).collect { |e| e ? '"' + e.gsub('"', '\"') + '"' : '""' }.join(', ')
1237 thumbnailspage4js = image2thumbnailpage4js.collect { |e| "\"#{e}\"" }.join(', ')
1238 for sizeobj in $images_size
1239 html = $html_images.collect { |l| l.clone }
1240 images4js = fullscreen_images[sizeobj['name']].collect { |e| "\"#{e}\"" }.join(', ')
1243 for sizeobj2 in all_images_sizes
1244 if sizeobj != sizeobj2
1245 otherimages4js += "var images_#{size2js(sizeobj2['name'])} = new Array(" + fullscreen_images[sizeobj2['name']].collect { |e| "\"#{e}\"" }.join(', ') + ")\n"
1246 othersizes << "\"#{size2js(sizeobj2['name'])}\""
1250 i.gsub!(/~~images~~/, images4js)
1251 i.gsub!(/~~other_images~~/, otherimages4js)
1252 i.gsub!(/~~thumbnailspages~~/, thumbnailspage4js)
1253 i.gsub!(/~~other_sizes~~/, othersizes.join(', '))
1254 i.gsub!(/~~captions~~/, captions4js)
1255 i.gsub!(/~~title~~/, quote_caption(xmldir.attributes['thumbnails-caption'] || utf8(File.basename(dir))))
1256 i.gsub!(/~~thumbnails~~/, '<a href="thumbnails-' + size2js(sizeobj['name']) + $htmlsuffix + '" id="thumbnails">' + defer_translation(N_('return to thumbnails')) + '</a>')
1257 i.gsub!(/~~theme~~/, $theme)
1258 i.gsub!(/~~current_size~~/, size2js(sizeobj['name']))
1259 i.gsub!(/~~htmlsuffix~~/, $htmlsuffix)
1260 i.gsub!(/~~madewith~~/, $madewith || '')
1261 i.gsub!(/~~indexlink~~/, $indexlink || '')
1262 substitute_html_sizes(i, sizeobj, 'image', '')
1263 substitute_navigation(i, xmldir)
1264 substitute_pathtobase(i, xmldir)
1266 save_html(html, "#{dest_dir}/image-#{size2js(sizeobj['name'])}")
1274 #- add attributes to <dir..> elements needing so
1275 if $mode != 'use_config'
1276 msg 3, _("\tfixating configuration file...")
1277 $xmldoc.elements.each('//dir') { |element|
1278 path = captionpath = element.attributes['path']
1279 descendant_element = nil
1280 #- search if there is at least one image or video down that dir; use workarounds to rexml slowness
1281 #- first, look down only one level, very fast
1283 element.elements.each { |elem|
1287 if elem.name == 'image' || elem.name == 'video'
1288 descendant_element = elem
1292 if descendant_element.nil?
1293 #- if there was nothing or only directory down one level, look down the first subdir
1294 if ! first_desc.nil?
1295 first_desc.elements.each { |elem|
1296 if elem.name == 'image' || elem.name == 'video'
1297 descendant_element = elem
1302 if descendant_element.nil?
1303 #- if there was still nothing found, use complete albeit slow method
1304 descendant_element = element.elements['descendant::image'] || element.elements['descendant::video']
1307 if !descendant_element
1308 msg 3, _("\t\tremoving %s, no element in it") % path
1309 element.remove #- means we have a directory with nothing interesting in it
1311 captionfile = "#{descendant_element.parent.attributes['path']}/#{descendant_element.attributes['filename']}"
1312 basename = File.basename(path)
1313 if element.elements['dir']
1314 if !element.attributes['subdirs-caption']
1315 element.add_attribute('subdirs-caption', basename)
1317 if !element.attributes['subdirs-captionfile']
1318 element.add_attribute('subdirs-captionfile', captionfile)
1321 if element.child_byname_notattr('image', 'deleted') || element.child_byname_notattr('video', 'deleted')
1322 if !element.attributes['thumbnails-caption']
1323 element.add_attribute('thumbnails-caption', basename)
1325 if !element.attributes['thumbnails-captionfile']
1326 element.add_attribute('thumbnails-captionfile', captionfile)
1333 #- write down to disk config if necessary
1335 ios = File.open($config_writeto, "w")
1341 msg 3, _("completed necessary stuff for GUI, exiting.")
1345 #- second pass to create index.html files and previous/next links
1346 info("creating index.html")
1347 msg 3, _("\trescanning directories to generate all 'index.html' files...")
1349 #- recompute the memoization because elements mights have been removed (the ones with no element in them)
1351 $xmldoc.elements.each('//dir') { |elem|
1352 optxpath[elem.attributes['path']] = elem
1355 examined_dirs.each { |dir|
1356 info("index.html: #{dir}|#{$source}")
1358 xmldir = optxpath[utf8(dir)]
1359 if !xmldir || (xmldir.attributes['already-generated'] && !$force) || xmldir.attributes['deleted']
1362 dest_dir = make_dest_filename(dir.sub(/^#{Regexp.quote($source)}/, $dest))
1364 previous_album = find_previous_album(xmldir)
1365 next_album = find_next_album(xmldir)
1367 if xmldir.elements['dir']
1368 html = $html_index.collect { |l| l.clone }
1371 caption = xmldir.attributes['subdirs-caption']
1372 i.gsub!(/~~title~~/, quote_caption(caption))
1373 substitute_navigation(i, xmldir)
1374 substitute_pathtobase(i, xmldir)
1375 discover_iterations(iterations, i)
1379 reset_iterations(iterations)
1381 #- deal with "current" album (directs to "thumbnails" page)
1382 if xmldir.attributes['thumbnails-caption']
1383 thumbnail = "#{dest_dir}/thumbnails-thumbnail.jpg"
1384 gen_thumbnails_subdir(from_utf8(xmldir.attributes['thumbnails-captionfile']), xmldir, false,
1385 [ { 'filename' => thumbnail, 'size' => $albums_thumbnail_size } ], 'thumbnails')
1386 html_index += run_iterations(iterations, 1)
1387 caption = xmldir.attributes['thumbnails-caption']
1388 html_index.gsub!(/~~image_iteration~~/, "<a href=\"thumbnails#{$htmlsuffix}\">" + img_element(thumbnail, caption) + '</a>')
1389 html_index.gsub!(/~~caption_iteration~~/, quote_caption(caption))
1392 #- deal with sub-albums (direct to subdirs/index.html pages)
1393 xmldir.elements.each('dir') { |child|
1394 if child.attributes['deleted']
1397 subdir = make_dest_filename(from_utf8(File.basename(child.attributes['path'])))
1398 thumbnail = "#{dest_dir}/thumbnails-#{subdir}.jpg"
1399 html_index += run_iterations(iterations, 1)
1400 captionfile, caption = find_subalbum_caption_info(child)
1401 gen_thumbnails_subdir(captionfile, child, false,
1402 [ { 'filename' => thumbnail, 'size' => $albums_thumbnail_size } ], find_subalbum_info_type(child))
1403 html_index.gsub!(/~~caption_iteration~~/, quote_caption(caption))
1404 html_index.gsub!(/~~image_iteration~~/, "<a href=\"#{subdir}/index#{$htmlsuffix}\">" + img_element(thumbnail, caption) + '</a>')
1407 html_index += close_iterations(iterations)
1410 i.gsub!(/~~thumbnails~~/, html_index)
1411 i.gsub!(/~~madewith~~/, $madewith || '')
1412 i.gsub!(/~~indexlink~~/, $indexlink || '')
1416 html = html_reload_to_thumbnails
1419 save_html(html, "#{dest_dir}/index")
1421 #- in case MultiViews will not work, generate some compat
1422 ios = File.open("#{dest_dir}/index.html", "w")
1423 ios.write("<html><head><meta http-equiv=\"refresh\" content=\"0.1;url=index.#{$multi_languages[1]}.html\"></head></html>")
1427 #- substitute multiple "return to albums", previous/next correctly
1428 #- the following two statements are dramatical optimizations to executing for each substInFile callback
1429 dirpresent = xmldir.elements['dir']
1430 parentname = xmldir.parent.name
1431 if xmldir.child_byname_notattr('image', 'deleted') || xmldir.child_byname_notattr('video', 'deleted')
1432 for suffix in [ '', '-nojs' ]
1433 for sizeobj in $images_size
1434 Dir.glob("#{dest_dir}/thumbnails-#{size2js(sizeobj['name'])}#{suffix}-*.html") do |file|
1435 #- unroll translations, they are costly if rerun for each line of files
1436 rta = find_translation_for_file(file, N_('return to albums'))
1437 pa = find_translation_for_file(file, N_('previous album'))
1438 na = find_translation_for_file(file, N_('next album'))
1439 substInFile(file) { |line|
1440 sub_previous_next_album(file, previous_album, next_album, line, pa, na)
1442 line.sub!(/~~return_to_albums~~/, '<a href="index' + $htmlsuffix + '">' + rta + '</a>')
1444 if parentname == 'dir'
1445 line.sub!(/~~return_to_albums~~/, '<a href="../index' + $htmlsuffix + '">' + rta + '</a>')
1447 line.sub!(/~~return_to_albums~~/, '')
1453 if suffix == '' && xmldir.child_byname_notattr('image', 'deleted')
1454 Dir.glob("#{dest_dir}/image-#{size2js(sizeobj['name'])}*.html") do |file|
1455 pa = find_translation_for_file(file, N_('previous album'))
1456 na = find_translation_for_file(file, N_('next album'))
1457 substInFile(file) { |line|
1458 sub_previous_next_album(file, previous_album, next_album, line, pa, na)
1467 msg 3, _(" all done.")
1474 build_html_skeletons