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-2008 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 require 'gettext/locale'
26 require 'booh/rexml/document'
29 require 'booh/booh-lib'
30 require 'booh/html-merges'
32 #- bind text domain as soon as possible because some _() functions are called early to build data structures
33 bindtextdomain("booh")
34 #- save locale for restoring for multi languages
35 $default_locale = Locale.get
39 [ '--help', '-h', GetoptLong::NO_ARGUMENT, _("Get help message") ],
40 [ '--version', '-V', GetoptLong::NO_ARGUMENT, _("Print version and exit") ],
42 [ '--source', '-s', GetoptLong::REQUIRED_ARGUMENT, _("Directory which contains original photos/videos as files or subdirs") ],
43 [ '--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!") ],
45 [ '--theme', '-t', GetoptLong::REQUIRED_ARGUMENT, _("Select HTML theme to use") ],
46 [ '--config', '-C', GetoptLong::REQUIRED_ARGUMENT, _("File containing config listing photos and videos within directories with captions") ],
47 [ '--config-skel', '-k', GetoptLong::REQUIRED_ARGUMENT, _("Filename where the script will output a config skeleton") ],
48 [ '--merge-config', '-M', GetoptLong::REQUIRED_ARGUMENT, _("File containing config listing, where to merge new/removed photos/videos from --source, and change theme info") ],
49 [ '--merge-config-onedir', '-O', GetoptLong::REQUIRED_ARGUMENT, _("File containing config listing, for merging the subdir specified with --dir") ],
50 [ '--merge-config-subdirs', '-U', GetoptLong::REQUIRED_ARGUMENT, _("File containing config listing, for merging the new subdirs down the subdir specified with --dir") ],
51 [ '--dir', '-D', GetoptLong::REQUIRED_ARGUMENT, _("Directory for merge with --merge-config-onedir or --merge-config-subdirs") ],
52 [ '--use-config', '-u', GetoptLong::REQUIRED_ARGUMENT, _("File containing config listing, where to change theme info") ],
53 [ '--force', '-f', GetoptLong::NO_ARGUMENT, _("Force generation of album even if the GUI marked some directories as already generated") ],
55 [ '--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)") ],
56 [ '--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(', ') ],
57 [ '--thumbnails-per-row', '-T', GetoptLong::REQUIRED_ARGUMENT, _("Specify the amount of thumbnails per row in the thumbnails page (if applicable in theme)") ],
58 [ '--thumbnails-per-page', '-p', GetoptLong::REQUIRED_ARGUMENT, _("Specify the amount of thumbnails per page in the thumbnails page, after which split occurs") ],
59 [ '--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)") ],
60 [ '--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") ],
61 [ '--index-link', '-l', GetoptLong::REQUIRED_ARGUMENT, _("Specify the HTML markup to use on the bottom of pages for a small link returning to wherever you see fit in your website (or somewhere else)") ],
62 [ '--made-with', '-n', GetoptLong::REQUIRED_ARGUMENT, _("Specify the HTML markup to use on the bottom of pages for a small 'made with' message") ],
63 [ '--comments-format','-c', GetoptLong::REQUIRED_ARGUMENT, _("Specify comments format to use for images instead of only filename when creating new albums; use ImageMagick's format") ],
65 [ '--mproc', '-m', GetoptLong::REQUIRED_ARGUMENT, _("Specify the number of processors for multi-processors machines") ],
67 [ '--for-gui', '-g', GetoptLong::NO_ARGUMENT, _("Do the minimum work to be able to see the album under the GUI (don't generate all thumbnails)") ],
69 [ '--verbose-level', '-v', GetoptLong::REQUIRED_ARGUMENT, _("Set max verbosity level (0: errors, 1: warnings, 2: important messages, 3: other messages)") ],
70 [ '--info-pipe', '-i', GetoptLong::REQUIRED_ARGUMENT, _("Name a file where to write information about what's going on (used by the GUI)") ],
73 #- default values for some globals
77 $ignore_videos = false
82 puts _("Usage: %s [OPTION]...") % File.basename($0)
84 printf " %3s, %-18s %s\n", ary[1], ary[0], ary[3]
89 parser = GetoptLong.new
90 parser.set_options(*$options.collect { |ary| ary[0..2] })
92 parser.each_option do |name, arg|
99 puts _("Booh version %s
101 Copyright (c) 2005-2008 Guillaume Cottenceau.
102 This is free software; see the source for copying conditions. There is NO
103 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.") % $VERSION
108 $source = File.expand_path(arg.sub(%r|/$|, ''))
109 if !File.directory?($source)
110 die _("Argument to --source must be a directory")
113 $dest = File.expand_path(arg.sub(%r|/$|, ''))
114 if File.exists?($dest) && !File.directory?($dest)
115 die _("If --destination exists, it must be a directory")
117 if $dest != make_dest_filename($dest)
118 die _("Sorry, destination directory can't contain non simple alphanumeric characters.")
121 # system("rm -rf #{$dest}")
126 arg = File.expand_path(arg)
127 if File.readable?(arg)
128 $xmldoc = REXML::Document.new File.new(arg)
131 die _('Config file does not exist or is unreadable.')
134 arg = File.expand_path(arg)
136 if File.directory?(arg)
137 die _("Config skeleton file (%s) already exists and is a directory! Please change the filename.") % arg
139 msg 1, _("Config skeleton file already exists, backuping to %s.backup") % arg
140 File.rename(arg, "#{arg}.backup")
143 $config_writeto = arg
145 when '--merge-config'
146 arg = File.expand_path(arg)
147 if File.readable?(arg)
148 msg 2, _("Merge config notice: backuping current config file to %s.backup") % arg
149 $xmldoc = REXML::Document.new File.new(arg)
150 File.rename(arg, "#{arg}.backup")
151 $config_writeto = arg
152 $mode = 'merge_config'
154 die _('Config file does not exist or is unreadable.')
156 when '--merge-config-onedir'
157 arg = File.expand_path(arg)
158 if File.readable?(arg)
159 msg 2, _("Merge config notice: backuping current config file to %s.backup") % arg
160 $xmldoc = REXML::Document.new File.new(arg)
161 File.rename(arg, "#{arg}.backup")
162 $config_writeto = arg
163 $mode = 'merge_config_onedir'
165 die _('Config file does not exist or is unreadable.')
167 when '--merge-config-subdirs'
168 arg = File.expand_path(arg)
169 if File.readable?(arg)
170 msg 2, _("Merge config notice: backuping current config file to %s.backup") % arg
171 $xmldoc = REXML::Document.new File.new(arg)
172 File.rename(arg, "#{arg}.backup")
173 $config_writeto = arg
174 $mode = 'merge_config_subdirs'
176 die _('Config file does not exist or is unreadable.')
179 arg = File.expand_path(arg)
180 if !File.readable?(arg)
181 die _('Specified directory to merge with --dir is not readable')
186 arg = File.expand_path(arg)
187 if File.readable?(arg)
188 msg 2, _("Use config notice: backuping current config file to %s.backup") % arg
189 $xmldoc = REXML::Document.new File.new(arg)
190 File.rename(arg, "#{arg}.backup")
191 $config_writeto = arg
192 $mode = 'use_config_changetheme'
194 die _('Config file does not exist or is unreadable.')
200 when '--multi-languages'
201 parts = arg.split(',')
202 if parts.size == 0 || parts.find_all { |e| ! SUPPORTED_LANGUAGES.include?(e.strip) }.size > 0
203 die _("--multi-languages: argument must be a comma-separated list of supported languages, with last element used as the fallback language; for example: 'fr,eo,en,en'; supported languages: %s") % SUPPORTED_LANGUAGES.join(', ')
205 $multi_languages = [ parts[0..-2], parts[-1] ]
207 when '--thumbnails-per-row'
210 when '--thumbnails-per-page'
213 when '--optimize-for-32'
214 $optimize_for_32 = true
216 when '--transcode-videos'
217 parts = arg.split(':', 2)
218 if parts.size != 2 || parts[0] =~ / / || arg !~ /%f/ || arg !~ /%o/
219 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")
221 $transcode_videos = arg
229 when '--comments-format'
230 $commentsformat = arg
242 when '--verbose-level'
243 $verbose_level = arg.to_i
246 $info_pipe = File.open(arg, File::WRONLY)
247 $info_pipe.sync = true
256 if !$source && $xmldoc
257 $source = from_utf8($xmldoc.root.attributes['source']).sub(%r|/$|, '')
258 $dest = from_utf8($xmldoc.root.attributes['destination']).sub(%r|/$|, '')
259 $theme ||= $xmldoc.root.attributes['theme']
260 $limit_sizes ||= $xmldoc.root.attributes['limit-sizes']
261 if $mode == 'use_config' || $mode =~ /^merge_config/
262 $optimize_for_32 = !$xmldoc.root.attributes['optimize-for-32'].nil?
263 $N_per_row = $xmldoc.root.attributes['thumbnails-per-row']
264 languages = $xmldoc.root.attributes['multi-languages']
266 languages = languages.split(',')
267 $multi_languages = [ languages[0..-2], languages[-1] ]
269 $N_per_page = $xmldoc.root.attributes['thumbnails-per-page']
270 $madewith = $xmldoc.root.attributes['made-with']
271 $indexlink = $xmldoc.root.attributes['index-link']
275 if $mode == 'merge_config_onedir' && !$onedir
276 die _("Missing --dir for --merge-config-onedir")
278 if $mode == 'merge_config_subdirs' && !$onedir
279 die _("Missing --dir for --merge-config-subdirs")
287 die _("Missing --destination parameter.")
293 select_theme($theme, $limit_sizes, $optimize_for_32, $N_per_row)
296 $xmldoc = Document.new "<booh/>"
297 $xmldoc << XMLDecl.new(XMLDecl::DEFAULT_VERSION, $CURRENT_CHARSET)
298 $xmldoc.root.add_attribute('version', $VERSION)
299 $xmldoc.root.add_attribute('source', $source)
300 $xmldoc.root.add_attribute('destination', $dest)
301 $xmldoc.root.add_attribute('theme', $theme)
303 $xmldoc.root.add_attribute('limit-sizes', $limit_sizes)
306 $xmldoc.root.add_attribute('multi-languages', $multi_languages[0].join(',') + ',' + $multi_languages[1])
309 $xmldoc.root.add_attribute('optimize-for-32', 'true')
312 $xmldoc.root.add_attribute('thumbnails-per-row', $N_per_row)
315 $xmldoc.root.add_attribute('thumbnails-per-page', $N_per_page)
318 $xmldoc.root.add_attribute('made-with', $madewith)
321 $xmldoc.root.add_attribute('index-link', $indexlink)
326 if $mode == 'merge_config' || $mode == 'use_config_changetheme'
327 $xmldoc.root.add_attribute('theme', $theme)
328 $xmldoc.root.add_attribute('version', $VERSION)
330 $xmldoc.root.add_attribute('limit-sizes', $limit_sizes)
332 $xmldoc.root.delete_attribute('limit-sizes')
335 $xmldoc.root.add_attribute('multi-languages', $multi_languages[0].join(',') + ',' + $multi_languages[1])
337 $xmldoc.root.delete_attribute('multi-languages')
341 $xmldoc.root.add_attribute('optimize-for-32', 'true')
343 $xmldoc.root.delete_attribute('optimize-for-32')
346 $xmldoc.root.add_attribute('thumbnails-per-row', $N_per_row)
348 $xmldoc.root.delete_attribute('thumbnails-per-row')
351 $xmldoc.root.add_attribute('thumbnails-per-page', $N_per_page)
353 $xmldoc.root.delete_attribute('thumbnails-per-page')
356 $xmldoc.root.add_attribute('made-with', $madewith)
358 $xmldoc.root.delete_attribute('made-with')
361 $xmldoc.root.add_attribute('index-link', $indexlink)
363 $xmldoc.root.delete_attribute('index-link')
368 $xmldoc.root.add_attribute('transcode-videos', $transcode_videos)
370 $xmldoc.root.delete_attribute('transcode-videos')
374 $madewith = $madewith.gsub('%booh', '"http://booh.org/"')
379 $htmlsuffix = '.html'
392 $info_pipe.puts(value)
398 $info_pipe.puts("die: " + value)
403 def check_installation
404 if !system("which convert >/dev/null 2>/dev/null")
405 die _("The program 'convert' is needed. Please install it.
406 It is generally available with the 'ImageMagick' software package.")
408 if !system("which identify >/dev/null 2>/dev/null")
409 msg 1, _("the program 'identify' is needed to get images sizes and EXIF data. Please install it.
410 It is generally available with the 'ImageMagick' software package.")
413 missing = %w(mplayer).delete_if { |prg| system("which #{prg} >/dev/null 2>/dev/null") }
415 msg 1, _("the following program(s) are needed to handle videos: '%s'. Videos will be ignored.") % missing.join(', ')
416 $ignore_videos = true
420 def replace_line(surround, keyword, line)
422 contents = eval "$#{keyword}"
423 line.sub!(/#{surround}#{keyword}#{surround}/, contents)
425 die _("No '%s' found for substitution") % keyword
429 def build_html_skeletons
430 $html_images = File.open("#{$FPATH}/themes/#{$theme}/skeleton_image.html").readlines
431 $html_thumbnails = File.open("#{$FPATH}/themes/#{$theme}/skeleton_thumbnails.html").readlines
432 $html_index = File.open("#{$FPATH}/themes/#{$theme}/skeleton_index.html").readlines
433 for line in $html_images + $html_thumbnails + $html_index
434 while line =~ /~~~(\w+)~~~/
435 replace_line('~~~', $1, line)
440 def find_caption_value(xmldir, filename)
441 if cap = xmldir.elements["*[@filename='#{utf8(filename)}']"].attributes['caption']
442 return cap.gsub("\n", '<br/>')
448 def find_captions(xmldir, images)
449 return images.collect { |img| find_caption_value(xmldir, img) }
452 #- stolen from CVSspam
454 text.sub(/[^a-zA-Z0-9\-,.*_\/]/) do
455 "%#{sprintf('%2X', $&[0])}"
460 return $limit_sizes =~ /original/ ? $images_size + [ { 'name' => 'original' } ] : $images_size
463 def html_reload_to_thumbnails
464 html_reload_to_thumbnails = $preferred_size_reloader.clone
465 html_reload_to_thumbnails.gsub!(/~~theme~~/, $theme)
466 html_reload_to_thumbnails.gsub!(/~~default_size~~/, $default_size['name'])
467 html_reload_to_thumbnails.gsub!(/~~htmlsuffix~~/, $htmlsuffix)
468 html_reload_to_thumbnails.gsub!(/~~all_sizes~~/, all_images_sizes.collect { |s| "\"#{size2js(s['name'])}\"" }.join(', '))
469 size_auto_chooser = '';
470 all_images_sizes.find_all { |s| s.has_key?('optimizedforwidth') }.
471 sort { |a,b| b['optimizedforwidth'].to_i <=> a['optimizedforwidth'].to_i }.
472 each { |s| size_auto_chooser += "if (w + 50 > #{s['optimizedforwidth']}) { return 'thumbnails-#{size2js(s['name'])}-0#{$htmlsuffix}'; }\n" }
473 html_reload_to_thumbnails.gsub!(/~~size_auto_chooser~~/, size_auto_chooser)
474 return html_reload_to_thumbnails
477 def discover_iterations(iterations, line)
478 if line =~ /~~iterate(\d)_open(_max(\d+|N))?~~/
479 for iter in iterations.values
482 iter['close_wait'] = $1.to_i
485 max = $3 == 'N' ? ($N_per_row || $default_N) : $3
486 iterations[$1.to_i] = { 'open' => true, 'max' => max, 'opening' => '', 'closing' => '' }
488 line.sub!(/.*/, '~~thumbnails~~')
492 elsif line =~ /~~iterate(\d)_close~~/
493 iterations[$1.to_i]['open'] = false;
494 iterations[$1.to_i]['close'] = true;
497 for iter in iterations.values
499 iter['opening'] += line
502 if !iter['close'] && iter['close_wait'] && iterations[iter['close_wait']]['close']
503 iter['closing'] += line
510 def reset_iterations(iterations)
511 for iter in iterations.values
516 def run_iterations(iterations, amount)
519 for level in iterations.keys.sort
520 if iterations[level]['value'] == 0
521 html += iterations[level]['opening']
522 elsif level == iterations.keys.max
523 if !iterations[level]['max'] || iterations[level]['max'] && iterations[level]['value'] + amount <= iterations[level]['max'].to_i
524 html += iterations[level]['opening']
529 iterations[level]['value'] += amount
530 if iterations[level]['max'] && iterations[level]['value'] > iterations[level]['max'].to_i
531 iterations[level]['value'] = 0
532 iterations[level-1]['value'] = 0
533 html += iterations[level-1]['closing']
537 return html + run_iterations(iterations, amount)
543 def close_iterations(iterations)
545 for level in iterations.keys.sort.reverse
546 html += iterations[level]['closing']
551 def img_element(fullpath)
552 if size = get_image_size(fullpath)
553 sizespec = 'width="' + size[:x].to_s + '" height="' + size[:y].to_s + '"'
557 return '<img src="' + File.basename(fullpath) + '" ' + sizespec + ' alt="image"/>'
561 return name.gsub(/-/, '')
564 def substitute_html_sizes(html, sizeobj, type, suffix)
566 if $images_size.length > 1 || (type == 'image' && $limit_sizes =~ /original/)
567 for sizeobj2 in $images_size
568 sizejs = size2js(sizeobj2['name'])
569 sizen = defer_translation(sizename(sizeobj2['name'], false))
570 if sizeobj != sizeobj2
571 if type == 'thumbnails'
572 sizestrings << '<a href="thumbnails-' + sizejs + suffix + $htmlsuffix + '" onclick="set_preferred_size(\'' + sizejs + '\')">' + sizen + '</a>'
574 sizestrings << '<a id="link' + sizejs + '" onclick="set_preferred_size(\'' + sizejs + '\')">' + sizen + '</a>'
580 if type == 'image' && $limit_sizes =~ /original/
581 sizestrings << '<a id="linkoriginal" target="newframe">' + defer_translation(sizename('original'), false) + '</a>'
584 html.sub!(/~~sizes~~(.+)~~/) { sizestrings.join($1) }
587 def substitute_navigation(html, xmldir)
588 if xmldir.parent.name == 'dir'
591 parent = xmldir.parent
592 while parent.name == 'dir'
593 parentcaption = parent.attributes['subdirs-caption'] || File.basename(parent.attributes['path'])
594 nav = "<a href=\"#{path}/index#{$htmlsuffix}\">#{parentcaption}</a> #{defer_translation(N_(" > "))} #{nav}"
596 parent = parent.parent
598 html.gsub!(/~~ifnavigation\?~~(.+?)~~fi~~/) { $1 }
599 html.gsub!(/~~navigation~~/, nav + (xmldir.attributes['subdirs-caption'] || File.basename(xmldir.attributes['path'])))
601 html.gsub!(/~~ifnavigation\?~~(.+?)~~fi~~/, '')
605 def substitute_pathtobase(html, xmldir)
608 while location.parent.name == 'dir'
610 location = location.parent
612 html.gsub!(/~~pathtobase~~/, path)
615 def xmldir2destdir(xmldir)
616 return make_dest_filename(from_utf8(File.basename(xmldir.attributes['path'])))
619 def find_previous_album(xmldir)
622 #- move to previous dir element if exists
623 if prevelem = xmldir.previous_element_byname_notattr('dir', 'deleted')
625 relative_pos += '../' + xmldir2destdir(xmldir) + '/'
627 #- after having moved to previous dir, we need to go down last subdir until the last one
628 while child = xmldir.elements['dir']
629 while nextchild = child.next_element_byname_notattr('dir', 'deleted')
632 relative_pos += xmldir2destdir(child) + '/'
636 #- previous dir doesn't exist, move to previous dir element if exists
637 xmldir = xmldir.parent
638 if xmldir.name == 'dir' && !xmldir.attributes['deleted']
639 relative_pos += '../'
644 end while !xmldir.child_byname_notattr('image', 'deleted') && !xmldir.child_byname_notattr('video', 'deleted')
645 return File.reduce_path(relative_pos)
648 def find_next_album(xmldir)
651 #- first child dir element (catches when initial xmldir has both thumbnails and subdirs)
652 if firstchild = xmldir.child_byname_notattr('dir', 'deleted')
654 relative_pos += xmldir2destdir(xmldir) + '/'
656 elsif nextbro = xmldir.next_element_byname_notattr('dir', 'deleted')
658 relative_pos += '../' + xmldir2destdir(xmldir) + '/'
660 #- go up until we have a next brother or we are finished
662 xmldir = xmldir.parent
663 relative_pos += '../'
664 end while xmldir && !xmldir.next_element_byname_notattr('dir', 'deleted')
666 xmldir = xmldir.next_element_byname('dir')
667 relative_pos += '../' + xmldir2destdir(xmldir) + '/'
672 end while !xmldir.child_byname_notattr('image', 'deleted') && !xmldir.child_byname_notattr('video', 'deleted')
673 return File.reduce_path(relative_pos)
676 def find_translation_for_file(file, msg)
678 if file =~ /\.(\w\w)\.html$/
679 bindtextdomain("booh", { :locale => "#{$1}.UTF-8" })
681 Locale.set_current($default_locale)
684 die "Internal error: cannot find multi language suffix of file '#{file}'"
691 def sub_previous_next_album(file, previous_album, next_album, html, previous_album_msg, next_album_msg)
693 html.gsub!(/~~previous_album~~/, '<a href="' + previous_album + 'thumbnails' + $htmlsuffix + '">' + previous_album_msg + '</a>')
694 html.gsub!(/~~ifprevious_album\?~~(.+?)~~fi~~/) { $1 }
696 html.gsub!(/~~previous_album~~/, '')
697 html.gsub!(/~~ifprevious_album\?~~(.+?)~~fi~~/, '')
700 html.gsub!(/~~next_album~~/, '<a href="' + next_album + 'thumbnails' + $htmlsuffix + '">' + next_album_msg + '</a>')
701 html.gsub!(/~~ifnext_album\?~~(.+?)~~fi~~/) { $1 }
703 html.gsub!(/~~next_album~~/, '')
704 html.gsub!(/~~ifnext_album\?~~(.+?)~~fi~~/, '')
709 def save_html(html, base_filename)
710 if html.class == Array
711 html = html.join("\n")
714 for language in ($multi_languages[0] + [ $multi_languages[1] ]).uniq
715 bindtextdomain("booh", { :locale => "#{language}.UTF-8" })
716 ios = File.open("#{base_filename}.#{language}.html", "w")
717 ios.write(html.gsub(/@@(.*?)@@/) { _($1) })
719 Locale.set_current($default_locale)
722 ios = File.open("#{base_filename}.html", "w")
723 ios.write(html.gsub(/@@(.*?)@@/) { utf8(_($1)) })
730 #- preprocess the path->dir, rexml is very slow with that; we seem to improve speed by 7%
732 $xmldoc.elements.each('//dir') { |elem|
733 optxpath[elem.attributes['path']] = elem
737 if $mode == 'merge_config_onedir'
738 examined_dirs = [ $onedir ]
739 elsif $mode == 'merge_config_subdirs'
740 examined_dirs = `find '#{$onedir}' -type d -follow`.sort.collect { |v| v.chomp }.delete_if { |v| optxpath.has_key?(utf8(v)) }
742 examined_dirs = `find '#{$source}' -type d -follow`.sort.collect { |v| v.chomp }
743 if $mode == 'merge_config'
744 $xmldoc.elements.each('//dir') { |elem|
745 if ! examined_dirs.include?(elem.attributes['path'])
746 msg 2, _("Merging config: removing directory %s from config, isn't on filesystem anymore") % elem.attributes['path']
752 info("directories: #{examined_dirs.length}, sizes: #{$images_size.length}")
754 examined_dirs.each { |dir|
756 die _("Source directory or sub-directories can't contain a single-quote character, sorry: %s") % dir
758 if $mode !~ /^use_config/
759 Dir.entries(dir).each { |file|
760 if file =~ /['"\[\]]/
761 die _("Files can't contain any of the characters ', \", [ or ], sorry: %s") % "#{dir}/#{file}"
767 examined_dirs.each { |dir|
768 if File.basename(dir) =~ /^\./
769 msg 1, _("Ignoring directory %s, begins with a dot (indicating a hidden directory)") % dir
773 dest_dir = make_dest_filename(dir.sub(/^#{Regexp.quote($source)}/, $dest))
775 #- place xml document on proper node if exists, else create
776 xmldir = optxpath[utf8(dir)]
777 if $mode == 'use_config' || $mode == 'use_config_changetheme'
778 if !xmldir || (xmldir.attributes['already-generated'] && !$force) || xmldir.attributes['deleted']
779 info("walking: #{dir}|#{$source}, 0 elements")
780 if xmldir && xmldir.attributes['deleted']
781 system("rm -rf '#{dest_dir}'")
786 if $mode == 'gen_config' || (($mode == 'merge_config' || $mode == 'merge_config_subdirs') && !xmldir)
787 #- add the <dir..> element if necessary
788 parent = File.dirname(dir)
789 xmldir = $xmldoc.elements["//dir[@path='#{utf8(parent)}']"]
791 xmldir = $xmldoc.root
793 #- need to remove the already-generated mark of the parent because of the sub-albums page containing now one more element
794 xmldir.delete_attribute('already-generated')
795 xmldir = optxpath[utf8(dir)] = xmldir.add_element('dir', { 'path' => utf8(dir) })
798 xmldir.delete_attribute('already-generated')
800 #- read images/videos entries from config or from directories depending on mode
802 if $mode == 'use_config' || $mode == 'use_config_changetheme'
803 msg 2, _("Handling %s from config list...") % dir
804 xmldir.elements.each { |element|
805 if %w(image video).include?(element.name) && !element.attributes['deleted']
806 entries << from_utf8(element.attributes['filename'])
810 msg 2, _("Examining %s...") % dir
811 entries = Dir.entries(dir).sort
812 #- populate config in case of gen_config, add new files in case of merge_config
814 if file =~ /['"\[\]]/
815 msg 1, _("Ignoring %s, contains one of forbidden characters: '\"[]") % "#{dir}/#{file}"
817 type = entry2type(file)
818 if type && !xmldir.elements["#{type}[@filename='#{utf8(file)}']"]
819 #- hack: don't run identify (which is slow) if format only contains default %t
820 if $commentsformat && type == 'image' && $commentsformat != '%t'
821 comment = utf8(`identify -format "#{$commentsformat}" '#{dir}/#{file}'`.chomp.sub(/\.$/, ''))
823 comment = utf8cut(file.sub(/\.[^\.]+$/, ''), 18)
825 xmldir.add_element type, { "filename" => utf8(file), "caption" => comment }
829 if $mode != 'gen_config'
830 #- cleanup removed files from config and reread entries from config to get proper ordering
832 xmldir.elements.each { |element|
833 fullpath = "#{dir}/#{from_utf8(element.attributes['filename'])}"
834 if %w(image video).include?(element.name)
835 if !File.readable?(fullpath)
836 msg 1, _("Config merge: removing %s from config; use the backup file to retrieve caption info if this was a mistake") % fullpath
837 xmldir.delete(element)
838 elsif !element.attributes['deleted']
839 entries << from_utf8(element.attributes['filename'])
843 #- if there is no more elements here, there is no album here anymore
844 if !xmldir.child_byname_notattr('image', 'deleted') && !xmldir.child_byname_notattr('video', 'deleted')
845 xmldir.delete_attribute('thumbnails-caption')
846 xmldir.delete_attribute('thumbnails-captionfile')
850 images = entries.find_all { |e| entry2type(e) == 'image' }
851 msg 3, _("\t%s photos") % images.length
852 videos = entries.find_all { |e| entry2type(e) == 'video' }
853 msg 3, _("\t%s videos") % videos.length
854 info("walking: #{dir}|#{$source}, #{images.length + videos.length} elements")
856 system("mkdir -p '#{dest_dir}'")
858 #- generate .htaccess file
860 ios = File.open("#{dest_dir}/.htaccess", "w")
861 ios.write("AddCharset UTF-8 .html\n")
862 if auth_user_file = xmldir.attributes['password-protect']
863 msg 3, _("\tgenerating password protection file #{dest_dir}/.htaccess")
864 ios.write("AuthType Basic\nAuthName \"protected area\"\nAuthUserFile #{auth_user_file}\nrequire valid-user\n")
867 ios.write("Options Multiviews\n")
868 ios.write("LanguagePriority #{$multi_languages[1]}\n")
869 ios.write("ForceLanguagePriority Prefer Fallback\n")
870 ios.write("DirectoryIndex index\n")
875 #- pass through if there are no images and videos
876 if images.size == 0 && videos.size == 0
878 #- cleanup old images/videos, especially if this directory contained images/videos previously.
879 if $mode != 'gen_config'
880 rightful_images = [ '.htaccess' ]
881 if xmldir.attributes['thumbnails-caption']
882 rightful_images << 'thumbnails-thumbnail.jpg'
884 xmldir.elements.each('dir') { |child|
885 if child.attributes['deleted']
888 subdir = make_dest_filename(from_utf8(File.basename(child.attributes['path'])))
889 rightful_images << "thumbnails-#{subdir}.jpg"
891 to_del = Dir.entries(dest_dir).find_all { |e| !File.directory?(File.join(dest_dir, e)) && !rightful_images.include?(e) }
893 File.delete(*to_del.collect { |e| File.join(dest_dir, e) })
897 #- copy any resource file that goes with the theme (css, images..)
898 themestuff = Dir.entries("#{$FPATH}/themes/#{$theme}").
899 find_all { |e| !%w(. .. skeleton_image.html skeleton_thumbnails.html skeleton_index.html metadata root CVS).include?(e) }
900 themestuff.each { |entry|
901 if !File.exists?(File.join(dest_dir, entry))
902 psys("cp '#{$FPATH}/themes/#{$theme}/#{entry}' '#{dest_dir}'")
906 #- copy any root-only resource file that goes with the theme (css, images..)
907 if xmldir.parent.name != 'dir'
908 themestuff_root = Dir.entries("#{$FPATH}/themes/#{$theme}/root").
909 find_all { |e| !%w(. .. CVS).include?(e) }
910 themestuff_root.each { |entry|
911 if !File.exists?(File.join(dest_dir, entry))
912 psys("cp '#{$FPATH}/themes/#{$theme}/root/#{entry}' '#{dest_dir}'")
920 msg 2, _("Outputting in %s...") % dest_dir
922 #- populate data structure with sizes from theme
923 for sizeobj in $images_size
924 fullscreen_images ||= {}
925 fullscreen_images[sizeobj['name']] = []
926 thumbnail_images ||= {}
927 thumbnail_images[sizeobj['name']] = []
928 thumbnail_videos ||= {}
929 thumbnail_videos[sizeobj['name']] = []
931 #- a special dummy size to keep 'references' to thumbnails in case of panorama, because the GUI will use the regular thumbnails
932 thumbnail_images['dont-delete-file-for-gui'] = []
933 if $limit_sizes =~ /original/
934 fullscreen_images['original'] = []
937 images.size >= 1 and msg 3, _("\tcreating photos thumbnails...")
939 #- create thumbnails for images
941 info("processing element")
942 base_dest_img = dest_dir + '/' + make_dest_filename(img.sub(/\.[^\.]+$/, ''))
944 thumbnail_dest_img = base_dest_img + "-#{$default_size['thumbnails']}.jpg"
945 gen_thumbnails_element("#{dir}/#{img}", xmldir, true, [ { 'filename' => thumbnail_dest_img, 'size' => $default_size['thumbnails'] } ])
948 elem = xmldir.elements["image[@filename='#{utf8(img)}']"]
949 for sizeobj in $images_size
950 size_fullscreen = sizeobj['fullscreen']
951 size_thumbnails = sizeobj['thumbnails']
952 fullscreen_dest_img = base_dest_img + "-#{size_fullscreen}.jpg"
953 fullscreen_images[sizeobj['name']] << File.basename(fullscreen_dest_img)
954 todo << { 'filename' => fullscreen_dest_img, 'size' => size_fullscreen }
955 if pano = pano_amount(elem)
956 thumbnail_images['dont-delete-file-for-gui'] << File.basename(base_dest_img + "-#{size_thumbnails}.jpg")
957 size_thumbnails = size_thumbnails.sub(/(\d+)/) { ($1.to_i * pano).to_i }
959 thumbnail_dest_img = base_dest_img + "-#{size_thumbnails}.jpg"
960 thumbnail_images[sizeobj['name']] << File.basename(thumbnail_dest_img)
961 todo << { 'filename' => thumbnail_dest_img, 'size' => size_thumbnails }
963 gen_thumbnails_element("#{dir}/#{img}", xmldir, true, todo)
964 if $limit_sizes =~ /original/
965 fullscreen_images['original'] << img
967 destimg = "#{dest_dir}/#{img}"
968 if $limit_sizes =~ /original/ && !File.exists?(destimg)
970 if ! sys("ln '#{dir}/#{img}' '#{destimg}'")
971 $hardlinks_ok = false
975 psys("cp '#{dir}/#{img}' '#{destimg}'")
981 videos.size >= 1 and msg 3, _("\tcreating videos thumbnails...")
982 transcoded_videos = {}
984 #- create thumbnails for videos
985 videos.each { |video|
986 info("processing element")
988 thumbnail_dest_img = dest_dir + '/' + make_dest_filename(video.sub(/\.[^\.]+$/, '')) + "-#{$default_size['thumbnails']}.jpg"
989 gen_thumbnails_element("#{dir}/#{video}", xmldir, true, [ { 'filename' => thumbnail_dest_img, 'size' => $default_size['thumbnails'] } ])
992 for sizeobj in $images_size
993 size_thumbnails = sizeobj['thumbnails']
994 thumbnail_dest_img = dest_dir + '/' + make_dest_filename(video.sub(/\.[^\.]+$/, '')) + "-#{size_thumbnails}.jpg"
995 thumbnail_videos[sizeobj['name']] << File.basename(thumbnail_dest_img)
996 todo << { 'filename' => thumbnail_dest_img, 'size' => size_thumbnails }
998 gen_thumbnails_element("#{dir}/#{video}", xmldir, true, todo)
1000 if $transcode_videos
1001 parts = $transcode_videos.split(':', 2)
1002 basedestvideo = video.sub(/\.\w+/, '') + '.' + parts[0]
1003 transcoded_videos[video] = basedestvideo
1004 destvideo = "#{dest_dir}/#{basedestvideo}"
1005 if ! File.exists?(destvideo)
1006 psys(parts[1].gsub(/%f/, "'#{dir}/#{video}'").gsub(/%o/, "'#{destvideo}'"))
1009 destvideo = "#{dest_dir}/#{video}"
1010 if ! File.exists?(destvideo)
1012 if ! sys("ln '#{dir}/#{video}' '#{destvideo}'")
1013 $hardlinks_ok = false
1017 psys("cp '#{dir}/#{video}' '#{destvideo}'")
1025 #- cleanup old images/videos (for when removing elements or sizes)
1026 all_elements = fullscreen_images.collect { |e| e[1] }.flatten.
1027 concat(thumbnail_images.collect { |e| e[1] }.flatten).
1028 concat(thumbnail_videos.collect { |e| e[1] }.flatten).
1029 concat($transcode_videos ? transcoded_videos.values : videos).
1031 to_del = Dir.entries(dest_dir).find_all { |e| !File.directory?(File.join(dest_dir, e)) && !all_elements.include?(e) && e !~ /^thumbnails-\w+\.jpg/ }
1033 msg 3, _("\tcleaning up: #{to_del.join(', ')}")
1034 File.delete(*to_del.collect { |e| File.join(dest_dir, e) })
1037 #- copy any resource file that goes with the theme (css, images..)
1038 themestuff = Dir.entries("#{$FPATH}/themes/#{$theme}").
1039 find_all { |e| !%w(. .. skeleton_image.html skeleton_thumbnails.html skeleton_index.html metadata root CVS).include?(e) }
1040 themestuff.each { |entry|
1041 if !File.exists?(File.join(dest_dir, entry))
1042 psys("cp '#{$FPATH}/themes/#{$theme}/#{entry}' '#{dest_dir}'")
1046 #- copy any root-only resource file that goes with the theme (css, images..)
1047 if xmldir.parent.name != 'dir'
1048 themestuff_root = Dir.entries("#{$FPATH}/themes/#{$theme}/root").
1049 find_all { |e| !%w(. .. CVS).include?(e) }
1050 themestuff_root.each { |entry|
1051 if !File.exists?(File.join(dest_dir, entry))
1052 psys("cp '#{$FPATH}/themes/#{$theme}/root/#{entry}' '#{dest_dir}'")
1057 msg 3, _("\tgenerating HTML pages...")
1058 #- fixup max per page
1059 if $N_per_page && $N_per_row
1060 $N_per_page = $N_per_page.to_i / $N_per_row.to_i * $N_per_row.to_i
1063 #- generate thumbnails*.html (page with thumbnails)
1064 image2thumbnailpage4js = []
1065 for sizeobj in $images_size
1066 info("processing size")
1067 html = $html_thumbnails.collect { |l| l.clone }
1071 xmldir.attributes['thumbnails-caption'] || utf8(File.basename(dir)))
1072 discover_iterations(iterations, i)
1075 html_thumbnails = ''
1076 html_thumbnails_nojs = ''
1079 reset_iterations(iterations)
1080 #- preprocess the @filename->elem, rexml is very slow with that; we dramatically improve this part of the processing
1082 xmldir.elements.each('image') { |elem|
1083 optfilename[elem.attributes['filename']] = elem
1086 type = images.include?(file) ? 'image' : videos.include?(file) ? 'video' : nil
1088 homogeinize_width = 100 / $N_per_row.to_i
1089 if type == 'image' && elem = optfilename[utf8(file)]
1090 if pano = pano_amount(elem)
1091 html_elem = run_iterations(iterations, pano)
1092 counter += count = pano.ceil
1093 html_elem.gsub!(/~~colspan~~/) { "colspan=\"#{count}\"" }
1094 homogeinize_width *= count
1096 html_elem = run_iterations(iterations, 1)
1098 html_elem.gsub!(/~~colspan~~/, '')
1101 html_elem = run_iterations(iterations, 1)
1103 html_elem.gsub!(/~~colspan~~/, '')
1105 html_elem.gsub!(/~~homogeinize_width~~/) { "width=\"#{homogeinize_width}%\"" }
1107 index = images.index(file)
1108 html_elem.gsub!(/~~caption_iteration~~/,
1109 find_caption_value(xmldir, images[index]) || utf8(images[index]))
1110 html_elem.gsub!(/~~ifimage\?~~(.+?)~~fi~~/) { $1 }
1111 html_elem.gsub!(/~~ifvideo\?~~(.+?)~~fi~~/, '')
1112 elsif type == 'video'
1113 index = videos.index(file)
1114 if File.exists?("#{dest_dir}/#{thumbnail_videos[sizeobj['name']][index]}")
1115 html_elem.gsub!(/~~image_iteration~~/,
1116 '<a href="' + ( $transcode_videos ? transcoded_videos[videos[index]] : videos[index] ) + '">' +
1117 img_element("#{dest_dir}/#{thumbnail_videos[sizeobj['name']][index]}") + '</a>')
1119 html_elem.gsub!(/~~image_iteration~~/,
1120 '<a href="' + ( $transcode_videos ? transcoded_videos[videos[index]] : videos[index] ) + '">' +
1121 defer_translation(N_("(no preview)")) + '</a>')
1123 html_elem.gsub!(/~~caption_iteration~~/, find_caption_value(xmldir, videos[index]) || utf8(videos[index]))
1124 html_elem.gsub!(/~~ifimage\?~~(.+?)~~fi~~/, '')
1125 html_elem.gsub!(/~~ifvideo\?~~(.+?)~~fi~~/) { $1 }
1127 html_thumbnails += html_elem
1128 html_thumbnails_nojs += html_elem
1130 html_thumbnails.gsub!(/~~image_iteration~~/,
1131 '<a href="image-' + size2js(sizeobj['name']) + $htmlsuffix + '#current=' + fullscreen_images[sizeobj['name']][index] +
1132 '" name="' + fullscreen_images[sizeobj['name']][index] + '">' +
1133 img_element("#{dest_dir}/#{thumbnail_images[sizeobj['name']][index]}") + '</a>')
1134 html_thumbnails_nojs.gsub!(/~~image_iteration~~/,
1135 '<a href="' + fullscreen_images[sizeobj['name']][index] + '" name="' + fullscreen_images[sizeobj['name']][index] + '">' +
1136 img_element("#{dest_dir}/#{thumbnail_images[sizeobj['name']][index]}") + '</a>')
1137 #- remember in which thumbnails page is this element, for image->thumbnail link
1138 if sizeobj == $images_size[0]
1139 image2thumbnailpage4js << pagecount
1143 if counter == $N_per_page
1144 html_thumbnails += close_iterations(iterations)
1145 html_thumbnails_nojs += close_iterations(iterations)
1146 all_pages << [ html_thumbnails, html_thumbnails_nojs ]
1147 html_thumbnails = ''
1148 html_thumbnails_nojs = ''
1150 reset_iterations(iterations)
1156 html_thumbnails += close_iterations(iterations)
1157 html_thumbnails_nojs += close_iterations(iterations)
1158 all_pages << [ html_thumbnails, html_thumbnails_nojs ]
1161 i.gsub!(/~~theme~~/, $theme)
1162 i.gsub!(/~~current_size~~/, sizeobj['name'])
1163 i.gsub!(/~~htmlsuffix~~/, $htmlsuffix)
1164 i.gsub!(/~~current_size_js~~/, size2js(sizeobj['name']))
1165 i.gsub!(/~~madewith~~/, $madewith || '')
1166 i.gsub!(/~~indexlink~~/, $indexlink || '')
1167 substitute_navigation(i, xmldir)
1169 html_nojs = html.collect { |l| l.clone }
1171 for page in all_pages
1172 html_thumbnails, html_thumbnails_nojs = page
1173 final_html = html.collect { |l| l.clone }
1174 mstuff = defer_translation(N_("Pages: ")) +
1175 (pagecount > 0 ? "<a href=\"thumbnails-#{size2js(sizeobj['name'])}%nojs-#{pagecount - 1}#{$htmlsuffix}\">" + defer_translation(N_("<- Previous")) + "</a> " : '') +
1176 all_pages.collect_with_index { |p,idx| page == p ? idx + 1 : "<a href=\"thumbnails-#{size2js(sizeobj['name'])}%nojs-#{idx}#{$htmlsuffix}\">#{idx + 1}</a>" }.join(', ') +
1177 (pagecount < all_pages.size - 1 ? " <a href=\"thumbnails-#{size2js(sizeobj['name'])}%nojs-#{pagecount + 1}#{$htmlsuffix}\">" + defer_translation(N_("Next ->")) + "</a> " : '')
1179 i.sub!(/~~run_slideshow~~/,
1180 images.size <= 1 ? '' : '<a href="image-' + size2js(sizeobj['name']) + $htmlsuffix + '#run_slideshow=1">' + defer_translation(N_("Run slideshow!"))+'</a>')
1181 i.sub!(/~~thumbnails~~/, html_thumbnails)
1182 if all_pages.size == 1
1183 i.gsub!(/~~ifmultiplepages\?~~.*~~fi~~/, '')
1185 i.gsub!(/~~ifmultiplepages\?~~(.+?)~~fi~~/) { $1 }
1186 i.gsub!(/~~multiplepagesstuff~~/, mstuff.gsub('%nojs', ''))
1188 substitute_html_sizes(i, sizeobj, 'thumbnails', "-#{pagecount}")
1189 substitute_pathtobase(i, xmldir)
1191 save_html(final_html, "#{dest_dir}/thumbnails-#{size2js(sizeobj['name'])}-#{pagecount}")
1192 final_html_nojs = html_nojs.collect { |l| l.clone }
1193 for i in final_html_nojs
1194 i.sub!(/~~run_slideshow~~/, defer_translation(N_("<i>Click on an image to view it larger</i>")))
1195 i.sub!(/~~thumbnails~~/, html_thumbnails_nojs)
1196 if all_pages.size == 1
1197 i.gsub!(/~~ifmultiplepages\?~~.*~~fi~~/, '')
1199 i.gsub!(/~~ifmultiplepages\?~~(.+?)~~fi~~/) { $1 }
1200 i.gsub!(/~~multiplepagesstuff~~/, mstuff.gsub('%nojs', '-nojs'))
1202 substitute_html_sizes(i, sizeobj, 'thumbnails', "-nojs-#{pagecount}")
1203 substitute_pathtobase(i, xmldir)
1205 save_html(final_html_nojs, "#{dest_dir}/thumbnails-#{size2js(sizeobj['name'])}-nojs-#{pagecount}")
1210 info("finished processing sizes")
1212 #- generate "main" thumbnails.html page that will reload to correct size thanks to cookie
1213 save_html(html_reload_to_thumbnails, "#{dest_dir}/thumbnails")
1215 #- generate image.html (page with fullscreen images)
1217 captions4js = find_captions(xmldir, images).collect { |e| e ? '"' + e.gsub('"', '\"') + '"' : '""' }.join(', ')
1218 thumbnailspage4js = image2thumbnailpage4js.collect { |e| "\"#{e}\"" }.join(', ')
1219 for sizeobj in $images_size
1220 html = $html_images.collect { |l| l.clone }
1221 images4js = fullscreen_images[sizeobj['name']].collect { |e| "\"#{e}\"" }.join(', ')
1224 for sizeobj2 in all_images_sizes
1225 if sizeobj != sizeobj2
1226 otherimages4js += "var images_#{size2js(sizeobj2['name'])} = new Array(" + fullscreen_images[sizeobj2['name']].collect { |e| "\"#{e}\"" }.join(', ') + ")\n"
1227 othersizes << "\"#{size2js(sizeobj2['name'])}\""
1231 i.gsub!(/~~images~~/, images4js)
1232 i.gsub!(/~~other_images~~/, otherimages4js)
1233 i.gsub!(/~~thumbnailspages~~/, thumbnailspage4js)
1234 i.gsub!(/~~other_sizes~~/, othersizes.join(', '))
1235 i.gsub!(/~~captions~~/, captions4js)
1236 i.gsub!(/~~title~~/, xmldir.attributes['thumbnails-caption'] || utf8(File.basename(dir)))
1237 i.gsub!(/~~thumbnails~~/, '<a href="thumbnails-' + size2js(sizeobj['name']) + $htmlsuffix + '" id="thumbnails">' + defer_translation(N_('return to thumbnails')) + '</a>')
1238 i.gsub!(/~~theme~~/, $theme)
1239 i.gsub!(/~~current_size~~/, size2js(sizeobj['name']))
1240 i.gsub!(/~~htmlsuffix~~/, $htmlsuffix)
1241 i.gsub!(/~~madewith~~/, $madewith || '')
1242 i.gsub!(/~~indexlink~~/, $indexlink || '')
1243 substitute_html_sizes(i, sizeobj, 'image', '')
1244 substitute_navigation(i, xmldir)
1245 substitute_pathtobase(i, xmldir)
1247 save_html(html, "#{dest_dir}/image-#{size2js(sizeobj['name'])}")
1255 #- add attributes to <dir..> elements needing so
1256 if $mode != 'use_config'
1257 msg 3, _("\tfixating configuration file...")
1258 $xmldoc.elements.each('//dir') { |element|
1259 path = captionpath = element.attributes['path']
1260 descendant_element = element.elements['descendant::image'] || element.elements['descendant::video']
1261 if !descendant_element
1262 msg 3, _("\t\tremoving %s, no element in it") % path
1263 element.remove #- means we have a directory with nothing interesting in it
1265 captionfile = "#{descendant_element.parent.attributes['path']}/#{descendant_element.attributes['filename']}"
1266 basename = File.basename(path)
1267 if element.elements['dir']
1268 if !element.attributes['subdirs-caption']
1269 element.add_attribute('subdirs-caption', basename)
1271 if !element.attributes['subdirs-captionfile']
1272 element.add_attribute('subdirs-captionfile', captionfile)
1275 if element.child_byname_notattr('image', 'deleted') || element.child_byname_notattr('video', 'deleted')
1276 if !element.attributes['thumbnails-caption']
1277 element.add_attribute('thumbnails-caption', basename)
1279 if !element.attributes['thumbnails-captionfile']
1280 element.add_attribute('thumbnails-captionfile', captionfile)
1287 #- write down to disk config if necessary
1289 ios = File.open($config_writeto, "w")
1290 $xmldoc.write(ios, 0)
1295 msg 3, _(" completed necessary stuff for GUI, exiting.")
1299 #- second pass to create index.html files and previous/next links
1300 info("creating index.html")
1301 msg 3, _("\trescanning directories to generate all 'index.html' files...")
1303 #- recompute the memoization because elements mights have been removed (the ones with no element in them)
1305 $xmldoc.elements.each('//dir') { |elem|
1306 optxpath[elem.attributes['path']] = elem
1309 examined_dirs.each { |dir|
1310 info("index.html: #{dir}|#{$source}")
1312 xmldir = optxpath[utf8(dir)]
1313 if !xmldir || (xmldir.attributes['already-generated'] && !$force) || xmldir.attributes['deleted']
1316 dest_dir = make_dest_filename(dir.sub(/^#{Regexp.quote($source)}/, $dest))
1318 previous_album = find_previous_album(xmldir)
1319 next_album = find_next_album(xmldir)
1321 if xmldir.elements['dir']
1322 html = $html_index.collect { |l| l.clone }
1325 caption = xmldir.attributes['subdirs-caption']
1326 i.gsub!(/~~title~~/, caption)
1327 substitute_navigation(i, xmldir)
1328 substitute_pathtobase(i, xmldir)
1329 discover_iterations(iterations, i)
1333 reset_iterations(iterations)
1335 #- deal with "current" album (directs to "thumbnails" page)
1336 if xmldir.attributes['thumbnails-caption']
1337 thumbnail = "#{dest_dir}/thumbnails-thumbnail.jpg"
1338 gen_thumbnails_subdir(from_utf8(xmldir.attributes['thumbnails-captionfile']), xmldir, false,
1339 [ { 'filename' => thumbnail, 'size' => $albums_thumbnail_size } ], 'thumbnails')
1340 html_index += run_iterations(iterations, 1)
1341 html_index.gsub!(/~~image_iteration~~/, "<a href=\"thumbnails#{$htmlsuffix}\">" + img_element(thumbnail) + '</a>')
1342 html_index.gsub!(/~~caption_iteration~~/, xmldir.attributes['thumbnails-caption'])
1345 #- deal with sub-albums (direct to subdirs/index.html pages)
1346 xmldir.elements.each('dir') { |child|
1347 if child.attributes['deleted']
1350 subdir = make_dest_filename(from_utf8(File.basename(child.attributes['path'])))
1351 thumbnail = "#{dest_dir}/thumbnails-#{subdir}.jpg"
1352 html_index += run_iterations(iterations, 1)
1353 captionfile, caption = find_subalbum_caption_info(child)
1354 gen_thumbnails_subdir(captionfile, child, false,
1355 [ { 'filename' => thumbnail, 'size' => $albums_thumbnail_size } ], find_subalbum_info_type(child))
1356 html_index.gsub!(/~~caption_iteration~~/, caption)
1357 html_index.gsub!(/~~image_iteration~~/, "<a href=\"#{subdir}/index#{$htmlsuffix}\">" + img_element(thumbnail) + '</a>')
1360 html_index += close_iterations(iterations)
1363 i.gsub!(/~~thumbnails~~/, html_index)
1364 i.gsub!(/~~madewith~~/, $madewith || '')
1365 i.gsub!(/~~indexlink~~/, $indexlink || '')
1369 html = html_reload_to_thumbnails
1372 save_html(html, "#{dest_dir}/index")
1374 #- in case MultiViews will not work, generate some compat
1375 ios = File.open("#{dest_dir}/index.html", "w")
1376 ios.write("<html><head><meta http-equiv=\"refresh\" content=\"0.1;url=index.#{$multi_languages[1]}.html\"></head></html>")
1380 #- substitute multiple "return to albums", previous/next correctly
1381 #- the following two statements are dramatical optimizations to executing for each substInFile callback
1382 dirpresent = xmldir.elements['dir']
1383 parentname = xmldir.parent.name
1384 if xmldir.child_byname_notattr('image', 'deleted') || xmldir.child_byname_notattr('video', 'deleted')
1385 for suffix in [ '', '-nojs' ]
1386 for sizeobj in $images_size
1387 Dir.glob("#{dest_dir}/thumbnails-#{size2js(sizeobj['name'])}#{suffix}-*.html") do |file|
1388 #- unroll translations, they are costly if rerun for each line of files
1389 rta = find_translation_for_file(file, N_('return to albums'))
1390 pa = find_translation_for_file(file, N_('previous album'))
1391 na = find_translation_for_file(file, N_('next album'))
1392 substInFile(file) { |line|
1393 sub_previous_next_album(file, previous_album, next_album, line, pa, na)
1395 line.sub!(/~~return_to_albums~~/, '<a href="index' + $htmlsuffix + '">' + rta + '</a>')
1397 if parentname == 'dir'
1398 line.sub!(/~~return_to_albums~~/, '<a href="../index' + $htmlsuffix + '">' + rta + '</a>')
1400 line.sub!(/~~return_to_albums~~/, '')
1406 if suffix == '' && xmldir.child_byname_notattr('image', 'deleted')
1407 Dir.glob("#{dest_dir}/image-#{size2js(sizeobj['name'])}*.html") do |file|
1408 pa = find_translation_for_file(file, N_('previous album'))
1409 na = find_translation_for_file(file, N_('next album'))
1410 substInFile(file) { |line|
1411 sub_previous_next_album(file, previous_album, next_album, line, pa, na)
1420 msg 3, _(" all done.")
1427 build_html_skeletons