no longer use internalized rexml
[booh] / bin / booh-backend
old mode 100755 (executable)
new mode 100644 (file)
index f4a2b52..12de9d8
 # called Boo, so this one will be it "Booh". Or whatever.
 #
 #
-# Copyright (c) 2005 Guillaume Cottenceau <gc3 at bluewin.ch>
+# Copyright (c) 2004-2013 Guillaume Cottenceau <http://zarb.org/~gc/resource/gc_mail.png>
 #
 # This software may be freely redistributed under the terms of the GNU
 # public license version 2.
 #
 # You should have received a copy of the GNU General Public License
 # along with this program; if not, write to the Free Software
-# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+begin
+    require 'rubygems'
+rescue LoadError
+end
 
 require 'getoptlong'
+begin
+    #- new style (ruby-locale separated package)
+    require 'locale'
+rescue LoadError
+    #- old style (locale stuff in ruby-gettext package)
+    require 'gettext/locale'
+end
 require 'gettext'
 include GetText
 require 'rexml/document'
@@ -30,31 +42,38 @@ require 'booh/html-merges'
 
 #- bind text domain as soon as possible because some _() functions are called early to build data structures
 bindtextdomain("booh")
+#- save locale for restoring for multi languages
+$default_locale = Locale.get
 
 #- options
 $options = [
-    [ '--help',          '-h', GetoptLong::NO_ARGUMENT,       _("Get help message") ],
-    [ '--version',       '-V', GetoptLong::NO_ARGUMENT,       _("Print version and exit") ],
+    [ '--help',          '-h', GetoptLong::NO_ARGUMENT, _("Get help message") ],
+    [ '--version',       '-V', GetoptLong::NO_ARGUMENT, _("Print version and exit") ],
 
-    [ '--source',        '-s', GetoptLong::REQUIRED_ARGUMENT, _("Directory which contains original images/videos as files or subdirs") ],
-    [ '--destination',   '-d', GetoptLong::REQUIRED_ARGUMENT, _("Directory which will contain the web-album") ],
-#    [ '--clean',         '-c', GetoptLong::NO_ARGUMENT,       _("Clean destination directory") ],
+    [ '--source',        '-s', GetoptLong::REQUIRED_ARGUMENT, _("Directory which contains original photos/videos as files or subdirs") ],
+    [ '--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!") ],
 
     [ '--theme',         '-t', GetoptLong::REQUIRED_ARGUMENT, _("Select HTML theme to use") ],
-    [ '--config',        '-C', GetoptLong::REQUIRED_ARGUMENT, _("File containing config listing images and videos within directories with captions") ],
+    [ '--config',        '-C', GetoptLong::REQUIRED_ARGUMENT, _("File containing config listing photos and videos within directories with captions") ],
     [ '--config-skel',   '-k', GetoptLong::REQUIRED_ARGUMENT, _("Filename where the script will output a config skeleton") ],
-    [ '--merge-config',  '-M', GetoptLong::REQUIRED_ARGUMENT, _("File containing config listing, where to merge new/removed images/videos from --source, and change theme info") ],
+    [ '--merge-config',  '-M', GetoptLong::REQUIRED_ARGUMENT, _("File containing config listing, where to merge new/removed photos/videos from --source, and change theme info") ],
     [ '--merge-config-onedir',  '-O', GetoptLong::REQUIRED_ARGUMENT, _("File containing config listing, for merging the subdir specified with --dir") ],
     [ '--merge-config-subdirs', '-U', GetoptLong::REQUIRED_ARGUMENT, _("File containing config listing, for merging the new subdirs down the subdir specified with --dir") ],
     [ '--dir',           '-D', GetoptLong::REQUIRED_ARGUMENT, _("Directory for merge with --merge-config-onedir or --merge-config-subdirs") ],
     [ '--use-config',    '-u', GetoptLong::REQUIRED_ARGUMENT, _("File containing config listing, where to change theme info") ],
-    [ '--force',         '-f', GetoptLong::NO_ARGUMENT,       _("Force generation of album even if the GUI marked some directories as already generated") ],
+    [ '--force',         '-f', GetoptLong::NO_ARGUMENT, _("Force generation of album even if the GUI marked some directories as already generated") ],
 
     [ '--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)") ],
+    [ '--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(', ') ],
     [ '--thumbnails-per-row', '-T', GetoptLong::REQUIRED_ARGUMENT, _("Specify the amount of thumbnails per row in the thumbnails page (if applicable in theme)") ],
-    [ '--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)") ],
+    [ '--thumbnails-per-page', '-p', GetoptLong::REQUIRED_ARGUMENT, _("Specify the amount of thumbnails per page in the thumbnails page, after which split occurs") ],
+    [ '--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)") ],
+    [ '--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") ],
+    [ '--mp4-generator', '-F', GetoptLong::REQUIRED_ARGUMENT, _("Use this .mp4 generator (%f is the placeholder for the input video, %o for the output video)") ],
     [ '--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)") ],
+    [ '--addthis', '-a', GetoptLong::NO_ARGUMENT, _("Include the 'addthis' bookmarking and sharing button") ],
     [ '--made-with',     '-n', GetoptLong::REQUIRED_ARGUMENT, _("Specify the HTML markup to use on the bottom of pages for a small 'made with' message") ],
+    [ '--quote-html',    '-q', GetoptLong::NO_ARGUMENT, _("Quote HTML markup in captions") ],
     [ '--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") ],
 
     [ '--mproc',         '-m', GetoptLong::REQUIRED_ARGUMENT, _("Specify the number of processors for multi-processors machines") ],
@@ -93,7 +112,7 @@ def handle_options
             when '--version'
                 puts _("Booh version %s
 
-Copyright (c) 2005 Guillaume Cottenceau.
+Copyright (c) 2005-2013 Guillaume Cottenceau.
 This is free software; see the source for copying conditions.  There is NO
 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.") % $VERSION
 
@@ -192,18 +211,47 @@ warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.") %
             when '--sizes'
                 $limit_sizes = arg
 
+            when '--multi-languages'
+                parts = arg.split(',')
+                if parts.size == 0 || parts.find_all { |e| ! SUPPORTED_LANGUAGES.include?(e.strip) }.size > 0
+                    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(', ')
+                end
+                $multi_languages = [ parts[0..-2], parts[-1] ]
+
             when '--thumbnails-per-row'
                 $N_per_row = arg
 
+            when '--thumbnails-per-page'
+                $N_per_page = arg
+
             when '--optimize-for-32'
                 $optimize_for_32 = true
 
+            when '--transcode-videos'
+                parts = arg.split(':', 2)
+                if parts.size != 2 || parts[0] =~ / / || arg !~ /%f/ || arg !~ /%o/
+                    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")
+                end
+                $transcode_videos = arg
+
+            when '--mp4-generator'
+                if arg !~ /%f/ || arg !~ /%o/
+                    die _("--mp4-generator: argument must contain %f for the input video and %o for the output video")
+                end
+                $mp4_generator = arg
+
             when '--made-with'
                 $madewith = arg
 
             when '--index-link'
                 $indexlink = arg
 
+            when '--addthis'
+                $addthis = true
+
+            when '--quote-html'
+                $quote_html = true
+
             when '--comments-format'
                 $commentsformat = arg
 
@@ -232,15 +280,23 @@ warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.") %
     end
 
     if !$source && $xmldoc
-        $source = from_utf8($xmldoc.root.attributes['source'])
-        $dest = from_utf8($xmldoc.root.attributes['destination'])
+        $source = from_utf8($xmldoc.root.attributes['source']).sub(%r|/$|, '')
+        $dest = from_utf8($xmldoc.root.attributes['destination']).sub(%r|/$|, '')
         $theme ||= $xmldoc.root.attributes['theme']
         $limit_sizes ||= $xmldoc.root.attributes['limit-sizes']
         if $mode == 'use_config' || $mode =~ /^merge_config/
             $optimize_for_32 = !$xmldoc.root.attributes['optimize-for-32'].nil?
             $N_per_row = $xmldoc.root.attributes['thumbnails-per-row']
+            languages = $xmldoc.root.attributes['multi-languages']
+            if languages
+                languages = languages.split(',')
+                $multi_languages = [ languages[0..-2], languages[-1] ]
+            end
+            $N_per_page = $xmldoc.root.attributes['thumbnails-per-page']
             $madewith = $xmldoc.root.attributes['made-with']
             $indexlink = $xmldoc.root.attributes['index-link']
+            $addthis = !$xmldoc.root.attributes['addthis'].nil?
+            $quote_html = !$xmldoc.root.attributes['quote-html'].nil?
         end
     end
 
@@ -259,30 +315,45 @@ warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.") %
         die _("Missing --destination parameter.")
     end
     if !$theme
-        $theme = 'simple'
+        $theme = 'gradient'
     end
 
     select_theme($theme, $limit_sizes, $optimize_for_32, $N_per_row)
 
     if !$xmldoc
-        additional_params = ''
+        $xmldoc = Document.new "<booh/>"
+        $xmldoc << XMLDecl.new(XMLDecl::DEFAULT_VERSION, $CURRENT_CHARSET)
+        $xmldoc.root.add_attribute('version', $VERSION)
+        $xmldoc.root.add_attribute('source', $source)
+        $xmldoc.root.add_attribute('destination', $dest)
+        $xmldoc.root.add_attribute('theme', $theme)
         if $limit_sizes
-            additional_params += "limit-sizes='#{$limit_sizes}'"
+            $xmldoc.root.add_attribute('limit-sizes', $limit_sizes)
+        end
+        if $multi_languages
+            $xmldoc.root.add_attribute('multi-languages', $multi_languages[0].join(',') + ',' + $multi_languages[1])
         end
         if $optimize_for_32
-            additional_params += " optimize-for-32='true'"
+            $xmldoc.root.add_attribute('optimize-for-32', 'true')
         end
         if $N_per_row
-            additional_params += " thumbnails-per-row='#{$N_per_row}'"
+            $xmldoc.root.add_attribute('thumbnails-per-row', $N_per_row)
+        end
+        if $N_per_page
+            $xmldoc.root.add_attribute('thumbnails-per-page', $N_per_page)
         end
         if $madewith
-            additional_params += " made-with='#{$madewith}'"
+            $xmldoc.root.add_attribute('made-with', $madewith)
         end
         if $indexlink
-            additional_params += " index-link='#{$indexlink}'"
+            $xmldoc.root.add_attribute('index-link', $indexlink)
+        end
+        if $addthis
+            $xmldoc.root.add_attribute('addthis', 'true')
+        end
+        if $quote_html
+            $xmldoc.root.add_attribute('quote-html', 'true')
         end
-        $xmldoc = Document.new "<booh version='#{$VERSION}' source='#{utf8($source)}' destination='#{utf8($dest)}' theme='#{$theme}' #{additional_params}/>"
-        $xmldoc << XMLDecl.new(XMLDecl::DEFAULT_VERSION, $CURRENT_CHARSET)
         $mode = 'gen_config'
     end
 
@@ -294,6 +365,12 @@ warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.") %
         else
             $xmldoc.root.delete_attribute('limit-sizes')
         end
+        if $multi_languages
+            $xmldoc.root.add_attribute('multi-languages', $multi_languages[0].join(',') + ',' + $multi_languages[1])
+        else
+            $xmldoc.root.delete_attribute('multi-languages')
+        end
+
         if $optimize_for_32
             $xmldoc.root.add_attribute('optimize-for-32', 'true')
         else
@@ -304,6 +381,11 @@ warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.") %
         else
             $xmldoc.root.delete_attribute('thumbnails-per-row')
         end
+        if $N_per_page
+            $xmldoc.root.add_attribute('thumbnails-per-page', $N_per_page)
+        else
+            $xmldoc.root.delete_attribute('thumbnails-per-page')
+        end
         if $madewith
             $xmldoc.root.add_attribute('made-with', $madewith)
         else
@@ -314,10 +396,37 @@ warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.") %
         else
             $xmldoc.root.delete_attribute('index-link')
         end
+        if $addthis
+            $xmldoc.root.add_attribute('addthis', 'true')
+        else
+            $xmldoc.root.delete_attribute('addthis')
+        end
+        if $quote_html
+            $xmldoc.root.add_attribute('quote-html', 'true')
+        else
+            $xmldoc.root.delete_attribute('quote-html')
+        end
+    end
+
+    if $transcode_videos
+        $xmldoc.root.add_attribute('transcode-videos', $transcode_videos)
+    else
+        $xmldoc.root.delete_attribute('transcode-videos')
+    end
+
+    if $mp4_generator
+        $xmldoc.root.add_attribute('mp4-generator', $mp4_generator)
+    else
+        $xmldoc.root.delete_attribute('mp4-generator')
     end
 
     if $madewith
-        $madewith = $madewith.gsub('%booh', '"http://zarb.org/~gc/html/booh.html"')
+        $madewith = $madewith.gsub('%booh', '"http://booh.org/"')
+    end
+    if $multi_languages
+        $htmlsuffix = ''
+    else
+        $htmlsuffix = '.html'
     end
 end
 
@@ -351,7 +460,7 @@ It is generally available with the 'ImageMagick' software package.")
 It is generally available with the 'ImageMagick' software package.")
         $no_identify = true
     end
-    missing = %w(transcode mencoder).delete_if { |prg| system("which #{prg} >/dev/null 2>/dev/null") }
+    missing = %w(mplayer).delete_if { |prg| system("which #{prg} >/dev/null 2>/dev/null") }
     if missing != []
         msg 1, _("the following program(s) are needed to handle videos: '%s'. Videos will be ignored.") % missing.join(', ')
         $ignore_videos = true
@@ -368,9 +477,9 @@ def replace_line(surround, keyword, line)
 end
 
 def build_html_skeletons
-    $html_images     = File.open("#{$FPATH}/themes/#{$theme}/skeleton_image.html").readlines
-    $html_thumbnails = File.open("#{$FPATH}/themes/#{$theme}/skeleton_thumbnails.html").readlines
-    $html_index      = File.open("#{$FPATH}/themes/#{$theme}/skeleton_index.html").readlines
+    $html_images     = File.open("#{$themedir}/skeleton_image.html").readlines
+    $html_thumbnails = File.open("#{$themedir}/skeleton_thumbnails.html").readlines
+    $html_index      = File.open("#{$themedir}/skeleton_index.html").readlines
     for line in $html_images + $html_thumbnails + $html_index
         while line =~ /~~~(\w+)~~~/
             replace_line('~~~', $1, line)
@@ -380,14 +489,20 @@ end
 
 def find_caption_value(xmldir, filename)
     if cap = xmldir.elements["*[@filename='#{utf8(filename)}']"].attributes['caption']
-        return cap.gsub("\n", '<br/>')
+        return cap
     else
         return nil
     end
 end
 
-def find_captions(xmldir, images)
-    return images.collect { |img| find_caption_value(xmldir, img) }
+def quote_caption(input, *for_attribute)
+    if $quote_html
+        return input.gsub('<', '&lt;').gsub('>', '&gt;').gsub('"', '&quot;').gsub("'", '&#39;').gsub("\n", '<br/>')
+    elsif for_attribute[0]
+        return input.gsub('"', '&quot;').gsub("\n", '<br/>')
+    else
+        return input.gsub("\n", '<br/>')
+    end
 end
 
 #- stolen from CVSspam
@@ -404,11 +519,13 @@ end
 def html_reload_to_thumbnails
     html_reload_to_thumbnails = $preferred_size_reloader.clone
     html_reload_to_thumbnails.gsub!(/~~theme~~/, $theme)
-    html_reload_to_thumbnails.gsub!(/~~default_size~~/, $default_size['name'])
+    html_reload_to_thumbnails.gsub!(/~~default_size~~/, size2js($default_size['name']))
+    html_reload_to_thumbnails.gsub!(/~~htmlsuffix~~/, $htmlsuffix)
     html_reload_to_thumbnails.gsub!(/~~all_sizes~~/, all_images_sizes.collect { |s| "\"#{size2js(s['name'])}\"" }.join(', '))
     size_auto_chooser = '';
-    all_images_sizes.sort { |a,b| b['optimizedforwidth'].to_i <=> a['optimizedforwidth'].to_i }.
-                     each { |s| size_auto_chooser += "if (w + 50 - #{s['optimizedforwidth']} > 0) { return 'thumbnails-#{size2js(s['name'])}.html'; }\n" }
+    all_images_sizes.find_all { |s| s.has_key?('optimizedforwidth') }.
+                     sort { |a,b| b['optimizedforwidth'].to_i <=> a['optimizedforwidth'].to_i }.
+                     each { |s| size_auto_chooser += "if (w + 50 > #{s['optimizedforwidth']}) { return 'thumbnails-#{size2js(s['name'])}-0#{$htmlsuffix}'; }\n" }
     html_reload_to_thumbnails.gsub!(/~~size_auto_chooser~~/, size_auto_chooser)
     return html_reload_to_thumbnails
 end
@@ -487,40 +604,78 @@ def close_iterations(iterations)
     return html
 end
 
-def img_element(fullpath)
+def img_element(fullpath, caption)
     if size = get_image_size(fullpath)
         sizespec = 'width="' + size[:x].to_s + '" height="' + size[:y].to_s + '"'
     else
         sizespec = ''
     end
-    return '<img src="' + File.basename(fullpath) + '" ' + sizespec + ' alt="image"/>'
+    return '<img src="' + File.basename(fullpath) + '" ' + sizespec + ' alt="' + quote_caption(caption, true) + '"/>'
 end
 
 def size2js(name)
     return name.gsub(/-/, '')
 end
 
-def substitute_html_sizes(html, sizeobj, type)
+def substitute_html_sizes(html, sizeobj, type, suffix)
     sizestrings = []
     if $images_size.length > 1 || (type == 'image' && $limit_sizes =~ /original/)
         for sizeobj2 in $images_size
+            sizejs = size2js(sizeobj2['name'])
+            sizen = defer_translation(sizename(sizeobj2['name'], false))
             if sizeobj != sizeobj2
                 if type == 'thumbnails'
-                    sizestrings << '<a href="thumbnails-' + size2js(sizeobj2['name']) + '.html">' + sizename(sizeobj2['name']) + '</a>'
+                    sizestrings << '<a href="thumbnails-' + sizejs + suffix + $htmlsuffix + '" onclick="set_preferred_size(\'' + sizejs + '\')">' + sizen + '</a>'
                 else
-                    sizestrings << '<a id="link' + size2js(sizeobj2['name']) + '">' + sizename(sizeobj2['name']) + '</a>'
+                    sizestrings << '<a id="link' + sizejs + '" onclick="set_preferred_size(\'' + sizejs + '\')">' + sizen + '</a>'
                 end
             else
-                sizestrings << sizename(sizeobj2['name'])
+                sizestrings << sizen
             end
         end
         if type == 'image' && $limit_sizes =~ /original/
-            sizestrings << '<a id="linkoriginal" target="newframe">' + sizename('original') + '</a>'
+            sizestrings << '<a id="linkoriginal" target="newframe">' + defer_translation(sizename('original', false)) + '</a>'
         end
     end
     html.sub!(/~~sizes~~(.+)~~/) { sizestrings.join($1) }
 end
 
+def substitute_navigation(html, xmldir)
+    if xmldir.parent.name == 'dir'
+        nav = ''
+        navtable = ''
+        path = '..'
+        parent = xmldir.parent
+        while parent.name == 'dir'
+            parentcaption = parent.attributes['subdirs-caption'] || File.basename(parent.attributes['path'])
+            nav = "<a href=\"#{path}/index#{$htmlsuffix}\">#{parentcaption}</a> #{defer_translation(N_(" > "))} #{nav}"
+            navtable = "<td><a href=\"#{path}/index#{$htmlsuffix}\">#{parentcaption}</a></td>#{navtable}"
+            path += '/..'
+            parent = parent.parent
+        end
+        html.gsub!(/~~ifnavigation\?~~(.+?)~~fi~~/) { $1 }
+        html.gsub!(/~~navigation~~/, nav + (xmldir.attributes['subdirs-caption'] || File.basename(xmldir.attributes['path'])))
+        html.gsub!(/~~navigationtable~~/, '<table id="navigation"><tr>' + navtable + "<td>" +
+                                          (xmldir.attributes['subdirs-caption'] || File.basename(xmldir.attributes['path'])) + "</td></tr></table>")
+    else
+        html.gsub!(/~~ifnavigation\?~~(.+?)~~fi~~/, '')
+    end
+end
+
+def substitute_addthis(html)
+    html.gsub!(/~~addthis~~/, $addthis ? '<a class="addthis_button" href="http://www.addthis.com/bookmark.php?v=250"><img src="http://s7.addthis.com/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="' + defer_translation(N_("Bookmark and Share")) + '" style="border:0"/></a><script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js"></script>' : '')
+end
+
+def substitute_pathtobase(html, xmldir)
+    path = ''
+    location = xmldir
+    while location.parent.name == 'dir'
+        path = '../' + path
+        location = location.parent
+    end
+    html.gsub!(/~~pathtobase~~/, path)
+end
+
 def xmldir2destdir(xmldir)
     return make_dest_filename(from_utf8(File.basename(xmldir.attributes['path'])))
 end
@@ -582,16 +737,31 @@ def find_next_album(xmldir)
     return File.reduce_path(relative_pos)
 end
 
-def sub_previous_next_album(previous_album, next_album, html)
+def find_translation_for_file(file, msg)
+    if $multi_languages
+        if file =~ /\.(\w\w)\.html$/
+            bindtextdomain("booh", { :locale => "#{$1}.UTF-8" })
+            retval = _(msg)
+            Locale.set_current($default_locale)
+            return retval
+        else
+            die "Internal error: cannot find multi language suffix of file '#{file}'"
+        end
+    else
+        return utf8(_(msg))
+    end
+end
+
+def sub_previous_next_album(file, previous_album, next_album, html, previous_album_msg, next_album_msg)
     if previous_album
-        html.gsub!(/~~previous_album~~/, '<a href="' + previous_album + 'thumbnails.html">' + utf8(_('previous album')) + '</a>')
+        html.gsub!(/~~previous_album~~/, '<a href="' + previous_album + 'thumbnails' + $htmlsuffix + '">' + previous_album_msg + '</a>')
         html.gsub!(/~~ifprevious_album\?~~(.+?)~~fi~~/) { $1 }
     else
         html.gsub!(/~~previous_album~~/, '')
         html.gsub!(/~~ifprevious_album\?~~(.+?)~~fi~~/, '')
     end
     if next_album
-        html.gsub!(/~~next_album~~/, '<a href="' + next_album + 'thumbnails.html">' + utf8(_('next album')) + '</a>')
+        html.gsub!(/~~next_album~~/, '<a href="' + next_album + 'thumbnails' + $htmlsuffix + '">' + next_album_msg + '</a>')
         html.gsub!(/~~ifnext_album\?~~(.+?)~~fi~~/) { $1 }
     else
         html.gsub!(/~~next_album~~/, '')
@@ -600,6 +770,37 @@ def sub_previous_next_album(previous_album, next_album, html)
     return html
 end
 
+def save_html(html, base_filename)
+    if html.class == Array
+        html = html.join('')
+    end
+    if $multi_languages
+        for language in ($multi_languages[0] + [ $multi_languages[1] ]).uniq
+            bindtextdomain("booh", { :locale => "#{language}.UTF-8" })
+            ios = File.open("#{base_filename}.#{language}.html", "w")
+            ios.write(html.gsub(/@@(.*?)@@/) { _($1) })
+            ios.close
+            Locale.set_current($default_locale)
+        end
+    else
+        ios = File.open("#{base_filename}.html", "w")
+        ios.write(html.gsub(/@@(.*?)@@/) { utf8(_($1)) })
+        ios.close
+    end
+end
+
+def apply_hook(name, content, *params)
+    if $hooks.nil?
+        return content
+    end
+    hook = $hooks[name]
+    if hook.nil?
+        return content
+    else
+        return hook.call(content, *params)
+    end
+end
+
 def walk_source_dir
 
     #- preprocess the path->dir, rexml is very slow with that; we seem to improve speed by 7%
@@ -612,9 +813,9 @@ def walk_source_dir
     if $mode == 'merge_config_onedir'
         examined_dirs = [ $onedir ]
     elsif $mode == 'merge_config_subdirs'
-        examined_dirs = `find '#{$onedir}' -type d -follow`.sort.collect { |v| v.chomp }.delete_if { |v| optxpath.has_key?(utf8(v)) }
+        examined_dirs = `find '#{$onedir}' -type d -follow`.split("\n").sort.collect { |v| v.chomp }.delete_if { |v| optxpath.has_key?(utf8(v)) }
     else
-        examined_dirs = `find '#{$source}' -type d -follow`.sort.collect { |v| v.chomp }
+        examined_dirs = `find '#{$source}' -type d -follow`.split("\n").sort.collect { |v| v.chomp }
         if $mode == 'merge_config'
             $xmldoc.elements.each('//dir') { |elem|
                 if ! examined_dirs.include?(elem.attributes['path'])
@@ -672,6 +873,14 @@ def walk_source_dir
         end
         xmldir.delete_attribute('already-generated')
 
+        #- preprocess all the existing elements, rexml is slow with that
+        optelements = {}
+        xmldir.elements.each { |elem|
+            if filename = elem.attributes['filename']
+                optelements[elem.name + '|' + filename] = elem
+            end
+        }
+
         #- read images/videos entries from config or from directories depending on mode
         entries = []
         if $mode == 'use_config' || $mode == 'use_config_changetheme'
@@ -690,13 +899,14 @@ def walk_source_dir
                     msg 1, _("Ignoring %s, contains one of forbidden characters: '\"[]") % "#{dir}/#{file}"
                 else
                     type = entry2type(file)
-                    if type && !xmldir.elements["#{type}[@filename='#{utf8(file)}']"]
-                        if $commentsformat && type == 'image'
+                    if type && ! optelements[type + '|' + utf8(file)]
+                        #- hack: don't run identify (which is slow) if format only contains default %t
+                        if $commentsformat && type == 'image' && $commentsformat != '%t'
                             comment = utf8(`identify -format "#{$commentsformat}" '#{dir}/#{file}'`.chomp.sub(/\.$/, ''))
                         else
                             comment = utf8cut(file.sub(/\.[^\.]+$/, ''), 18)
                         end
-                        xmldir.add_element type, { "filename" => utf8(file), "caption" => comment }
+                        optelements[type + '|' + utf8(file)] = xmldir.add_element(type, { "filename" => utf8(file), "caption" => comment })
                     end
                 end
             end
@@ -722,21 +932,42 @@ def walk_source_dir
             end
         end
         images = entries.find_all { |e| entry2type(e) == 'image' }
-        msg 3, _("\t%s images") % images.length
+        msg 3, _("\t%s photos") % images.length
         videos = entries.find_all { |e| entry2type(e) == 'video' }
         msg 3, _("\t%s videos") % videos.length
         info("walking: #{dir}|#{$source}, #{images.length + videos.length} elements")
 
         system("mkdir -p '#{dest_dir}'")
 
+        #- generate .htaccess file
+        if !$forgui
+            ios = File.open("#{dest_dir}/.htaccess", "w")
+            ios.write("AddCharset UTF-8 .html\n")
+            ios.write("\n")
+            ios.write("<IfModule mod_expires.c>\n")
+            ios.write(" ExpiresActive On\n")
+            ios.write(" ExpiresDefault A1800\n")
+            ios.write("</IfModule>")
+            ios.write("\n")
+            if auth_user_file = xmldir.attributes['password-protect']
+                msg 3, _("\tgenerating password protection file #{dest_dir}/.htaccess")
+                ios.write("AuthType Basic\nAuthName \"protected area\"\nAuthUserFile #{auth_user_file}\nrequire valid-user\n")
+            end
+            if $multi_languages
+                ios.write("Options +Multiviews\n")
+                ios.write("LanguagePriority #{$multi_languages[1]}\n")
+                ios.write("ForceLanguagePriority Prefer Fallback\n")
+                ios.write("DirectoryIndex index\n")
+            end
+            ios.close
+        end
+
         #- pass through if there are no images and videos
         if images.size == 0 && videos.size == 0
             if !$forgui
                 #- cleanup old images/videos, especially if this directory contained images/videos previously.
-                themestuff = Dir.entries("#{$FPATH}/themes/#{$theme}").
-                                find_all { |e| !%w(. .. skeleton_image.html skeleton_thumbnails.html skeleton_index.html metadata CVS).include?(e) }
                 if $mode != 'gen_config'
-                    rightful_images = []
+                    rightful_images = [ '.htaccess' ]
                     if xmldir.attributes['thumbnails-caption']
                         rightful_images << 'thumbnails-thumbnail.jpg'
                     end
@@ -747,18 +978,31 @@ def walk_source_dir
                         subdir = make_dest_filename(from_utf8(File.basename(child.attributes['path'])))
                         rightful_images << "thumbnails-#{subdir}.jpg"
                     }
-                    to_del = Dir.entries(dest_dir).find_all { |e| !File.directory?("#{dest_dir}/#{e}") && !rightful_images.include?(e) } - themestuff
+                    to_del = Dir.entries(dest_dir).find_all { |e| !File.directory?(File.join(dest_dir, e)) && !rightful_images.include?(e) }
                     if to_del.size > 0
-                        system("rm -f " + to_del.collect { |e| "'#{dest_dir}/#{e}'" }.join(' '))
+                        File.delete(*to_del.collect { |e| File.join(dest_dir, e) })
                     end
                 end
                 
                 #- copy any resource file that goes with the theme (css, images..)
+                themestuff = Dir.entries("#{$themedir}").
+                                 find_all { |e| !%w(. .. skeleton_image.html skeleton_thumbnails.html skeleton_index.html metadata root).include?(e) }
                 themestuff.each { |entry|
-                    if !File.exists?("#{dest_dir}/#{entry}")
-                        psys("cp '#{$FPATH}/themes/#{$theme}/#{entry}' '#{dest_dir}'")
+                    if !File.exists?(File.join(dest_dir, entry))
+                        psys("cp '#{$themedir}/#{entry}' '#{dest_dir}'")
                     end
                 }
+
+                #- copy any root-only resource file that goes with the theme (css, images..)
+                if xmldir.parent.name != 'dir'
+                    themestuff_root = Dir.entries("#{$themedir}/root").
+                                          find_all { |e| !%w(. ..).include?(e) }
+                    themestuff_root.each { |entry|
+                        if !File.exists?(File.join(dest_dir, entry))
+                            psys("cp '#{$themedir}/root/#{entry}' '#{dest_dir}'")
+                        end
+                    }
+                end
             end
             next
         end
@@ -771,6 +1015,8 @@ def walk_source_dir
             fullscreen_images[sizeobj['name']] = []
             thumbnail_images ||= {}
             thumbnail_images[sizeobj['name']] = []
+            fullscreen_videos ||= {}
+            fullscreen_videos[sizeobj['name']] = []
             thumbnail_videos ||= {}
             thumbnail_videos[sizeobj['name']] = []
         end
@@ -778,20 +1024,21 @@ def walk_source_dir
         thumbnail_images['dont-delete-file-for-gui'] = []
         if $limit_sizes =~ /original/
             fullscreen_images['original'] = []
+            fullscreen_videos['original'] = []
         end
 
-        images.size >= 1 and msg 3, _("\tcreating images thumbnails...")
+        images.size >= 1 and msg 3, _("\tcreating photos thumbnails...")
 
         #- create thumbnails for images
         images.each { |img|
             info("processing element")
             base_dest_img = dest_dir + '/' + make_dest_filename(img.sub(/\.[^\.]+$/, ''))
+            elem = optelements['image|' + utf8(img)]
             if $forgui
                 thumbnail_dest_img = base_dest_img + "-#{$default_size['thumbnails']}.jpg"
-                gen_thumbnails_element("#{dir}/#{img}", xmldir, true, [ { 'filename' => thumbnail_dest_img, 'size' => $default_size['thumbnails'] } ])
+                gen_thumbnails_element("#{dir}/#{img}", elem, true, [ { 'filename' => thumbnail_dest_img, 'size' => $default_size['thumbnails'] } ])
             else
                 todo = []
-                elem = xmldir.elements["image[@filename='#{utf8(img)}']"]
                 for sizeobj in $images_size
                     size_fullscreen = sizeobj['fullscreen']
                     size_thumbnails = sizeobj['thumbnails']
@@ -804,9 +1051,9 @@ def walk_source_dir
                     end
                     thumbnail_dest_img = base_dest_img + "-#{size_thumbnails}.jpg"
                     thumbnail_images[sizeobj['name']] << File.basename(thumbnail_dest_img)
-                    todo << { 'filename' => thumbnail_dest_img,  'size' => size_thumbnails }
+                    todo << { 'filename' => thumbnail_dest_img, 'size' => size_thumbnails }
                 end
-                gen_thumbnails_element("#{dir}/#{img}", xmldir, true, todo)
+                gen_thumbnails_element("#{dir}/#{img}", elem, true, todo)
                 if $limit_sizes =~ /original/
                     fullscreen_images['original'] << img
                 end
@@ -825,175 +1072,394 @@ def walk_source_dir
         }
 
         videos.size >= 1 and msg 3, _("\tcreating videos thumbnails...")
+        transcoded_videos = {}
+        mp4_videos = {}
 
         #- create thumbnails for videos
         videos.each { |video|
             info("processing element")
+            base_dest_img = dest_dir + '/' + make_dest_filename(video.sub(/\.[^\.]+$/, ''))
+            elem = optelements['video|' + utf8(video)]
             if $forgui
-                thumbnail_dest_img = dest_dir + '/' + make_dest_filename(video.sub(/\.[^\.]+$/, '')) + "-#{$default_size['thumbnails']}.jpg"
-                gen_thumbnails_element("#{dir}/#{video}", xmldir, true, [ { 'filename' => thumbnail_dest_img, 'size' => $default_size['thumbnails'] } ])
+                thumbnail_dest_img = base_dest_img + "-#{$default_size['thumbnails']}.jpg"
+                gen_thumbnails_element("#{dir}/#{video}", elem, true, [ { 'filename' => thumbnail_dest_img, 'size' => $default_size['thumbnails'] } ])
             else
                 todo = []
                 for sizeobj in $images_size
+                    size_fullscreen = sizeobj['fullscreen']
                     size_thumbnails = sizeobj['thumbnails']
-                    thumbnail_dest_img = dest_dir + '/' + make_dest_filename(video.sub(/\.[^\.]+$/, '')) + "-#{size_thumbnails}.jpg"
+                    fullscreen_dest_img = base_dest_img + "-#{size_fullscreen}.jpg"
+                    fullscreen_videos[sizeobj['name']] << File.basename(fullscreen_dest_img)
+                    todo << { 'filename' => fullscreen_dest_img, 'size' => size_fullscreen }
+                    thumbnail_dest_img = base_dest_img + "-#{size_thumbnails}.jpg"
                     thumbnail_videos[sizeobj['name']] << File.basename(thumbnail_dest_img)
                     todo << { 'filename' => thumbnail_dest_img, 'size' => size_thumbnails }
                 end
-                gen_thumbnails_element("#{dir}/#{video}", xmldir, true, todo)
-            end
-            destvideo = "#{dest_dir}/#{video}"
-            if !File.exists?(destvideo)
-                if $hardlinks_ok
-                    if ! sys("ln '#{dir}/#{video}' '#{destvideo}'")
-                        $hardlinks_ok = false
+                gen_thumbnails_element("#{dir}/#{video}", elem, true, todo)
+
+                if $transcode_videos
+                    parts = $transcode_videos.split(':', 2)
+                    basedestvideo = video.sub(/\.\w+/, '') + '.' + parts[0]
+                    transcoded_videos[video] = basedestvideo
+                    destvideo = "#{dest_dir}/#{basedestvideo}"
+                    if ! File.exists?(destvideo)
+                        psys(parts[1].gsub(/%f/, "'#{dir}/#{video}'").gsub(/%o/, "'#{destvideo}'"))
+                    end
+
+                elsif $mp4_generator
+                    msg 3, "video1"
+                    basedestvideo = video.sub(/\.\w+/, '') + '.mp4'
+                    mp4_videos[video] = basedestvideo
+                    destvideo = "#{dest_dir}/#{basedestvideo}"
+                    if File.exists?(destvideo) && File.size(destvideo) == 0
+                        File.delete(destvideo)
+                    end
+                    if ! File.exists?(destvideo)
+                        psys($mp4_generator.gsub(/%f/, "'#{dir}/#{video}'").gsub(/%o/, "'#{destvideo}'"))
+                    end
+                    msg 3, "video2"
+
+                else
+                    destvideo = "#{dest_dir}/#{video}"
+                    if ! File.exists?(destvideo)
+                        if $hardlinks_ok
+                            if ! sys("ln '#{dir}/#{video}' '#{destvideo}'")
+                                $hardlinks_ok = false
+                            end
+                        end
+                        if ! $hardlinks_ok
+                            psys("cp '#{dir}/#{video}' '#{destvideo}'")
+                        end
                     end
-                end
-                if ! $hardlinks_ok
-                    psys("cp '#{dir}/#{video}' '#{destvideo}'")
                 end
             end
         }
 
-        if !$forgui
-            themestuff = Dir.entries("#{$FPATH}/themes/#{$theme}").
-                             find_all { |e| !%w(. .. skeleton_image.html skeleton_thumbnails.html skeleton_index.html metadata CVS).include?(e) }
-
+        if !$forgui            
             #- cleanup old images/videos (for when removing elements or sizes)
             all_elements = fullscreen_images.collect { |e| e[1] }.flatten.
                      concat(thumbnail_images.collect { |e| e[1] }.flatten).
+                     concat(fullscreen_videos.collect { |e| e[1] }.flatten).
                      concat(thumbnail_videos.collect { |e| e[1] }.flatten).
-                     concat(videos)
-            to_del = Dir.entries(dest_dir).find_all { |e| !File.directory?("#{dest_dir}/#{e}") && !all_elements.include?(e) && e !~ /^thumbnails-\w+\.jpg/ } - themestuff
+                     concat($transcode_videos ? transcoded_videos.values : $mp4_generator ? mp4_videos.values : videos).
+                     push('.htaccess')
+            to_del = Dir.entries(dest_dir).find_all { |e| !File.directory?(File.join(dest_dir, e)) && !all_elements.include?(e) && e !~ /^thumbnails-\w+\.jpg/ }
             if to_del.size > 0
-                msg 3, _("\tcleaning up: #{to_del.join(', ')}")
-                system("rm -f " + to_del.collect { |e| "#{dest_dir}/#{e}" }.join(' '))
+                msg 3, _("\tcleaning up: ") + to_del.join(', ')
+                File.delete(*to_del.collect { |e| File.join(dest_dir, e) })
             end
 
             #- copy any resource file that goes with the theme (css, images..)
+            themestuff = Dir.entries("#{$themedir}").
+                             find_all { |e| !%w(. .. skeleton_image.html skeleton_thumbnails.html skeleton_index.html metadata root).include?(e) }
             themestuff.each { |entry|
-                if !File.exists?("#{dest_dir}/#{entry}")
-                    psys("cp '#{$FPATH}/themes/#{$theme}/#{entry}' '#{dest_dir}'")
+                if !File.exists?(File.join(dest_dir, entry))
+                    psys("cp '#{$themedir}/#{entry}' '#{dest_dir}'")
                 end
             }
-
-            #- generate .htaccess file if needed
-            if auth_user_file = xmldir.attributes['password-protect']
-                msg 3, _("\tgenerating password protection file #{dest_dir}/.htaccess")
-                ios = File.open("#{dest_dir}/.htaccess", "w")
-                ios.write("AuthType Basic\nAuthName \"protected area\"\nAuthUserFile #{auth_user_file}\nrequire valid-user\n")
-                ios.close
+            
+            #- copy any root-only resource file that goes with the theme (css, images..)
+            if xmldir.parent.name != 'dir'
+                themestuff_root = Dir.entries("#{$themedir}/root").
+                                      find_all { |e| !%w(. ..).include?(e) }
+                themestuff_root.each { |entry|
+                    if !File.exists?(File.join(dest_dir, entry))
+                        psys("cp '#{$themedir}/root/#{entry}' '#{dest_dir}'")
+                    end
+                }
             end
-
+            
             msg 3, _("\tgenerating HTML pages...")
+            #- fixup max per page
+            if $N_per_page
+                $N_per_page = $N_per_page.to_i / ($N_per_row || $default_N).to_i * ($N_per_row || $default_N).to_i
+            end
 
-            #- generate thumbnails.html (page with thumbnails)
+            #- generate thumbnails*.html (page with thumbnails)
+            element2thumbnailpage4js = []
             for sizeobj in $images_size
                 info("processing size")
                 html = $html_thumbnails.collect { |l| l.clone }
                 iterations = {}
                 for i in html
-                    i.sub!(/~~run_slideshow~~/, images.size <= 1 ? '' : '<a href="image-' + size2js(sizeobj['name']) + '.html#run_slideshow=1">' + utf8(_('Run slideshow!'))+'</a>')
-                    i.sub!(/~~title~~/, xmldir.attributes['thumbnails-caption'] || utf8(File.basename(dir)))
-                    substitute_html_sizes(i, sizeobj, 'thumbnails')
+                    i.sub!(/~~title~~/, quote_caption(xmldir.attributes['thumbnails-caption'] || utf8(File.basename(dir))))
                     discover_iterations(iterations, i)
                 end
+                all_pages = []
                 html_thumbnails = ''
+                html_thumbnails_nojs = ''
+                counter = 0
+                pagecount = 0
                 reset_iterations(iterations)
+                #- preprocess the @filename->elem, rexml is very slow with that; we dramatically improve this part of the processing
+                optfilename = {}
+                xmldir.elements.each('image') { |elem|
+                    optfilename[elem.attributes['filename']] = elem
+                }
                 for file in entries
                     type = images.include?(file) ? 'image' : videos.include?(file) ? 'video' : nil
                     if type
-                        if type == 'image' && elem = xmldir.elements["image[@filename='#{utf8(file)}']"]
-                            if  pano = pano_amount(elem)
-                                html_thumbnails += run_iterations(iterations, pano)
-                                html_thumbnails.gsub!(/~~colspan~~/) { 'colspan="' + pano.ceil.to_s + '"' }
+                        homogeinize_width = 100 / ($N_per_row || $default_N).to_i
+                        if type == 'image' && elem = optfilename[utf8(file)]
+                            if pano = pano_amount(elem)
+                                html_elem = run_iterations(iterations, pano)
+                                counter += count = pano.ceil
+                                html_elem.gsub!(/~~colspan~~/) { "colspan=\"#{count}\"" }
+                                homogeinize_width *= count
                             else
-                                html_thumbnails += run_iterations(iterations, 1)
-                                html_thumbnails.gsub!(/~~colspan~~/, '')
+                                html_elem = run_iterations(iterations, 1)
+                                counter += 1
+                                html_elem.gsub!(/~~colspan~~/, '')
                             end
                         else 
-                            html_thumbnails += run_iterations(iterations, 1)
-                            html_thumbnails.gsub!(/~~colspan~~/, '')
+                            html_elem = run_iterations(iterations, 1)
+                            counter += 1
+                            html_elem.gsub!(/~~colspan~~/, '')
                         end
+                        html_elem.gsub!(/~~homogeinize_width~~/) { "width=\"#{homogeinize_width}%\"" }
                         if type == 'image'
                             index = images.index(file)
-                            html_thumbnails.gsub!(/~~image_iteration~~/,
-                                                  '<a href="image-' + size2js(sizeobj['name']) + '.html#current=' + fullscreen_images[sizeobj['name']][index] +
-                                                      '" name="' + fullscreen_images[sizeobj['name']][index] + '">' +
-                                                      img_element("#{dest_dir}/#{thumbnail_images[sizeobj['name']][index]}") + '</a>')
-                            html_thumbnails.gsub!(/~~caption_iteration~~/,
-                                                  find_caption_value(xmldir, images[index]) || utf8(images[index]))
-                            html_thumbnails.gsub!(/~~ifimage\?~~(.+?)~~fi~~/) { $1 }
-                            html_thumbnails.gsub!(/~~ifvideo\?~~(.+?)~~fi~~/, '')
+                            caption = find_caption_value(xmldir, images[index]) || utf8(images[index])
+                            html_elem.gsub!(/~~caption_iteration~~/, quote_caption(caption))
+                            html_elem.gsub!(/~~ifimage\?~~(.+?)~~fi~~/) { $1 }
+                            html_elem.gsub!(/~~ifvideo\?~~(.+?)~~fi~~/, '')
                         elsif type == 'video'
                             index = videos.index(file)
+                            caption = find_caption_value(xmldir, videos[index]) || utf8(videos[index])
                             if File.exists?("#{dest_dir}/#{thumbnail_videos[sizeobj['name']][index]}")
-                                html_thumbnails.gsub!(/~~image_iteration~~/,
-                                                      '<a href="' + videos[index] + '">' + img_element("#{dest_dir}/#{thumbnail_videos[sizeobj['name']][index]}") + '</a>')
+                                video_preview = '<div class="thumbnail_video">' + img_element("#{dest_dir}/#{thumbnail_videos[sizeobj['name']][index]}", caption) + '</div>'
                             else
-                                html_thumbnails.gsub!(/~~image_iteration~~/,
-                                                      '<a href="' + videos[index] + '">' + utf8(_("(no preview)")) + '</a>')
+                                video_preview = defer_translation(N_("(no preview)"))
                             end
-                            html_thumbnails.gsub!(/~~caption_iteration~~/,
-                                                  find_caption_value(xmldir, videos[index]) || utf8(videos[index]))
-                            html_thumbnails.gsub!(/~~ifimage\?~~(.+?)~~fi~~/, '')
-                            html_thumbnails.gsub!(/~~ifvideo\?~~(.+?)~~fi~~/) { $1 }
+                            if $mp4_generator
+                                size = get_image_size("#{dest_dir}/#{thumbnail_videos[sizeobj['name']][index]}")
+                                if ! size
+                                    sizeobj['thumbnails'] =~ /(\d+)x(\d+)/
+                                    size = { :x => $1, :y => $2 }
+                                end
+                                sizespec = 'width:' + size[:x].to_s + 'px;height:' + (size[:y] + 24).to_s + 'px'
+                                image_iteration = '<a href="' + mp4_videos[videos[index]] + '" style="display:block;' + sizespec + '"' +
+                                                  '   id="player' + index.to_s + '">' + video_preview +
+                                                  '<img src="~~pathtobase~~play_video.png" style="position:relative;top:-' + ((size[:y]+48)/2).to_s + 'px;border:0;background-color:transparent"/></a>'
+                                html_elem.gsub!(/~~image_iteration~~/, apply_hook(:image_iteration, image_iteration, 'video'))
+                            else
+                                html_elem.gsub!(/~~image_iteration~~/,
+                                                '<a href="' + ( $transcode_videos ? transcoded_videos[videos[index]] : videos[index] ) + '">' +
+                                                    video_preview + '</a>')
+                            end
+                            html_elem.gsub!(/~~caption_iteration~~/, quote_caption(caption))
+                            html_elem.gsub!(/~~ifimage\?~~(.+?)~~fi~~/, '')
+                            html_elem.gsub!(/~~ifvideo\?~~(.+?)~~fi~~/) { $1 }
+                        end
+                        html_thumbnails += html_elem
+                        html_thumbnails_nojs += html_elem
+                        if type == 'image'
+                            img_preview = '<div class="thumbnail_image">' + img_element("#{dest_dir}/#{thumbnail_images[sizeobj['name']][index]}", caption) + '</div>'
+                            html_thumbnails.gsub!(/~~image_iteration~~/,
+                                                  '<a href="image-' + size2js(sizeobj['name']) + $htmlsuffix + '#current=' + fullscreen_images[sizeobj['name']][index] +
+                                                      '" name="' + fullscreen_images[sizeobj['name']][index] + '">' + img_preview + '</a>')
+                            html_thumbnails_nojs.gsub!(/~~image_iteration~~/, 
+                                                       '<a href="' + fullscreen_images[sizeobj['name']][index] + '" name="' + fullscreen_images[sizeobj['name']][index] + '">' +
+                                                       img_preview + '</a>')
+                        end
+                        #- remember in which thumbnails page is this element, for element->thumbnail link
+                        if sizeobj == $images_size[0]
+                            element2thumbnailpage4js << pagecount
+                        end
+
+                        if counter == $N_per_page
+                            html_thumbnails      += close_iterations(iterations)
+                            html_thumbnails_nojs += close_iterations(iterations)
+                            all_pages << [ html_thumbnails, html_thumbnails_nojs ]
+                            html_thumbnails = ''
+                            html_thumbnails_nojs = ''
+                            counter = 0
+                            reset_iterations(iterations)
+                            pagecount += 1
                         end
                     end
                 end
-                html_thumbnails += close_iterations(iterations)
+                if counter > 0
+                    html_thumbnails      += close_iterations(iterations)
+                    html_thumbnails_nojs += close_iterations(iterations)
+                    all_pages << [ html_thumbnails, html_thumbnails_nojs ]
+                end
                 for i in html
-                    i.sub!(/~~thumbnails~~/, html_thumbnails)
                     i.gsub!(/~~theme~~/, $theme)
                     i.gsub!(/~~current_size~~/, sizeobj['name'])
+                    i.gsub!(/~~htmlsuffix~~/, $htmlsuffix)
                     i.gsub!(/~~current_size_js~~/, size2js(sizeobj['name']))
                     i.gsub!(/~~madewith~~/, $madewith || '')
                     i.gsub!(/~~indexlink~~/, $indexlink || '')
+                    if !$indexlink || $indexlink.empty?
+                       i.gsub!(/~~ifindexlink\?~~(.+?)~~fi~~/) { }
+                    else
+                       i.gsub!(/~~ifindexlink\?~~(.+?)~~fi~~/) { $1 }
+                    end
+                    substitute_addthis(i)
+                    substitute_navigation(i, xmldir)
+                end
+                html_nojs = html.collect { |l| l.clone }
+                pagecount = 0
+                for page in all_pages
+                    html_thumbnails, html_thumbnails_nojs = page
+                    final_html = html.collect { |l| l.clone }
+                    mstuff = defer_translation(N_("Pages: ")) +
+                             (pagecount > 0 ? "<a href=\"thumbnails-#{size2js(sizeobj['name'])}%nojs-#{pagecount - 1}#{$htmlsuffix}\">" + defer_translation(N_("<- Previous")) + "</a> " : '') +
+                             all_pages.collect_with_index { |p,idx| page == p ? idx + 1 : "<a href=\"thumbnails-#{size2js(sizeobj['name'])}%nojs-#{idx}#{$htmlsuffix}\">#{idx + 1}</a>" }.join(', ') +
+                             (pagecount < all_pages.size - 1 ? " <a href=\"thumbnails-#{size2js(sizeobj['name'])}%nojs-#{pagecount + 1}#{$htmlsuffix}\">" + defer_translation(N_("Next ->")) + "</a> " : '')
+                    for i in final_html
+                        i.sub!(/~~run_slideshow_link~~/, entries.size <= 1 ? '' : 'image-' + size2js(sizeobj['name']) + $htmlsuffix + '#run_slideshow=1')
+                        i.sub!(/~~run_slideshow_text~~/, defer_translation(N_("Run slideshow!")))
+                        i.sub!(/~~run_slideshow~~/,
+                               entries.size <= 1 ? '' : '<a href="image-' + size2js(sizeobj['name']) + $htmlsuffix + '#run_slideshow=1">' + defer_translation(N_("Run slideshow!"))+'</a>')
+                        i.sub!(/~~thumbnails~~/, html_thumbnails)
+                        if all_pages.size == 1
+                            i.gsub!(/~~ifmultiplepages\?~~.*~~fi~~/, '')
+                        else
+                            i.gsub!(/~~ifmultiplepages\?~~(.+?)~~fi~~/) { $1 }
+                            i.gsub!(/~~multiplepagesstuff~~/, mstuff.gsub('%nojs', ''))
+                        end
+                        substitute_html_sizes(i, sizeobj, 'thumbnails', "-#{pagecount}")
+                        substitute_pathtobase(i, xmldir)
+                    end
+                    save_html(final_html, "#{dest_dir}/thumbnails-#{size2js(sizeobj['name'])}-#{pagecount}")
+                    final_html_nojs = html_nojs.collect { |l| l.clone }
+                    for i in final_html_nojs
+                        i.sub!(/~~run_slideshow~~/, defer_translation(N_("<i>Click on an image to view it larger</i>")))
+                        i.sub!(/~~thumbnails~~/, html_thumbnails_nojs)
+                        if all_pages.size == 1
+                            i.gsub!(/~~ifmultiplepages\?~~.*~~fi~~/, '')
+                        else
+                            i.gsub!(/~~ifmultiplepages\?~~(.+?)~~fi~~/) { $1 }
+                            i.gsub!(/~~multiplepagesstuff~~/, mstuff.gsub('%nojs', '-nojs'))
+                        end
+                        substitute_html_sizes(i, sizeobj, 'thumbnails', "-nojs-#{pagecount}")
+                        substitute_pathtobase(i, xmldir)
+                    end
+                    save_html(final_html_nojs, "#{dest_dir}/thumbnails-#{size2js(sizeobj['name'])}-nojs-#{pagecount}")
+                    pagecount += 1
                 end
-                ios = File.open("#{dest_dir}/thumbnails-#{size2js(sizeobj['name'])}.html", "w")
-                ios.write(html)
-                ios.close
             end
 
             info("finished processing sizes")
 
             #- generate "main" thumbnails.html page that will reload to correct size thanks to cookie
-            ios = File.open("#{dest_dir}/thumbnails.html", "w")
-            ios.write(html_reload_to_thumbnails)
-            ios.close
+            save_html(html_reload_to_thumbnails, "#{dest_dir}/thumbnails")
 
             #- generate image.html (page with fullscreen images)
-            if images.size > 0
-                captions4js = find_captions(xmldir, images).collect { |e| e ? '"' + e.gsub('"', '\"') + '"' : '""' }.join(', ')
+            captions4js = []
+            types4js = []
+            videos4js = []
+            for file in entries
+                type = images.include?(file) ? 'image' : videos.include?(file) ? 'video' : nil
+                if type
+                    caption = find_caption_value(xmldir, file)
+                    if caption
+                        captions4js << '"' + quote_caption(caption, true) + '"'
+                    else
+                        captions4js << '""'
+                    end
+                    types4js << '"' + type + '"'
+                    if type == 'video'
+                        index = videos.index(file)
+                        if $mp4_generator
+                            videos4js << '"' + mp4_videos[videos[index]] + '"'
+                        else
+                            videos4js << '"' + videos[index] + '"'
+                        end
+                    else
+                        videos4js << '""'
+                    end
+                end
+            end
 
-                for sizeobj in $images_size
-                    html = $html_images.collect { |l| l.clone }
-                    images4js = fullscreen_images[sizeobj['name']].collect { |e| "\"#{e}\"" }.join(', ')
-                    otherimages4js = ''
-                    othersizes = []
-                    for sizeobj2 in all_images_sizes
-                        if sizeobj != sizeobj2
-                            otherimages4js += "var images_#{size2js(sizeobj2['name'])} = new Array(" + fullscreen_images[sizeobj2['name']].collect { |e| "\"#{e}\"" }.join(', ') + ")\n"
-                            othersizes << "\"#{size2js(sizeobj2['name'])}\""
+            for sizeobj in $images_size
+                elements4js = []
+                elemswidths4js = []
+                elemsheights4js = []
+                for file in entries
+                    type = images.include?(file) ? 'image' : videos.include?(file) ? 'video' : nil
+                    if type
+                        if type == 'image'
+                            index = images.index(file)
+                            elements4js << '"' + fullscreen_images[sizeobj['name']][index] + '"'
+                            size = get_image_size("#{dest_dir}/#{fullscreen_images[sizeobj['name']][index]}")
+                            if size
+                                elemswidths4js << size[:x].to_s
+                                elemsheights4js << size[:y].to_s
+                            else
+                                elemswidths4js << "''"
+                                elemsheights4js << "''"
+                            end
+                        else
+                            index = videos.index(file)
+                            elements4js << '"' + fullscreen_videos[sizeobj['name']][index] + '"'
+                            size = get_image_size("#{dest_dir}/#{fullscreen_videos[sizeobj['name']][index]}")
+                            if size
+                                elemswidths4js << size[:x].to_s
+                                elemsheights4js << size[:y].to_s
+                            else
+                                elemswidths4js << "''"
+                                elemsheights4js << "''"
+                            end
                         end
                     end
-                    for i in html
-                        i.gsub!(/~~images~~/, images4js)
-                        i.gsub!(/~~other_images~~/, otherimages4js)
-                        i.gsub!(/~~other_sizes~~/, othersizes.join(', '))
-                        i.gsub!(/~~captions~~/, captions4js)
-                        i.gsub!(/~~title~~/, xmldir.attributes['thumbnails-caption'] || utf8(File.basename(dir)))
-                        i.gsub!(/~~thumbnails~~/, '<a href="thumbnails-' + size2js(sizeobj['name']) + '.html" id="thumbnails">' + utf8(_('return to thumbnails')) + '</a>')
-                        i.gsub!(/~~theme~~/, $theme)
-                        i.gsub!(/~~current_size~~/, size2js(sizeobj['name']))
-                        i.gsub!(/~~madewith~~/, $madewith || '')
-                        i.gsub!(/~~indexlink~~/, $indexlink || '')
-                        substitute_html_sizes(i, sizeobj, 'image')
+                end
+
+                html = $html_images.collect { |l| l.clone }
+                hiddenimages4remotes = '<span style="display:none">' + thumbnail_images[sizeobj['name']][0..4].collect { |e| "<img src=\"#{e}\"/>" }.join('') + '</span>'
+                otherelements4js = ''
+                othersizes = []
+                for sizeobj2 in all_images_sizes
+                    if sizeobj != sizeobj2
+                        oe4js = []
+                        for file in entries
+                            type = images.include?(file) ? 'image' : videos.include?(file) ? 'video' : nil
+                            if type
+                                if type == 'image'
+                                    index = images.index(file)
+                                    oe4js << '"' + fullscreen_images[sizeobj2['name']][index] + '"'
+                                elsif sizeobj2['name'] != 'original'
+                                    index = videos.index(file)
+                                    oe4js << '"' + fullscreen_videos[sizeobj2['name']][index] + '"'
+                                end
+                            end
+                        end
+                        otherelements4js += "var elements_#{size2js(sizeobj2['name'])} = new Array(" + oe4js.join(', ') + ");\n"
+                        othersizes << "\"#{size2js(sizeobj2['name'])}\""
+                    end
+                end
+                for i in html
+                    i.gsub!(/~~images~~/, elements4js.join(', '))
+                    i.gsub!(/~~types~~/, types4js.join(', '))
+                    i.gsub!(/~~videos~~/, videos4js.join(', '))
+                    i.gsub!(/~~widths~~/, elemswidths4js.join(', '))
+                    i.gsub!(/~~heights~~/, elemsheights4js.join(', '))
+                    i.gsub!(/~~hidden_images_for_remotes~~/, hiddenimages4remotes)
+                    i.gsub!(/~~other_images~~/, otherelements4js)
+                    i.gsub!(/~~thumbnailspages~~/, element2thumbnailpage4js.collect { |e| "\"#{e}\"" }.join(', '))
+                    i.gsub!(/~~other_sizes~~/, othersizes.join(', '))
+                    i.gsub!(/~~captions~~/, captions4js.join(', '))
+                    i.gsub!(/~~title~~/, quote_caption(xmldir.attributes['thumbnails-caption'] || utf8(File.basename(dir))))
+
+                    i.gsub!(/~~thumbnails~~/, '<a id="thumbnails">' + defer_translation(N_('return to thumbnails')) + '</a>')
+                    i.gsub!(/~~theme~~/, $theme)
+                    i.gsub!(/~~current_size~~/, size2js(sizeobj['name']))
+                    i.gsub!(/~~htmlsuffix~~/, $htmlsuffix)
+                    i.gsub!(/~~madewith~~/, $madewith || '')
+                    i.gsub!(/~~indexlink~~/, $indexlink || '')
+                    if !$indexlink || $indexlink.empty?
+                        i.gsub!(/~~ifindexlink\?~~(.+?)~~fi~~/) { }
+                    else
+                        i.gsub!(/~~ifindexlink\?~~(.+?)~~fi~~/) { $1 }
                     end
-                    ios = File.open("#{dest_dir}/image-#{size2js(sizeobj['name'])}.html", "w")
-                    ios.write(html)
-                    ios.close
+                    substitute_addthis(i)
+                    substitute_html_sizes(i, sizeobj, 'image', '')
+                    substitute_navigation(i, xmldir)
+                    substitute_pathtobase(i, xmldir)
                 end
+                save_html(html, "#{dest_dir}/image-#{size2js(sizeobj['name'])}")
             end
         end
     }
@@ -1005,7 +1471,34 @@ def walk_source_dir
         msg 3, _("\tfixating configuration file...")
         $xmldoc.elements.each('//dir') { |element|
             path = captionpath = element.attributes['path']
-            descendant_element = element.elements['descendant::image'] || element.elements['descendant::video']
+            descendant_element = nil
+            #- search if there is at least one image or video down that dir; use workarounds to rexml slowness
+            #- first, look down only one level, very fast
+            first_desc = nil
+            element.elements.each { |elem|
+                if first_desc.nil?
+                    first_desc = elem
+                end
+                if elem.name == 'image' || elem.name == 'video'
+                    descendant_element = elem
+                    break
+                end
+            }
+            if descendant_element.nil?
+                #- if there was nothing or only directory down one level, look down the first subdir
+                if ! first_desc.nil?
+                    first_desc.elements.each { |elem|
+                        if elem.name == 'image' || elem.name == 'video'
+                            descendant_element = elem
+                            break
+                        end
+                    }
+                end
+                if descendant_element.nil?
+                    #- if there was still nothing found, use complete albeit slow method
+                    descendant_element = element.elements['descendant::image'] || element.elements['descendant::video']
+                end
+            end
             if !descendant_element
                 msg 3, _("\t\tremoving %s, no element in it") % path
                 element.remove  #- means we have a directory with nothing interesting in it
@@ -1035,12 +1528,12 @@ def walk_source_dir
     #- write down to disk config if necessary
     if $config_writeto
         ios = File.open($config_writeto, "w")
-        $xmldoc.write(ios, 0)
+        $xmldoc.write(ios)
         ios.close
     end
 
     if $forgui
-        msg 3, _(" completed necessary stuff for GUI, exiting.")
+        msg 3, _("completed necessary stuff for GUI, exiting.")
         return
     end
 
@@ -1071,22 +1564,9 @@ def walk_source_dir
             iterations = {}
             for i in html
                 caption = xmldir.attributes['subdirs-caption']
-                i.gsub!(/~~title~~/, caption)
-                if xmldir.parent.name == 'dir'
-                    nav = ''
-                    path = '..'
-                    parent = xmldir.parent
-                    while parent.name == 'dir'
-                        parentcaption = parent.attributes['subdirs-caption']
-                        nav = "<a href='#{path}/index.html'>#{parentcaption}</a> #{utf8(_(" > "))} #{nav}"
-                        path += '/..'
-                        parent = parent.parent
-                    end
-                    i.gsub!(/~~ifnavigation\?~~(.+?)~~fi~~/) { $1 }
-                    i.gsub!(/~~navigation~~/, nav + caption)
-                else
-                    i.gsub!(/~~ifnavigation\?~~(.+?)~~fi~~/, '')
-                end
+                i.gsub!(/~~title~~/, quote_caption(caption))
+                substitute_navigation(i, xmldir)
+                substitute_pathtobase(i, xmldir)
                 discover_iterations(iterations, i)
             end
             
@@ -1099,8 +1579,9 @@ def walk_source_dir
                 gen_thumbnails_subdir(from_utf8(xmldir.attributes['thumbnails-captionfile']), xmldir, false,
                                       [ { 'filename' => thumbnail, 'size' => $albums_thumbnail_size } ], 'thumbnails')
                 html_index += run_iterations(iterations, 1)
-                html_index.gsub!(/~~image_iteration~~/, "<a href='thumbnails.html'>" + img_element(thumbnail) + '</a>')
-                html_index.gsub!(/~~caption_iteration~~/, xmldir.attributes['thumbnails-caption'])
+                caption = xmldir.attributes['thumbnails-caption']
+                html_index.gsub!(/~~image_iteration~~/, "<a href=\"thumbnails#{$htmlsuffix}\">" + img_element(thumbnail, caption) + '</a>')
+                html_index.gsub!(/~~caption_iteration~~/, quote_caption(caption))
             end
 
             #- deal with sub-albums (direct to subdirs/index.html pages)
@@ -1114,8 +1595,8 @@ def walk_source_dir
                 captionfile, caption = find_subalbum_caption_info(child)
                 gen_thumbnails_subdir(captionfile, child, false,
                                       [ { 'filename' => thumbnail, 'size' => $albums_thumbnail_size } ], find_subalbum_info_type(child))
-                html_index.gsub!(/~~caption_iteration~~/, caption)
-                html_index.gsub!(/~~image_iteration~~/, "<a href='#{make_dest_filename(subdir)}/index.html'>" + img_element(thumbnail) + '</a>')
+                html_index.gsub!(/~~caption_iteration~~/, quote_caption(caption))
+                html_index.gsub!(/~~image_iteration~~/, "<a href=\"#{subdir}/index#{$htmlsuffix}\">" + img_element(thumbnail, caption) + '</a>')
             }
 
             html_index += close_iterations(iterations)
@@ -1124,36 +1605,61 @@ def walk_source_dir
                 i.gsub!(/~~thumbnails~~/, html_index)
                 i.gsub!(/~~madewith~~/, $madewith || '')
                 i.gsub!(/~~indexlink~~/, $indexlink || '')
+                if !$indexlink || $indexlink.empty?
+                   i.gsub!(/~~ifindexlink\?~~(.+?)~~fi~~/) { }
+                else
+                   i.gsub!(/~~ifindexlink\?~~(.+?)~~fi~~/) { $1 }
+                end
+                substitute_addthis(i)
             end
             
         else
             html = html_reload_to_thumbnails
         end
 
-        ios = File.open("#{dest_dir}/index.html", "w")
-        ios.write(html)
-        ios.close
+        save_html(html, "#{dest_dir}/index")
+        if $multi_languages
+            #- in case MultiViews will not work, generate some compat
+            ios = File.open("#{dest_dir}/index.html", "w")
+            ios.write("<html><head><meta http-equiv=\"refresh\" content=\"0.1;url=index.#{$multi_languages[1]}.html\"></head></html>")
+            ios.close
+        end
 
-        #- substitute "return to albums" and previous/next correctly
+        #- substitute multiple "return to albums", previous/next correctly
+        #- the following two statements are dramatical optimizations to executing for each substInFile callback
+        dirpresent = xmldir.elements['dir']
+        parentname = xmldir.parent.name
         if xmldir.child_byname_notattr('image', 'deleted') || xmldir.child_byname_notattr('video', 'deleted')
-            for sizeobj in $images_size
-                substInFile("#{dest_dir}/thumbnails-#{size2js(sizeobj['name'])}.html") { |line|
-                    sub_previous_next_album(previous_album, next_album, line)
-                    if xmldir.elements['dir']
-                        line.sub!(/~~return_to_albums~~/, '<a href="index.html">' + utf8(_('return to albums')) + '</a>')
-                    else
-                        if xmldir.parent.name == 'dir'
-                            line.sub!(/~~return_to_albums~~/, '<a href="../index.html">' + utf8(_('return to albums')) + '</a>')
-                        else
-                            line.sub!(/~~return_to_albums~~/, '')
+            for suffix in [ '', '-nojs' ]
+                for sizeobj in $images_size
+                    Dir.glob("#{dest_dir}/thumbnails-#{size2js(sizeobj['name'])}#{suffix}-*.html") do |file|
+                        #- unroll translations, they are costly if rerun for each line of files
+                        rta = find_translation_for_file(file, N_('return to albums'))
+                        pa = find_translation_for_file(file, N_('previous album'))
+                        na = find_translation_for_file(file, N_('next album'))
+                        substInFile(file) { |line|
+                            sub_previous_next_album(file, previous_album, next_album, line, pa, na)
+                            if dirpresent
+                                line.sub!(/~~return_to_albums~~/, '<a href="index' + $htmlsuffix + '">' + rta + '</a>')
+                            else
+                                if parentname == 'dir'
+                                    line.sub!(/~~return_to_albums~~/, '<a href="../index' + $htmlsuffix + '">' + rta + '</a>')
+                                else
+                                    line.sub!(/~~return_to_albums~~/, '')
+                                end
+                            end
+                            line
+                        }
+                    end
+                    if suffix == '' && xmldir.child_byname_notattr('image', 'deleted')
+                        Dir.glob("#{dest_dir}/image-#{size2js(sizeobj['name'])}*.html") do |file|
+                            pa = find_translation_for_file(file, N_('previous album'))
+                            na = find_translation_for_file(file, N_('next album'))
+                            substInFile(file) { |line|
+                                sub_previous_next_album(file, previous_album, next_album, line, pa, na)
+                            }
                         end
                     end
-                    line
-                }
-                if xmldir.child_byname_notattr('image', 'deleted')
-                    substInFile("#{dest_dir}/image-#{size2js(sizeobj['name'])}.html") { |line|
-                        sub_previous_next_album(previous_album, next_album, line)
-                    }
                 end
             end
         end
@@ -1169,3 +1675,4 @@ check_installation
 build_html_skeletons
 
 walk_source_dir
+exit(0)