]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/commitdiff
ircify_html: options to handle img tags
authorGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Thu, 24 Sep 2009 20:47:07 +0000 (22:47 +0200)
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Thu, 24 Sep 2009 20:47:07 +0000 (22:47 +0200)
lib/rbot/core/utils/extends.rb

index fc7119fa8c7df89d4bbf969ac88733e28d479635..b1964617f2b153d6fc9f3df19994b6e5369b7a4f 100644 (file)
@@ -245,6 +245,29 @@ class ::String
       warning "unknown :a_href option #{val} passed to ircify_html" if val
     end
 
+    # If opts[:img] is defined, it should be a String. Each image
+    # will be replaced by the string itself, replacing occurrences of
+    # %{alt} %{dimensions} and %{src} with the alt text, image dimensions
+    # and URL
+    if val = opts[:img]
+      if val.kind_of? String
+        txt.gsub!(/<img\s+(.*?)\s*\/?>/) do |imgtag|
+          attrs = Hash.new
+          imgtag.scan(/([[:alpha:]]+)\s*=\s*(['"])?(.*?)\2/) do |key, quote, value|
+            k = key.downcase.intern rescue 'junk'
+            attrs[k] = value
+          end
+          attrs[:alt] ||= attrs[:title]
+          attrs[:width] ||= '...'
+          attrs[:height] ||= '...'
+          attrs[:dimensions] ||= "#{attrs[:width]}x#{attrs[:height]}"
+          val % attrs
+        end
+      else
+        warning ":img option is not a string"
+      end
+    end
+
     # Paragraph and br tags are converted to whitespace
     txt.gsub!(/<\/?(p|br)(?:\s+[^>]*)?\s*\/?\s*>/i, ' ')
     txt.gsub!("\n", ' ')