properly html-escape captions; use caption for alt text suggested by Warly
authorGuillaume Cottenceau <gcottenc@gmail.com>
Wed, 17 Jun 2009 12:15:31 +0000 (14:15 +0200)
committerGuillaume Cottenceau <gcottenc@gmail.com>
Wed, 17 Jun 2009 12:15:31 +0000 (14:15 +0200)
bin/booh-backend

index 30b0a0e..7150e23 100755 (executable)
@@ -439,12 +439,16 @@ 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 quote_caption(input)
+    return input.gsub('<', '&lt;').gsub('>', '&gt;').gsub('"', '&quot;').gsub("'", '&#39;').gsub("\n", '<br/>')
+end
+
 def find_captions(xmldir, images)
     return images.collect { |img| find_caption_value(xmldir, img) }
 end
@@ -548,13 +552,13 @@ 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) + '"/>'
 end
 
 def size2js(name)
@@ -1076,8 +1080,7 @@ def walk_source_dir
                 html = $html_thumbnails.collect { |l| l.clone }
                 iterations = {}
                 for i in html
-                    i.sub!(/~~title~~/,
-                           xmldir.attributes['thumbnails-caption'] || utf8(File.basename(dir)))
+                    i.sub!(/~~title~~/, quote_caption(xmldir.attributes['thumbnails-caption'] || utf8(File.basename(dir))))
                     discover_iterations(iterations, i)
                 end
                 all_pages = []
@@ -1114,22 +1117,23 @@ def walk_source_dir
                         html_elem.gsub!(/~~homogeinize_width~~/) { "width=\"#{homogeinize_width}%\"" }
                         if type == 'image'
                             index = images.index(file)
-                            html_elem.gsub!(/~~caption_iteration~~/,
-                                            find_caption_value(xmldir, images[index]) || utf8(images[index]))
+                            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_elem.gsub!(/~~image_iteration~~/,
                                                 '<a href="' + ( $transcode_videos ? transcoded_videos[videos[index]] : videos[index] ) + '">' +
-                                                    img_element("#{dest_dir}/#{thumbnail_videos[sizeobj['name']][index]}") + '</a>')
+                                                    img_element("#{dest_dir}/#{thumbnail_videos[sizeobj['name']][index]}", caption) + '</a>')
                             else
                                 html_elem.gsub!(/~~image_iteration~~/,
                                                 '<a href="' + ( $transcode_videos ? transcoded_videos[videos[index]] : videos[index] ) + '">' +
                                                     defer_translation(N_("(no preview)")) + '</a>')
                             end
-                            html_elem.gsub!(/~~caption_iteration~~/, find_caption_value(xmldir, videos[index]) || utf8(videos[index]))
+                            html_elem.gsub!(/~~caption_iteration~~/, quote_caption(caption))
                             html_elem.gsub!(/~~ifimage\?~~(.+?)~~fi~~/, '')
                             html_elem.gsub!(/~~ifvideo\?~~(.+?)~~fi~~/) { $1 }
                         end
@@ -1139,10 +1143,10 @@ def walk_source_dir
                             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_element("#{dest_dir}/#{thumbnail_images[sizeobj['name']][index]}") + '</a>')
+                                                      img_element("#{dest_dir}/#{thumbnail_images[sizeobj['name']][index]}", caption) + '</a>')
                             html_thumbnails_nojs.gsub!(/~~image_iteration~~/, 
                                                        '<a href="' + fullscreen_images[sizeobj['name']][index] + '" name="' + fullscreen_images[sizeobj['name']][index] + '">' +
-                                                       img_element("#{dest_dir}/#{thumbnail_images[sizeobj['name']][index]}") + '</a>')
+                                                       img_element("#{dest_dir}/#{thumbnail_images[sizeobj['name']][index]}", caption) + '</a>')
                             #- remember in which thumbnails page is this element, for image->thumbnail link
                             if sizeobj == $images_size[0]
                                 image2thumbnailpage4js << pagecount
@@ -1242,7 +1246,7 @@ def walk_source_dir
                         i.gsub!(/~~thumbnailspages~~/, thumbnailspage4js)
                         i.gsub!(/~~other_sizes~~/, othersizes.join(', '))
                         i.gsub!(/~~captions~~/, captions4js)
-                        i.gsub!(/~~title~~/, xmldir.attributes['thumbnails-caption'] || utf8(File.basename(dir)))
+                        i.gsub!(/~~title~~/, quote_caption(xmldir.attributes['thumbnails-caption'] || utf8(File.basename(dir))))
                         i.gsub!(/~~thumbnails~~/, '<a href="thumbnails-' + size2js(sizeobj['name']) + $htmlsuffix + '" id="thumbnails">' + defer_translation(N_('return to thumbnails')) + '</a>')
                         i.gsub!(/~~theme~~/, $theme)
                         i.gsub!(/~~current_size~~/, size2js(sizeobj['name']))
@@ -1359,7 +1363,7 @@ def walk_source_dir
             iterations = {}
             for i in html
                 caption = xmldir.attributes['subdirs-caption']
-                i.gsub!(/~~title~~/, caption)
+                i.gsub!(/~~title~~/, quote_caption(caption))
                 substitute_navigation(i, xmldir)
                 substitute_pathtobase(i, xmldir)
                 discover_iterations(iterations, i)
@@ -1374,8 +1378,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#{$htmlsuffix}\">" + 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)
@@ -1389,8 +1394,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=\"#{subdir}/index#{$htmlsuffix}\">" + 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)