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) 2005 Guillaume Cottenceau <gc3 at bluewin.ch>
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., 675 Mass Ave, Cambridge, MA 02139, USA.
25 require 'rexml/document'
28 require 'booh/booh-lib'
29 require 'booh/html-merges'
31 #- bind text domain as soon as possible because some _() functions are called early to build data structures
32 bindtextdomain("booh")
36 [ '--help', '-h', GetoptLong::NO_ARGUMENT, _("Get help message") ],
37 [ '--version', '-V', GetoptLong::NO_ARGUMENT, _("Print version and exit") ],
39 [ '--source', '-s', GetoptLong::REQUIRED_ARGUMENT, _("Directory which contains original images/videos as files or subdirs") ],
40 [ '--destination', '-d', GetoptLong::REQUIRED_ARGUMENT, _("Directory which will contain the web-album") ],
41 # [ '--clean', '-c', GetoptLong::NO_ARGUMENT, _("Clean destination directory") ],
43 [ '--theme', '-t', GetoptLong::REQUIRED_ARGUMENT, _("Select HTML theme to use") ],
44 [ '--config', '-C', GetoptLong::REQUIRED_ARGUMENT, _("File containing config listing images and videos within directories with captions") ],
45 [ '--config-skel', '-k', GetoptLong::REQUIRED_ARGUMENT, _("Filename where the script will output a config skeleton") ],
46 [ '--merge-config', '-M', GetoptLong::REQUIRED_ARGUMENT, _("File containing config listing, where to merge new images/videos from --source, and change theme info") ],
47 [ '--merge-config-onedir', '-O', GetoptLong::REQUIRED_ARGUMENT, _("File containing config listing, for merging the subdir specified with --dir") ],
48 [ '--merge-config-subdirs', '-U', GetoptLong::REQUIRED_ARGUMENT, _("File containing config listing, for merging the new subdirs down the subdir specified with --dir") ],
49 [ '--dir', '-D', GetoptLong::REQUIRED_ARGUMENT, _("Directory for merge with --merge-config-onedir or --merge-config-subdirs") ],
50 [ '--use-config', '-u', GetoptLong::REQUIRED_ARGUMENT, _("File containing config listing, where to change theme info") ],
51 [ '--force', '-f', GetoptLong::NO_ARGUMENT, _("Force generation of album even if the GUI marked some directories as already generated") ],
53 [ '--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)") ],
54 [ '--thumbnails-per-row', '-T', GetoptLong::REQUIRED_ARGUMENT, _("Specify the amount of thumbnails per row in the thumbnails page (if applicable in theme)") ],
55 [ '--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 pictures from non digital cameras are 3/2 when pictures from digital cameras are 4/3)") ],
56 [ '--made-with', '-n', GetoptLong::REQUIRED_ARGUMENT, _("Specify the HTML markup to use on the bottom of pages") ],
57 [ '--empty-comments','-e', GetoptLong::NO_ARGUMENT, _("Prefer empty comments over filename when creating new albums") ],
59 [ '--mproc', '-m', GetoptLong::REQUIRED_ARGUMENT, _("Specify the number of processors for multi-processors machines") ],
61 [ '--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)") ],
63 [ '--verbose-level', '-v', GetoptLong::REQUIRED_ARGUMENT, _("Set max verbosity level (0: errors, 1: warnings, 2: important messages, 3: other messages)") ],
64 [ '--info-pipe', '-i', GetoptLong::REQUIRED_ARGUMENT, _("Name a file where to write information about what's going on (used by the GUI)") ],
67 #- default values for some globals
71 $ignore_videos = false
76 puts _("Usage: %s [OPTION]...") % File.basename($0)
78 printf " %3s, %-18s %s\n", ary[1], ary[0], ary[3]
83 parser = GetoptLong.new
84 parser.set_options(*$options.collect { |ary| ary[0..2] })
86 parser.each_option do |name, arg|
93 puts _("Booh version %s
95 Copyright (c) 2005 Guillaume Cottenceau.
96 This is free software; see the source for copying conditions. There is NO
97 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.") % $VERSION
102 $source = File.expand_path(arg.sub(%r|/$|, ''))
103 if !File.directory?($source)
104 die _("Argument to --source must be a directory")
107 $dest = File.expand_path(arg.sub(%r|/$|, ''))
108 if File.exists?($dest) && !File.directory?($dest)
109 die _("If --destination exists, it must be a directory")
111 if $dest != make_dest_filename($dest)
112 die _("Sorry, destination directory can't contain non simple alphanumeric characters.")
115 # system("rm -rf #{$dest}")
120 arg = File.expand_path(arg)
121 if File.readable?(arg)
122 $xmldoc = REXML::Document.new File.new(arg)
125 die _('Config file does not exist or is unreadable.')
128 arg = File.expand_path(arg)
130 if File.directory?(arg)
131 die _("Config skeleton file (%s) already exists and is a directory! Please change the filename.") % arg
133 msg 1, _("Config skeleton file already exists, backuping to %s.backup") % arg
134 File.rename(arg, "#{arg}.backup")
137 $config_writeto = arg
139 when '--merge-config'
140 arg = File.expand_path(arg)
141 if File.readable?(arg)
142 msg 2, _("Merge config notice: backuping current config file to %s.backup") % arg
143 $xmldoc = REXML::Document.new File.new(arg)
144 File.rename(arg, "#{arg}.backup")
145 $config_writeto = arg
146 $mode = 'merge_config'
148 die _('Config file does not exist or is unreadable.')
150 when '--merge-config-onedir'
151 arg = File.expand_path(arg)
152 if File.readable?(arg)
153 msg 2, _("Merge config notice: backuping current config file to %s.backup") % arg
154 $xmldoc = REXML::Document.new File.new(arg)
155 File.rename(arg, "#{arg}.backup")
156 $config_writeto = arg
157 $mode = 'merge_config_onedir'
159 die _('Config file does not exist or is unreadable.')
161 when '--merge-config-subdirs'
162 arg = File.expand_path(arg)
163 if File.readable?(arg)
164 msg 2, _("Merge config notice: backuping current config file to %s.backup") % arg
165 $xmldoc = REXML::Document.new File.new(arg)
166 File.rename(arg, "#{arg}.backup")
167 $config_writeto = arg
168 $mode = 'merge_config_subdirs'
170 die _('Config file does not exist or is unreadable.')
173 arg = File.expand_path(arg)
174 if !File.readable?(arg)
175 die _('Specified directory to merge with --dir is not readable')
180 arg = File.expand_path(arg)
181 if File.readable?(arg)
182 msg 2, _("Use config notice: backuping current config file to %s.backup") % arg
183 $xmldoc = REXML::Document.new File.new(arg)
184 File.rename(arg, "#{arg}.backup")
185 $config_writeto = arg
186 $mode = 'use_config_changetheme'
188 die _('Config file does not exist or is unreadable.')
194 when '--thumbnails-per-row'
197 when '--optimize-for-32'
198 $optimize_for_32 = true
203 when '--empty-comments'
204 $empty_comments = true
216 when '--verbose-level'
217 $verbose_level = arg.to_i
220 $info_pipe = File.open(arg, File::WRONLY)
221 $info_pipe.sync = true
230 if !$source && $xmldoc
231 $source = from_utf8($xmldoc.root.attributes['source'])
232 $dest = from_utf8($xmldoc.root.attributes['destination'])
233 $theme ||= $xmldoc.root.attributes['theme']
234 $limit_sizes ||= $xmldoc.root.attributes['limit-sizes']
235 if $mode == 'use_config' || $mode =~ /^merge_config/
236 $optimize_for_32 = !$xmldoc.root.attributes['optimize-for-32'].nil?
237 $N_per_row = $xmldoc.root.attributes['thumbnails-per-row']
238 $madewith = $xmldoc.root.attributes['made-with']
242 if $mode == 'merge_config_onedir' && !$onedir
243 die _("Missing --dir for --merge-config-onedir")
245 if $mode == 'merge_config_subdirs' && !$onedir
246 die _("Missing --dir for --merge-config-subdirs")
254 die _("Missing --destination parameter.")
260 select_theme($theme, $limit_sizes, $optimize_for_32, $N_per_row)
263 additional_params = ''
265 additional_params += "limit-sizes='#{$limit_sizes}'"
268 additional_params += " optimize-for-32='true'"
271 additional_params += " thumbnails-per-row='#{$N_per_row}'"
274 additional_params += " made-with='#{$madewith}'"
276 $xmldoc = Document.new "<booh version='#{$VERSION}' source='#{utf8($source)}' destination='#{utf8($dest)}' theme='#{$theme}' #{additional_params}/>"
277 $xmldoc << XMLDecl.new(XMLDecl::DEFAULT_VERSION, $CURRENT_CHARSET)
281 if $mode == 'merge_config' || $mode == 'use_config_changetheme'
282 $xmldoc.root.add_attribute('theme', $theme)
283 $xmldoc.root.add_attribute('version', $VERSION)
285 $xmldoc.root.add_attribute('limit-sizes', $limit_sizes)
287 $xmldoc.root.delete_attribute('limit-sizes')
290 $xmldoc.root.add_attribute('optimize-for-32', 'true')
292 $xmldoc.root.delete_attribute('optimize-for-32')
295 $xmldoc.root.add_attribute('thumbnails-per-row', $N_per_row)
297 $xmldoc.root.delete_attribute('thumbnails-per-row')
300 $xmldoc.root.add_attribute('made-with', $madewith)
302 $xmldoc.root.delete_attribute('made-with')
307 $madewith = $madewith.gsub('%booh', '"http://zarb.org/~gc/html/booh.html"')
320 $info_pipe.puts(value)
324 def check_installation
325 if !system("which convert >/dev/null 2>/dev/null")
326 die _("The program 'convert' is needed. Please install it.
327 It is generally available with the 'ImageMagick' software package.")
329 if !system("which identify >/dev/null 2>/dev/null")
330 msg 1, _("the program 'identify' is needed to get images sizes and EXIF data. Please install it.
331 It is generally available with the 'ImageMagick' software package.")
334 missing = %w(transcode mencoder).delete_if { |prg| system("which #{prg} >/dev/null 2>/dev/null") }
336 msg 1, _("the following program(s) are needed to handle videos: '%s'. Videos will be ignored.") % missing.join(', ')
337 $ignore_videos = true
341 def replace_line(surround, keyword, line)
343 contents = eval "$#{keyword}"
344 line.sub!(/#{surround}#{keyword}#{surround}/, contents)
346 die _("No '%s' found for substitution") % keyword
350 def build_html_skeletons
351 $html_images = File.open("#{$FPATH}/themes/#{$theme}/skeleton_image.html").readlines
352 $html_thumbnails = File.open("#{$FPATH}/themes/#{$theme}/skeleton_thumbnails.html").readlines
353 $html_index = File.open("#{$FPATH}/themes/#{$theme}/skeleton_index.html").readlines
354 for line in $html_images + $html_thumbnails + $html_index
355 while line =~ /~~~(\w+)~~~/
356 replace_line('~~~', $1, line)
361 def find_caption_value(xmldir, filename)
362 if cap = xmldir.elements["*[@filename='#{utf8(filename)}']"].attributes['caption']
363 return cap.gsub("\n", '<br/>')
369 def find_captions(xmldir, images)
370 return images.collect { |img| find_caption_value(xmldir, img) }
373 #- stolen from CVSspam
375 text.sub(/[^a-zA-Z0-9\-,.*_\/]/) do
376 "%#{sprintf('%2X', $&[0])}"
381 return $limit_sizes =~ /original/ ? $images_size + [ { 'name' => 'original' } ] : $images_size
384 def html_reload_to_thumbnails
385 html_reload_to_thumbnails = $preferred_size_reloader.clone
386 html_reload_to_thumbnails.gsub!(/~~theme~~/, $theme)
387 html_reload_to_thumbnails.gsub!(/~~default_size~~/, $default_size['name'])
388 html_reload_to_thumbnails.gsub!(/~~all_sizes~~/, all_images_sizes.collect { |s| "\"#{size2js(s['name'])}\"" }.join(', '))
389 return html_reload_to_thumbnails
392 def discover_iterations(iterations, line)
393 if line =~ /~~iterate(\d)_open(_max(\d+|N))?~~/
394 for iter in iterations.values
397 iter['close_wait'] = $1.to_i
400 max = $3 == 'N' ? ($N_per_row || $default_N) : $3
401 iterations[$1.to_i] = { 'open' => true, 'max' => max, 'opening' => '', 'closing' => '' }
403 line.sub!(/.*/, '~~thumbnails~~')
407 elsif line =~ /~~iterate(\d)_close~~/
408 iterations[$1.to_i]['open'] = false;
409 iterations[$1.to_i]['close'] = true;
412 for iter in iterations.values
414 iter['opening'] += line
417 if !iter['close'] && iter['close_wait'] && iterations[iter['close_wait']]['close']
418 iter['closing'] += line
425 def reset_iterations(iterations)
426 for iter in iterations.values
431 def run_iterations(iterations, amount)
434 for level in iterations.keys.sort
435 if iterations[level]['value'] == 0
436 html += iterations[level]['opening']
437 elsif level == iterations.keys.max
438 if !iterations[level]['max'] || iterations[level]['max'] && iterations[level]['value'] + amount <= iterations[level]['max'].to_i
439 html += iterations[level]['opening']
444 iterations[level]['value'] += amount
445 if iterations[level]['max'] && iterations[level]['value'] > iterations[level]['max'].to_i
446 iterations[level]['value'] = 0
447 iterations[level-1]['value'] = 0
448 html += iterations[level-1]['closing']
452 return html + run_iterations(iterations, amount)
458 def close_iterations(iterations)
460 for level in iterations.keys.sort.reverse
461 html += iterations[level]['closing']
466 def img_element(fullpath)
467 if size = get_image_size(fullpath)
468 sizespec = 'width="' + size[:x].to_s + '" height="' + size[:y].to_s + '"'
472 return '<img src="' + File.basename(fullpath) + '" ' + sizespec + ' class="image"/>'
476 return name.gsub(/-/, '')
479 def substitute_html_sizes(html, sizeobj, type)
481 if $images_size.length > 1 || (type == 'image' && $limit_sizes =~ /original/)
482 for sizeobj2 in $images_size
483 if sizeobj != sizeobj2
484 if type == 'thumbnails'
485 sizestrings << '<a href="thumbnails-' + size2js(sizeobj2['name']) + '.html">' + sizename(sizeobj2['name']) + '</a>'
487 sizestrings << '<a id="link' + size2js(sizeobj2['name']) + '">' + sizename(sizeobj2['name']) + '</a>'
490 sizestrings << sizename(sizeobj2['name'])
493 if type == 'image' && $limit_sizes =~ /original/
494 sizestrings << '<a id="linkoriginal" target="newframe">' + sizename('original') + '</a>'
497 html.sub!(/~~sizes~~(.+)~~/) { sizestrings.join($1) }
500 def xmldir2destdir(xmldir)
501 return make_dest_filename(from_utf8(File.basename(xmldir.attributes['path'])))
504 def find_previous_album(xmldir)
507 #- move to previous dir element if exists
508 if prevelem = xmldir.previous_element_byname_notattr('dir', 'deleted')
510 relative_pos += '../' + xmldir2destdir(xmldir) + '/'
512 #- after having moved to previous dir, we need to go down last subdir until the last one
513 while child = xmldir.elements['dir']
514 while nextchild = child.next_element_byname_notattr('dir', 'deleted')
517 relative_pos += xmldir2destdir(child) + '/'
521 #- previous dir doesn't exist, move to previous dir element if exists
522 xmldir = xmldir.parent
523 if xmldir.name == 'dir' && !xmldir.attributes['deleted']
524 relative_pos += '../'
529 end while !xmldir.child_byname_notattr('image', 'deleted') && !xmldir.child_byname_notattr('video', 'deleted')
530 return File.reduce_path(relative_pos)
533 def find_next_album(xmldir)
536 #- first child dir element (catches when initial xmldir has both thumbnails and subdirs)
537 if firstchild = xmldir.child_byname_notattr('dir', 'deleted')
539 relative_pos += xmldir2destdir(xmldir) + '/'
541 elsif nextbro = xmldir.next_element_byname_notattr('dir', 'deleted')
543 relative_pos += '../' + xmldir2destdir(xmldir) + '/'
545 #- go up until we have a next brother or we are finished
547 xmldir = xmldir.parent
548 relative_pos += '../'
549 end while xmldir && !xmldir.next_element_byname_notattr('dir', 'deleted')
551 xmldir = xmldir.next_element_byname('dir')
552 relative_pos += '../' + xmldir2destdir(xmldir) + '/'
557 end while !xmldir.child_byname_notattr('image', 'deleted') && !xmldir.child_byname_notattr('video', 'deleted')
558 return File.reduce_path(relative_pos)
561 def sub_previous_next_album(previous_album, next_album, html)
563 html.gsub!(/~~previous_album~~/, '<a href="' + previous_album + 'thumbnails.html">' + utf8(_('previous album')) + '</a>')
564 html.gsub!(/~~ifprevious_album\?~~(.+?)~~fi~~/) { $1 }
566 html.gsub!(/~~previous_album~~/, '')
567 html.gsub!(/~~ifprevious_album\?~~(.+?)~~fi~~/, '')
570 html.gsub!(/~~next_album~~/, '<a href="' + next_album + 'thumbnails.html">' + utf8(_('next album')) + '</a>')
571 html.gsub!(/~~ifnext_album\?~~(.+?)~~fi~~/) { $1 }
573 html.gsub!(/~~next_album~~/, '')
574 html.gsub!(/~~ifnext_album\?~~(.+?)~~fi~~/, '')
581 #- preprocess the path->dir, rexml is very slow with that; we seem to improve speed by 7%
583 $xmldoc.elements.each('//dir') { |elem|
584 optxpath[elem.attributes['path']] = elem
588 if $mode == 'merge_config_onedir'
589 examined_dirs = [ $onedir ]
590 elsif $mode == 'merge_config_subdirs'
591 examined_dirs = `find '#{$onedir}' -type d -follow`.sort.collect { |v| v.chomp }.delete_if { |v| optxpath.has_key?(utf8(v)) }
593 examined_dirs = `find '#{$source}' -type d -follow`.sort
595 info("directories: #{examined_dirs.length}, sizes: #{$images_size.length}")
597 examined_dirs.each { |dir|
599 if File.basename(dir) =~ /^\./
600 msg 1, _("Ignoring directory %s, begins with a dot (indicating a hidden directory)") % dir
605 die _("Source directory or sub-directories can't contain a single-quote character, sorry: %s") % dir
608 #- place xml document on proper node if exists, else create
609 xmldir = optxpath[utf8(dir)]
610 if $mode == 'use_config' || $mode == 'use_config_changetheme'
611 if !xmldir || (xmldir.attributes['already-generated'] && !$force) || xmldir.attributes['deleted']
612 info("walking: #{dir}|#{$source}, 0 elements")
616 if $mode == 'gen_config' || (($mode == 'merge_config' || $mode == 'merge_config_subdirs') && !xmldir)
617 #- add the <dir..> element if necessary
618 parent = File.dirname(dir)
619 xmldir = $xmldoc.elements["//dir[@path='#{utf8(parent)}']"]
621 xmldir = $xmldoc.root
623 #- need to remove the already-generated mark of the parent because of the sub-albums page containing now one more element
624 xmldir.delete_attribute('already-generated')
625 xmldir = optxpath[utf8(dir)] = xmldir.add_element('dir', { 'path' => utf8(dir) })
628 xmldir.delete_attribute('already-generated')
630 #- read images/videos entries from config or from directories depending on mode
632 if $mode == 'use_config' || $mode == 'use_config_changetheme'
633 msg 2, _("Handling %s from config list...") % dir
634 xmldir.elements.each { |element|
635 if %w(image video).include?(element.name) && !element.attributes['deleted']
636 entries << from_utf8(element.attributes['filename'])
640 msg 2, _("Examining %s...") % dir
641 entries = Dir.entries(dir).sort
642 #- populate config in case of gen_config, add new files in case of merge_config
644 if file =~ /['"\[\]]/
645 msg 1, _("Ignoring %s, contains one of forbidden characters: '\"[]") % "#{dir}/#{file}"
647 type = entry2type(file)
648 if type && !xmldir.elements["#{type}[@filename='#{utf8(file)}']"]
649 xmldir.add_element type, { "filename" => utf8(file), "caption" => $empty_comments ? '' : utf8(file.sub(/\.[^\.]+$/, '')[0..17]) }
653 if $mode != 'gen_config'
654 #- cleanup removed files from config and reread entries from config to get proper ordering
656 xmldir.elements.each { |element|
657 fullpath = "#{dir}/#{from_utf8(element.attributes['filename'])}"
658 if %w(image video).include?(element.name)
659 if !File.readable?(fullpath)
660 msg 1, _("Config merge: removing %s from config; use the backup file to retrieve caption info if this was a mistake") % fullpath
661 xmldir.delete(element)
662 elsif !element.attributes['deleted']
663 entries << from_utf8(element.attributes['filename'])
667 #- if there is no more elements here, there is no album here anymore
668 if !xmldir.child_byname_notattr('image', 'deleted') && !xmldir.child_byname_notattr('video', 'deleted')
669 xmldir.delete_attribute('thumbnails-caption')
670 xmldir.delete_attribute('thumbnails-captionfile')
674 images = entries.find_all { |e| entry2type(e) == 'image' }
675 msg 3, _("\t%s images") % images.length
676 videos = entries.find_all { |e| entry2type(e) == 'video' }
677 msg 3, _("\t%s videos") % videos.length
678 info("walking: #{dir}|#{$source}, #{images.length + videos.length} elements")
680 dest_dir = make_dest_filename(dir.sub(/^#{Regexp.quote($source)}/, $dest))
681 system("mkdir -p '#{dest_dir}'")
683 #- pass through if there are no images and videos
684 if images.size == 0 && videos.size == 0
686 #- cleanup old images/videos, especially if this directory contained images/videos previously.
687 themestuff = Dir.entries("#{$FPATH}/themes/#{$theme}").
688 find_all { |e| !%w(. .. skeleton_image.html skeleton_thumbnails.html skeleton_index.html metadata CVS).include?(e) }
689 if $mode != 'gen_config'
691 if xmldir.attributes['thumbnails-caption']
692 rightful_images << 'thumbnails-thumbnail.jpg'
694 xmldir.elements.each('dir') { |child|
695 if child.attributes['deleted']
698 subdir = make_dest_filename(from_utf8(File.basename(child.attributes['path'])))
699 rightful_images << "thumbnails-#{subdir}.jpg"
701 to_del = Dir.entries(dest_dir).find_all { |e| !File.directory?("#{dest_dir}/#{e}") && !rightful_images.include?(e) } - themestuff
703 system("rm -f " + to_del.collect { |e| "#{dest_dir}/#{e}" }.join(' '))
707 #- copy any resource file that goes with the theme (css, images..)
708 themestuff.each { |entry|
709 if !File.exists?("#{dest_dir}/#{entry}")
710 psys("cp '#{$FPATH}/themes/#{$theme}/#{entry}' '#{dest_dir}'")
717 msg 2, _("Outputting in %s...") % dest_dir
719 #- populate data structure with sizes from theme
720 for sizeobj in $images_size
721 fullscreen_images ||= {}
722 fullscreen_images[sizeobj['name']] = []
723 thumbnail_images ||= {}
724 thumbnail_images[sizeobj['name']] = []
725 thumbnail_videos ||= {}
726 thumbnail_videos[sizeobj['name']] = []
728 #- a special dummy size to keep 'references' to thumbnails in case of panorama, because the GUI will use the regular thumbnails
729 thumbnail_images['dont-delete-file-for-gui'] = []
730 if $limit_sizes =~ /original/
731 fullscreen_images['original'] = []
734 images.size >= 1 and msg 3, _("\tcreating images thumbnails...")
736 #- create thumbnails for images
738 info("processing element")
739 base_dest_img = dest_dir + '/' + make_dest_filename(img.sub(/\.[^\.]+$/, ''))
741 thumbnail_dest_img = base_dest_img + "-#{$default_size['thumbnails']}.jpg"
742 gen_thumbnails_element("#{dir}/#{img}", xmldir, true, [ { 'filename' => thumbnail_dest_img, 'size' => $default_size['thumbnails'] } ])
745 elem = xmldir.elements["image[@filename='#{utf8(img)}']"]
746 for sizeobj in $images_size
747 size_fullscreen = sizeobj['fullscreen']
748 size_thumbnails = sizeobj['thumbnails']
749 fullscreen_dest_img = base_dest_img + "-#{size_fullscreen}.jpg"
750 fullscreen_images[sizeobj['name']] << File.basename(fullscreen_dest_img)
751 todo << { 'filename' => fullscreen_dest_img, 'size' => size_fullscreen }
752 if pano = pano_amount(elem)
753 thumbnail_images['dont-delete-file-for-gui'] << File.basename(base_dest_img + "-#{size_thumbnails}.jpg")
754 size_thumbnails = size_thumbnails.sub(/(\d+)/) { ($1.to_i * pano).to_i }
756 thumbnail_dest_img = base_dest_img + "-#{size_thumbnails}.jpg"
757 thumbnail_images[sizeobj['name']] << File.basename(thumbnail_dest_img)
758 todo << { 'filename' => thumbnail_dest_img, 'size' => size_thumbnails }
760 gen_thumbnails_element("#{dir}/#{img}", xmldir, true, todo)
761 if $limit_sizes =~ /original/
762 fullscreen_images['original'] << img
764 destimg = "#{dest_dir}/#{img}"
765 if $limit_sizes =~ /original/ && !File.exists?(destimg)
767 if ! sys("ln '#{dir}/#{img}' '#{destimg}'")
768 $hardlinks_ok = false
772 psys("cp '#{dir}/#{img}' '#{destimg}'")
778 videos.size >= 1 and msg 3, _("\tcreating videos thumbnails...")
780 #- create thumbnails for videos
781 videos.each { |video|
782 info("processing element")
784 thumbnail_dest_img = dest_dir + '/' + make_dest_filename(video.sub(/\.[^\.]+$/, '')) + "-#{$default_size['thumbnails']}.jpg"
785 gen_thumbnails_element("#{dir}/#{video}", xmldir, true, [ { 'filename' => thumbnail_dest_img, 'size' => $default_size['thumbnails'] } ])
788 for sizeobj in $images_size
789 size_thumbnails = sizeobj['thumbnails']
790 thumbnail_dest_img = dest_dir + '/' + make_dest_filename(video.sub(/\.[^\.]+$/, '')) + "-#{size_thumbnails}.jpg"
791 thumbnail_videos[sizeobj['name']] << File.basename(thumbnail_dest_img)
792 todo << { 'filename' => thumbnail_dest_img, 'size' => size_thumbnails }
794 gen_thumbnails_element("#{dir}/#{video}", xmldir, true, todo)
796 destvideo = "#{dest_dir}/#{video}"
797 if !File.exists?(destvideo)
798 psys("cp '#{dir}/#{video}' '#{destvideo}'")
803 themestuff = Dir.entries("#{$FPATH}/themes/#{$theme}").
804 find_all { |e| !%w(. .. skeleton_image.html skeleton_thumbnails.html skeleton_index.html metadata CVS).include?(e) }
806 #- cleanup old images/videos (for when removing elements or sizes)
807 all_elements = fullscreen_images.collect { |e| e[1] }.flatten.
808 concat(thumbnail_images.collect { |e| e[1] }.flatten).
809 concat(thumbnail_videos.collect { |e| e[1] }.flatten).
811 to_del = Dir.entries(dest_dir).find_all { |e| !File.directory?("#{dest_dir}/#{e}") && !all_elements.include?(e) && e !~ /^thumbnails-\w+\.jpg/ } - themestuff
813 msg 3, _("\tcleaning up: #{to_del.join(', ')}")
814 system("rm -f " + to_del.collect { |e| "#{dest_dir}/#{e}" }.join(' '))
817 #- copy any resource file that goes with the theme (css, images..)
818 themestuff.each { |entry|
819 if !File.exists?("#{dest_dir}/#{entry}")
820 psys("cp '#{$FPATH}/themes/#{$theme}/#{entry}' '#{dest_dir}'")
824 #- generate .htaccess file if needed
825 if auth_user_file = xmldir.attributes['password-protect']
826 msg 3, _("\tgenerating password protection file #{dest_dir}/.htaccess")
827 ios = File.open("#{dest_dir}/.htaccess", "w")
828 ios.write("AuthType Basic\nAuthName \"protected area\"\nAuthUserFile #{auth_user_file}\nrequire valid-user\n")
832 msg 3, _("\tgenerating HTML pages...")
834 #- generate thumbnails.html (page with thumbnails)
835 for sizeobj in $images_size
836 info("processing size")
837 html = $html_thumbnails.collect { |l| l.clone }
840 i.sub!(/~~run_slideshow~~/, images.size <= 1 ? '' : '<a href="image-' + size2js(sizeobj['name']) + '.html#run_slideshow=1">' + utf8(_('Run slideshow!'))+'</a>')
841 i.sub!(/~~title~~/, xmldir.attributes['thumbnails-caption'] || utf8(File.basename(dir)))
842 substitute_html_sizes(i, sizeobj, 'thumbnails')
843 discover_iterations(iterations, i)
846 reset_iterations(iterations)
848 type = images.include?(file) ? 'image' : videos.include?(file) ? 'video' : nil
850 if type == 'image' && elem = xmldir.elements["image[@filename='#{utf8(file)}']"]
851 if pano = pano_amount(elem)
852 html_thumbnails += run_iterations(iterations, pano)
853 html_thumbnails.gsub!(/~~colspan~~/) { 'colspan="' + pano.ceil.to_s + '"' }
855 html_thumbnails += run_iterations(iterations, 1)
856 html_thumbnails.gsub!(/~~colspan~~/, '')
859 html_thumbnails += run_iterations(iterations, 1)
860 html_thumbnails.gsub!(/~~colspan~~/, '')
863 index = images.index(file)
864 html_thumbnails.gsub!(/~~image_iteration~~/,
865 '<a href="image-' + size2js(sizeobj['name']) + '.html#current=' + fullscreen_images[sizeobj['name']][index] +
866 '" name="' + fullscreen_images[sizeobj['name']][index] + '">' +
867 img_element("#{dest_dir}/#{thumbnail_images[sizeobj['name']][index]}") + '</a>')
868 html_thumbnails.gsub!(/~~caption_iteration~~/,
869 find_caption_value(xmldir, images[index]) || utf8(images[index]))
870 html_thumbnails.gsub!(/~~ifimage\?~~(.+?)~~fi~~/) { $1 }
871 html_thumbnails.gsub!(/~~ifvideo\?~~(.+?)~~fi~~/, '')
872 elsif type == 'video'
873 index = videos.index(file)
874 if File.exists?("#{dest_dir}/#{thumbnail_videos[sizeobj['name']][index]}")
875 html_thumbnails.gsub!(/~~image_iteration~~/,
876 '<a href="' + videos[index] + '">' + img_element("#{dest_dir}/#{thumbnail_videos[sizeobj['name']][index]}") + '</a>')
878 html_thumbnails.gsub!(/~~image_iteration~~/,
879 '<a href="' + videos[index] + '">' + utf8(_("(no preview)")) + '</a>')
881 html_thumbnails.gsub!(/~~caption_iteration~~/,
882 find_caption_value(xmldir, videos[index]) || utf8(videos[index]))
883 html_thumbnails.gsub!(/~~ifimage\?~~(.+?)~~fi~~/, '')
884 html_thumbnails.gsub!(/~~ifvideo\?~~(.+?)~~fi~~/) { $1 }
888 html_thumbnails += close_iterations(iterations)
890 i.sub!(/~~thumbnails~~/, html_thumbnails)
891 i.gsub!(/~~theme~~/, $theme)
892 i.gsub!(/~~current_size~~/, sizeobj['name'])
893 i.gsub!(/~~current_size_js~~/, size2js(sizeobj['name']))
894 i.gsub!(/~~madewith~~/, $madewith || '')
896 ios = File.open("#{dest_dir}/thumbnails-#{size2js(sizeobj['name'])}.html", "w")
901 info("finished processing sizes")
903 #- generate "main" thumbnails.html page that will reload to correct size thanks to cookie
904 ios = File.open("#{dest_dir}/thumbnails.html", "w")
905 ios.write(html_reload_to_thumbnails)
908 #- generate image.html (page with fullscreen images)
910 captions4js = find_captions(xmldir, images).collect { |e| e ? '"' + e.gsub('"', '\"') + '"' : '""' }.join(', ')
912 for sizeobj in $images_size
913 html = $html_images.collect { |l| l.clone }
914 images4js = fullscreen_images[sizeobj['name']].collect { |e| "\"#{e}\"" }.join(', ')
917 for sizeobj2 in all_images_sizes
918 if sizeobj != sizeobj2
919 otherimages4js += "var images_#{size2js(sizeobj2['name'])} = new Array(" + fullscreen_images[sizeobj2['name']].collect { |e| "\"#{e}\"" }.join(', ') + ")\n"
920 othersizes << "\"#{size2js(sizeobj2['name'])}\""
924 i.gsub!(/~~images~~/, images4js)
925 i.gsub!(/~~other_images~~/, otherimages4js)
926 i.gsub!(/~~other_sizes~~/, othersizes.join(', '))
927 i.gsub!(/~~captions~~/, captions4js)
928 i.gsub!(/~~title~~/, xmldir.attributes['thumbnails-caption'] || utf8(File.basename(dir)))
929 i.gsub!(/~~thumbnails~~/, '<a href="thumbnails-' + size2js(sizeobj['name']) + '.html" id="thumbnails">' + utf8(_('return to thumbnails')) + '</a>')
930 i.gsub!(/~~theme~~/, $theme)
931 i.gsub!(/~~current_size~~/, size2js(sizeobj['name']))
932 i.gsub!(/~~madewith~~/, $madewith || '')
933 substitute_html_sizes(i, sizeobj, 'image')
935 ios = File.open("#{dest_dir}/image-#{size2js(sizeobj['name'])}.html", "w")
945 #- add attributes to <dir..> elements needing so
946 if $mode != 'use_config'
947 msg 3, _("\tfixating configuration file...")
948 $xmldoc.elements.each('//dir') { |element|
949 path = captionpath = element.attributes['path']
950 descendant_element = element.elements['descendant::image'] || element.elements['descendant::video']
951 if !descendant_element
952 msg 3, _("\t\tremoving %s, no element in it") % path
953 element.remove #- means we have a directory with nothing interesting in it
955 captionfile = "#{descendant_element.parent.attributes['path']}/#{descendant_element.attributes['filename']}"
956 basename = File.basename(path)
957 if element.elements['dir']
958 if !element.attributes['subdirs-caption']
959 element.add_attribute('subdirs-caption', basename)
961 if !element.attributes['subdirs-captionfile']
962 element.add_attribute('subdirs-captionfile', captionfile)
965 if element.child_byname_notattr('image', 'deleted') || element.child_byname_notattr('video', 'deleted')
966 if !element.attributes['thumbnails-caption']
967 element.add_attribute('thumbnails-caption', basename)
969 if !element.attributes['thumbnails-captionfile']
970 element.add_attribute('thumbnails-captionfile', captionfile)
977 #- write down to disk config if necessary
979 ios = File.open($config_writeto, "w")
980 $xmldoc.write(ios, 0)
985 msg 3, _(" completed necessary stuff for GUI, exiting.")
989 #- second pass to create index.html files and previous/next links
990 info("creating index.html")
991 msg 3, _("\trescanning directories to generate all `index.html' files...")
993 #- recompute the memoization because elements mights have been removed (the ones with no element in them)
995 $xmldoc.elements.each('//dir') { |elem|
996 optxpath[elem.attributes['path']] = elem
999 examined_dirs.each { |dir|
1001 info("index.html: #{dir}|#{$source}")
1003 xmldir = optxpath[utf8(dir)]
1004 if !xmldir || (xmldir.attributes['already-generated'] && !$force) || xmldir.attributes['deleted']
1007 dest_dir = make_dest_filename(dir.sub(/^#{Regexp.quote($source)}/, $dest))
1009 previous_album = find_previous_album(xmldir)
1010 next_album = find_next_album(xmldir)
1012 if xmldir.elements['dir']
1013 html = $html_index.collect { |l| l.clone }
1016 caption = xmldir.attributes['subdirs-caption']
1017 i.gsub!(/~~title~~/, caption)
1018 if xmldir.parent.name == 'dir'
1021 parent = xmldir.parent
1022 while parent.name == 'dir'
1023 parentcaption = parent.attributes['subdirs-caption']
1024 nav = "<a href='#{path}/index.html'>#{parentcaption}</a> #{utf8(_(" > "))} #{nav}"
1026 parent = parent.parent
1028 i.gsub!(/~~ifnavigation\?~~(.+?)~~fi~~/) { $1 }
1029 i.gsub!(/~~navigation~~/, nav + caption)
1031 i.gsub!(/~~ifnavigation\?~~(.+?)~~fi~~/, '')
1033 discover_iterations(iterations, i)
1037 reset_iterations(iterations)
1039 #- deal with "current" album (directs to "thumbnails" page)
1040 if xmldir.attributes['thumbnails-caption']
1041 thumbnail = "#{dest_dir}/thumbnails-thumbnail.jpg"
1042 gen_thumbnails_subdir(from_utf8(xmldir.attributes['thumbnails-captionfile']), xmldir, false,
1043 [ { 'filename' => thumbnail, 'size' => $albums_thumbnail_size } ], 'thumbnails')
1044 html_index += run_iterations(iterations, 1)
1045 html_index.gsub!(/~~image_iteration~~/, "<a href='thumbnails.html'>" + img_element(thumbnail) + '</a>')
1046 html_index.gsub!(/~~caption_iteration~~/, xmldir.attributes['thumbnails-caption'])
1049 #- deal with sub-albums (direct to subdirs/index.html pages)
1050 xmldir.elements.each('dir') { |child|
1051 if child.attributes['deleted']
1054 subdir = make_dest_filename(from_utf8(File.basename(child.attributes['path'])))
1055 thumbnail = "#{dest_dir}/thumbnails-#{subdir}.jpg"
1056 html_index += run_iterations(iterations, 1)
1057 captionfile, caption = find_subalbum_caption_info(child)
1058 gen_thumbnails_subdir(captionfile, child, false,
1059 [ { 'filename' => thumbnail, 'size' => $albums_thumbnail_size } ], find_subalbum_info_type(child))
1060 html_index.gsub!(/~~caption_iteration~~/, caption)
1061 html_index.gsub!(/~~image_iteration~~/, "<a href='#{make_dest_filename(subdir)}/index.html'>" + img_element(thumbnail) + '</a>')
1064 html_index += close_iterations(iterations)
1067 i.gsub!(/~~thumbnails~~/, html_index)
1068 i.gsub!(/~~madewith~~/, $madewith || '')
1072 html = html_reload_to_thumbnails
1075 ios = File.open("#{dest_dir}/index.html", "w")
1079 #- substitute "return to albums" and previous/next correctly
1080 if xmldir.child_byname_notattr('image', 'deleted') || xmldir.child_byname_notattr('video', 'deleted')
1081 for sizeobj in $images_size
1082 substInFile("#{dest_dir}/thumbnails-#{size2js(sizeobj['name'])}.html") { |line|
1083 sub_previous_next_album(previous_album, next_album, line)
1084 if xmldir.elements['dir']
1085 line.sub!(/~~return_to_albums~~/, '<a href="index.html">' + utf8(_('return to albums')) + '</a>')
1087 if xmldir.parent.name == 'dir'
1088 line.sub!(/~~return_to_albums~~/, '<a href="../index.html">' + utf8(_('return to albums')) + '</a>')
1090 line.sub!(/~~return_to_albums~~/, '')
1095 if xmldir.child_byname_notattr('image', 'deleted')
1096 substInFile("#{dest_dir}/image-#{size2js(sizeobj['name'])}.html") { |line|
1097 sub_previous_next_album(previous_album, next_album, line)
1104 msg 3, _(" all done.")
1111 build_html_skeletons