handle index.html
authorgc <gc>
Mon, 14 Mar 2005 23:02:45 +0000 (23:02 +0000)
committergc <gc>
Mon, 14 Mar 2005 23:02:45 +0000 (23:02 +0000)
booh
themes/simple/parameters.rb
themes/simple/skeleton_image.html [moved from themes/simple/skeleton.html with 94% similarity]
themes/simple/skeleton_index.html [new file with mode: 0644]
themes/simple/skeleton_thumbnails.html

diff --git a/booh b/booh
index b5814cc45f67c1e92b74bd97ffa5a15bef768e5c..e2c0401da1a1263ca893fa418c01710f49bc20ab 100755 (executable)
--- a/booh
+++ b/booh
@@ -215,9 +215,10 @@ def replace_line(surround, keyword, line)
 end
 
 def build_html_skeletons
-    $html_images     = File.open("#{$FPATH}/themes/#{$theme}/skeleton.html").readlines
+    $html_images     = File.open("#{$FPATH}/themes/#{$theme}/skeleton_image.html").readlines
     $html_thumbnails = File.open("#{$FPATH}/themes/#{$theme}/skeleton_thumbnails.html").readlines
-    for line in $html_images + $html_thumbnails
+    $html_index      = File.open("#{$FPATH}/themes/#{$theme}/skeleton_index.html").readlines
+    for line in $html_images + $html_thumbnails + $html_index
         while line =~ /~~~(\w+)~~~/
             replace_line('~~~', $1, line)
         end
@@ -268,6 +269,60 @@ def entry2type(entry)
     end
 end
 
+def gen_thumbnails(orig, dests)
+    if !dests.detect { |dest| !File.exists?(dest['filename']) } 
+        return true
+    end
+
+    if entry2type(orig) == 'image'
+        convert_options = ''
+        orientation = `exif '#{orig}'`.detect { |line| line =~ /^Orientation/ }
+        if orientation =~ /right - top/
+            convert_options += '-rotate 90 '
+        end
+        if orientation =~ /left - bottom/
+            convert_options += '-rotate -90 '
+        end
+        for dest in dests
+            if !File.exists?(dest['filename'])
+                psys("#{$convert} #{convert_options}-geometry #{dest['size']} '#{orig}' '#{dest['filename']}'")
+            end
+        end
+        return true
+
+    elsif entry2type(orig) == 'video'
+        for dest in dests
+            dest_dir = File.dirname(dest['filename'])
+            if !File.exists?("#{dest_dir}/screenshot.jpg000000.jpg")
+                cmd = "transcode -a 0 -c 1 -i '#{orig}' -y jpg -o '#{dest_dir}/screenshot.jpg' 2>&1"
+                msg 2, cmd
+                results = `#{cmd}`
+                if results =~ /V: import format.*unknown/ || !File.exists?("#{dest_dir}/screenshot.jpg000000.jpg")
+                    msg 1, __("could not extract first image of video %s with transcode, will try first converting with mencoder", dest['filename'])
+                    cmd = "mencoder '#{orig}' -nosound -ovc lavc -lavcopts vcodec=mpeg4 -o '#{dest_dir}/foo.avi' -frames 1 >/dev/null 2>/dev/null"
+                    msg 2, cmd
+                    system cmd
+                    if File.exists?("#{dest_dir}/foo.avi")
+                        cmd = "transcode -a 0 -c 1 -i '#{dest_dir}/foo.avi' -y jpg -o '#{dest_dir}/screenshot.jpg' 2>&1"
+                        msg 2, cmd
+                        results = `#{cmd}`
+                        system("rm -f '#{dest_dir}/foo.avi'")
+                        if results =~ /V: import format.*unknown/ || !File.exists?("#{dest_dir}/screenshot.jpg000000.jpg")
+                            msg 0, __("could not extract first image of video %s encoded by mencoder", "#{dest_dir}/foo.avi")
+                            return false
+                        end
+                    else
+                        msg 0, __("could not make mencoder to encode %s to mpeg4", "#{orig}")
+                        return false
+                    end
+                end
+            end
+            sys("#{$convert} -geometry #{dest['size']} #{dest_dir}/screenshot.jpg000000.jpg '#{dest['filename']}'")
+        end
+        return true
+    end
+end
+
 #- stolen from CVSspam
 def urlencode(text)
   text.sub(/[^a-zA-Z0-9\-,.*_\/]/) do
@@ -275,6 +330,72 @@ def urlencode(text)
   end
 end
 
+def html_refresh(target)
+    return "<html><head><META http-equiv='refresh' content='0;URL=#{target}'></head><body></body><html>"
+end
+
+def discover_iterations(iterations, line)
+    if line =~ /~~iterate(\d)_open(_max(\d+))?~~/
+        for iter in iterations.values
+            if iter['open']
+                iter['open'] = false
+                iter['close_wait'] = $1.to_i
+            end
+        end
+        iterations[$1.to_i] = { 'open' => true, 'max' => $3, 'opening' => '', 'closing' => '' }
+        if $1.to_i == 1
+            line.sub!(/.*/, '~~thumbnails~~')
+        else
+            line.sub!(/.*/, '')
+        end
+    elsif line =~ /~~iterate(\d)_close~~/
+        iterations[$1.to_i]['open']  = false;
+        iterations[$1.to_i]['close'] = true;
+        line.sub!(/.*/, '')
+    else
+        for iter in iterations.values
+            if iter['open']
+                iter['opening'] += line
+                line.sub!(/.*/, '')
+            end
+            if !iter['close'] && iter['close_wait'] && iterations[iter['close_wait']]['close']
+                iter['closing'] += line
+                line.sub!(/.*/, '')
+            end
+        end
+    end
+end
+
+def reset_iterations(iterations)
+    for iter in iterations.values
+        iter['value'] = 1
+    end
+end
+
+def run_iterations(iterations)
+    html = ''
+    for level in iterations.keys.sort
+        if iterations[level]['value'] == 1 || level == iterations.keys.max
+            html += iterations[level]['opening']
+        end
+        iterations[level]['value'] += 1
+        if iterations[level]['max'] && iterations[level]['value'] > iterations[level]['max'].to_i
+            iterations[level]['value'] = 1
+            iterations[level-1]['value'] = 1
+            html += iterations[level-1]['closing']
+        end
+    end
+    return html
+end
+
+def close_iterations(iterations)
+    html = ''
+    for level in iterations.keys.sort.reverse
+        html += iterations[level]['closing']
+    end
+    return html
+end
+
 def walk_source_dir
 
     `find #{$source} -type d`.each { |dir|
@@ -294,7 +415,7 @@ def walk_source_dir
                 if !xmldir
                     xmldir = $xmldoc.root
                 end
-                xmldir = xmldir.add_element "dir", { "path" => dir }
+                xmldir = xmldir.add_element 'dir', { 'path' => dir, 'new' => 1 }
             end
         end
 
@@ -344,7 +465,16 @@ def walk_source_dir
         dest_dir = dir.sub(/^#{Regexp.quote($source)}/, $dest)
         system("mkdir -p '#{dest_dir}'")
 
-        #- don't generate HTML files if there are no images and videos
+        #- copy any resource file that goes with the theme (css, images..)
+        for entry in Dir.entries("#{$FPATH}/themes/#{$theme}")
+            if !%w(. .. skeleton_images.html skeleton_thumbnails.html skeleton_index.html parameters.rb CVS).include?(entry)
+                if !File.exists?("#{dest_dir}/#{entry}")
+                    psys("cp '#{$FPATH}/themes/#{$theme}/#{entry}' '#{dest_dir}'")
+                end
+            end
+        end
+
+        #- pass through if there are no images and videos
         if images.size == 0 && videos.size == 0
             next
         end
@@ -371,22 +501,8 @@ def walk_source_dir
                 thumbnail_dest_img  = base_dest_img + "-#{size_thumbnails}.jpg"
                 fullscreen_images[sizeobj['name']] << File.basename(fullscreen_dest_img)
                 thumbnail_images[sizeobj['name']]  << File.basename(thumbnail_dest_img)
-                if !File.exists?(fullscreen_dest_img) || !File.exists?(thumbnail_dest_img)
-                    convert_options = ''
-                    orientation = `exif '#{dir}/#{img}'`.detect { |line| line =~ /^Orientation/ }
-                    if orientation =~ /right - top/
-                        convert_options += '-rotate 90 '
-                    end
-                    if orientation =~ /left - bottom/
-                        convert_options += '-rotate -90 '
-                    end
-                    if !File.exists?(fullscreen_dest_img)
-                        psys("#{$convert} #{convert_options}-geometry #{size_fullscreen} '#{dir}/#{img}' '#{fullscreen_dest_img}'")
-                    end
-                    if !File.exists?(thumbnail_dest_img)
-                        psys("#{$convert} #{convert_options}-geometry #{size_thumbnails} '#{dir}/#{img}' '#{thumbnail_dest_img}'")
-                    end
-                end
+                gen_thumbnails("#{dir}/#{img}", [ { 'filename' => fullscreen_dest_img, 'size' => size_fullscreen },
+                                                  { 'filename' => thumbnail_dest_img, 'size' => size_thumbnails } ])
             end
         }
 
@@ -397,35 +513,7 @@ def walk_source_dir
                 size_thumbnails = sizeobj['thumbnails']
                 thumbnail_dest_img = dest_dir + '/' + video.sub(/\.[^\.]+$/, '') + "-#{size_thumbnails}.jpg"
                 thumbnail_videos[sizeobj['name']] << File.basename(thumbnail_dest_img)
-                if !File.exists?(thumbnail_dest_img) && thumbnail_ok
-                    if !File.exists?("#{dest_dir}/screenshot.jpg000000.jpg")
-                        cmd = "transcode -a 0 -c 1 -i '#{dir}/#{video}' -y jpg -o '#{dest_dir}/screenshot.jpg' 2>&1"
-                        msg 2, cmd
-                        results = `#{cmd}`
-                        if results =~ /V: import format.*unknown/ || !File.exists?("#{dest_dir}/screenshot.jpg000000.jpg")
-                            msg 1, __("could not extract first image of video %s with transcode, will try first converting with mencoder", "#{dir}/#{video}")
-                            cmd = "mencoder '#{dir}/#{video}' -nosound -ovc lavc -lavcopts vcodec=mpeg4 -o '#{dest_dir}/foo.avi' -frames 1 >/dev/null 2>/dev/null"
-                            msg 2, cmd
-                            system cmd
-                            if File.exists?("#{dest_dir}/foo.avi")
-                                cmd = "transcode -a 0 -c 1 -i '#{dest_dir}/foo.avi' -y jpg -o '#{dest_dir}/screenshot.jpg' 2>&1"
-                                msg 2, cmd
-                                results = `#{cmd}`
-                                system("rm -f '#{dest_dir}/foo.avi'")
-                                if results =~ /V: import format.*unknown/ || !File.exists?("#{dest_dir}/screenshot.jpg000000.jpg")
-                                    msg 0, __("could not extract first image of video %s encoded by mencoder", "#{dest_dir}/foo.avi")
-                                    thumbnail_ok = false
-                                    next
-                                end
-                            else
-                                msg 0, __("could not make mencoder to encode %s to mpeg4", "#{dir}/#{video}")
-                                thumbnail_ok = false
-                                next
-                            end
-                        end
-                    end
-                    sys("#{$convert} -geometry #{size_thumbnails} #{dest_dir}/screenshot.jpg000000.jpg '#{thumbnail_dest_img}'")
-                end
+                thumbnail_ok &&= gen_thumbnails("#{dir}/#{video}", [ { 'filename' => thumbnail_dest_img, 'size' => size_thumbnails } ])
             end
             destvideo = "#{dest_dir}/#{video}"
             if !File.exists?(destvideo)
@@ -443,7 +531,7 @@ def walk_source_dir
             iterations = {}
             for i in html
                 i.sub!(/~~run_slideshow~~/, '<a href="image-' + sizeobj['name'] + '.html?run_slideshow">' + _('Run slideshow!') + '</a>')
-                i.sub!(/~~title~~/, File.basename(dir))
+                i.sub!(/~~title~~/, xmldir.attributes['thumbnails-caption'] || File.basename(dir))
                 for sizeobj2 in $images_size
                     if sizeobj != sizeobj2
                         i.sub!(/~~size_#{sizeobj2['name']}~~/, '<a href="thumbnails-' + sizeobj2['name'] + '.html">' + sizenames[sizeobj2['name']] + '</a>')
@@ -451,54 +539,14 @@ def walk_source_dir
                         i.sub!(/~~size_#{sizeobj2['name']}~~/, sizenames[sizeobj2['name']])
                     end
                 end
-                if i =~ /~~iterate(\d)_open(_max(\d+))?~~/
-                    for iter in iterations.values
-                        if iter['open']
-                            iter['open'] = false
-                            iter['close_wait'] = $1.to_i
-                        end
-                    end
-                    iterations[$1.to_i] = { 'open' => true, 'max' => $3, 'opening' => '', 'closing' => '' }
-                    if $1.to_i == 1
-                        i.sub!(/.*/, '~~thumbnails~~')
-                    else
-                        i.sub!(/.*/, '')
-                    end
-                elsif i =~ /~~iterate(\d)_close~~/
-                    iterations[$1.to_i]['open']  = false;
-                    iterations[$1.to_i]['close'] = true;
-                    i.sub!(/.*/, '')
-                else
-                    for iter in iterations.values
-                        if iter['open']
-                            iter['opening'] += i
-                            i.sub!(/.*/, '')
-                        end
-                        if !iter['close'] && iter['close_wait'] && iterations[iter['close_wait']]['close']
-                            iter['closing'] += i
-                            i.sub!(/.*/, '')
-                        end
-                    end
-                end
+                discover_iterations(iterations, i)
             end
             html_thumbnails = ''
-            for iter in iterations.values
-                iter['value'] = 1
-            end
+            reset_iterations(iterations)
             for file in entries
                 type = images.include?(file) ? 'image' : videos.include?(file) ? 'video' : nil
                 if type
-                    for level in iterations.keys.sort
-                        if iterations[level]['value'] == 1 || level == iterations.keys.max
-                            html_thumbnails += iterations[level]['opening']
-                        end
-                        iterations[level]['value'] += 1
-                        if iterations[level]['max'] && iterations[level]['value'] > iterations[level]['max'].to_i
-                            iterations[level]['value'] = 1
-                            iterations[level-1]['value'] = 1
-                            html_thumbnails += iterations[level-1]['closing']
-                        end
-                    end
+                    html_thumbnails += run_iterations(iterations)
                     if type == 'image'
                         index = images.index(file)
                         html_thumbnails.gsub!(/~~image_iteration~~/,
@@ -524,9 +572,7 @@ def walk_source_dir
                     end
                 end
             end
-            for level in iterations.keys.sort.reverse
-                html_thumbnails += iterations[level]['closing']
-            end
+            html_thumbnails += close_iterations(iterations)
             for i in html
                 i.sub!(/~~thumbnails~~/, html_thumbnails)
             end
@@ -554,7 +600,7 @@ def walk_source_dir
                 i.sub!(/~~other_images~~/, otherimages4js)
                 i.sub!(/~~other_sizes~~/, othersizes.join(', '))
                 i.sub!(/~~captions~~/, captions4js)
-                i.sub!(/~~title~~/, File.basename(dir))
+                i.sub!(/~~title~~/, xmldir.attributes['thumbnails-caption'] || File.basename(dir))
                 i.sub!(/~~thumbnails~~/, '<a href="thumbnails-' + sizeobj['name'] + '.html">' + _('Return to thumbnails') + '</a>')
                 for sizeobj2 in $images_size
                     if sizeobj != sizeobj2
@@ -570,24 +616,108 @@ def walk_source_dir
             ios.write(html)
             ios.close
         end
+    }
 
-        #- copy any resource file that goes with the theme (css, images..)
-        for entry in Dir.entries("#{$FPATH}/themes/#{$theme}")
-            if !%w(. .. skeleton.html skeleton_thumbnails.html parameters.rb CVS).include?(entry)
-                if !File.exists?("#{dest_dir}/#{entry}")
-                    psys("cp '#{$FPATH}/themes/#{$theme}/#{entry}' '#{dest_dir}'")
+    #- add attributes to <dir..> elements needing so
+    if $mode != 'use_config'
+        $xmldoc.elements.each('//dir[@new]') { |element|
+            path = captionpath = element.attributes['path']
+            child = element
+            captionfile = nil
+            while true
+                child = child.elements[1]
+                if !child
+                    element.remove  #- means we have a directory with nothing interesting in it
+                    break
+                elsif child.name == 'dir'
+                    captionpath = child.attributes['path']
+                else
+                    captionfile = "#{captionpath}/#{child.attributes['filename']}"
+                    break
                 end
             end
-        end
-    }
+            basename = File.basename(path)
+            if element.elements['dir']
+                element.add_attribute('subdirs-caption', basename)
+                element.add_attribute('subdirs-captionfile', captionfile)
+            end
+            if element.elements['image'] || element.elements['video']
+                element.add_attribute('thumbnails-caption', basename)
+                element.add_attribute('thumbnails-captionfile', captionfile)
+            end
+            element.delete_attribute('new')
+        }
+    end
 
+    #- write down to disk config if necessary
     if $config_writeto
         ios = File.open($config_writeto, "w")
         $xmldoc.write(ios, 0)
         ios.close
     end
 
-    #- add index.html (page with albums)
+    #- second pass to create index.html files
+    default_thumbnails = $images_size.detect { |sizeobj| sizeobj['default'] }
+    if !default_thumbnails
+        die __("Theme `%s' has no default size.", $theme)
+    else
+        default_thumbnails = default_thumbnails['name']
+    end
+
+    `find #{$source} -type d`.each { |dir|
+        dir.chomp!
+        xmldir = $xmldoc.elements["//dir[@path='#{dir}']"]
+        dest_dir = dir.sub(/^#{Regexp.quote($source)}/, $dest)
+
+        html = $html_index.collect { |l| l.clone }
+        iterations = {}
+        for i in html
+            i.sub!(/~~title~~/, xmldir.attributes['subdirs-caption'] || xmldir.attributes['thumbnails-caption'] || File.basename(dir))
+            discover_iterations(iterations, i)
+        end
+
+        html_index = ''
+        reset_iterations(iterations)
+
+        if xmldir.elements['dir']
+            #- deal with "current" album (directs to "thumbnails" page)
+            if xmldir.attributes['thumbnails-caption']
+                thumbnail = "#{dest_dir}/thumbnails-thumbnail.jpg"
+                gen_thumbnails(xmldir.attributes['thumbnails-captionfile'], [ { 'filename' => thumbnail, 'size' => $albums_thumbnail_size } ])
+                html_index += run_iterations(iterations)
+                html_index.gsub!(/~~image_iteration~~/, "<a href='thumbnails-#{default_thumbnails}.html'><img src='#{thumbnail}' border='0'/></a>")
+                html_index.gsub!(/~~caption_iteration~~/, xmldir.attributes['thumbnails-caption'])
+            end
+
+            #- deal with sub-albums (direct to subdirs/index.html pages)
+            xmldir.elements.each('dir') { |child|
+                subdir = File.basename(child.attributes['path'])
+                thumbnail = "#{dest_dir}/thumbnails-#{subdir}.jpg"
+                html_index += run_iterations(iterations)
+                html_index.gsub!(/~~image_iteration~~/, "<a href='#{subdir}/index.html'><img src='#{thumbnail}' border='0'/></a>")
+                #- first look for subdirs info; if not, means there is no subdir
+                caption = child.attributes['subdirs-caption']
+                if caption
+                    gen_thumbnails(child.attributes['subdirs-captionfile'], [ { 'filename' => thumbnail, 'size' => $albums_thumbnail_size } ])
+                    html_index.gsub!(/~~caption_iteration~~/, caption)
+                else
+                    gen_thumbnails(child.attributes['thumbnails-captionfile'], [ { 'filename' => thumbnail, 'size' => $albums_thumbnail_size } ])
+                    html_index.gsub!(/~~caption_iteration~~/, child.attributes['thumbnails-caption'])
+                end
+            }
+        else
+            html = html_refresh("thumbnails-#{default_thumbnails}.html")
+        end
+
+        html_index += close_iterations(iterations)
+        for i in html
+            i.sub!(/~~thumbnails~~/, html_index)
+        end
+
+        ios = File.open("#{dest_dir}/index.html", "w")
+        ios.write(html)
+        ios.close
+    }
 end
 
 handle_options
index 8775518ffaaa3ce83d75b7983a57aa40c6df7698..c7ba91639d3cde659789510deb4b11d1a0a9bf95 100644 (file)
@@ -33,6 +33,7 @@ $images_size = [
         'name' => 'medium',
         'fullscreen' => '704x528',
         'thumbnails' => '240x192',
+        'default' => true,
     },
     {
         'name' => 'large',
@@ -40,3 +41,5 @@ $images_size = [
         'thumbnails' => '300x240',
     }
 ]
+
+$albums_thumbnail_size = '300x240'
similarity index 94%
rename from themes/simple/skeleton.html
rename to themes/simple/skeleton_image.html
index dfccbf4467b99735c501d4d736d4146b12f34f8d..173e5c37e2ec1fb273b8d61c8df4782b3b322104 100644 (file)
@@ -3,6 +3,7 @@
 
 <head>
     <title>~~title~~</title>
+    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
     ~~~head_code~~~
 </head>
 
diff --git a/themes/simple/skeleton_index.html b/themes/simple/skeleton_index.html
new file mode 100644 (file)
index 0000000..56f92fb
--- /dev/null
@@ -0,0 +1,27 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml">
+
+<head>
+    <title>~~title~~</title>
+    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
+</head>
+
+<body bgcolor="#FFFFFF">
+
+<p align="center"><font size="+3">~~title~~</font></p>
+
+<table width="100%" cellpadding="10" border="0">
+~~iterate1_open~~
+<tr>
+    <td width="50%" align="right" valign="top">
+       ~~image_iteration~~
+    </td>
+    <td width="50%" align="left">
+       <p>~~caption_iteration~~</p>
+    </td>
+</tr>
+~~iterate1_close~~
+</table>
+
+</body>
+</html>
index 17cf7fef50466bfa0f35a0626fe30aca2017ba65..38bb6d80ebb5754e559a8a5ad8f53220b348c896 100644 (file)
@@ -3,6 +3,7 @@
 
 <head>
     <title>~~title~~</title>
+    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
 </head>
 
 <body bgcolor="#FFFFFF">