]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/commitdiff
utils: support new HTMLEntities interface
authorGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Sat, 2 Apr 2011 08:54:15 +0000 (10:54 +0200)
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Sat, 2 Apr 2011 08:54:15 +0000 (10:54 +0200)
Since version 4.1.0, HTMLEntities removed the decode_entities()
class method and uses a decoder since. Support the new interface as well
as the old one.

Also slightly optimize by moving the conditional outside of the method
definition. Now a rescan is required even if HTMLEntities is loaded at
runtime, but the method should be slightly faster.

lib/rbot/core/utils/utils.rb

index ec3b3c5d358292a64a492eee5e5224bbf428dfa9..7fe83410c817cc15b45fbf1cb79bc65c5aa37dc9 100644 (file)
@@ -338,11 +338,21 @@ module ::Irc
     # Decode HTML entities in the String _str_, using HTMLEntities if the
     # package was found, or UNESCAPE_TABLE otherwise.
     #
-    def Utils.decode_html_entities(str)
-      if defined? ::HTMLEntities
-        return HTMLEntities.decode_entities(str)
+
+    if defined? ::HTMLEntities
+      if ::HTMLEntities.respond_to? :decode_entities
+        def Utils.decode_html_entities(str)
+          return HTMLEntities.decode_entities(str)
+        end
       else
-        str.gsub(/(&(.+?);)/) {
+        @@html_entities = HTMLEntities.new
+        def Utils.decode_html_entities(str)
+          return @@html_entities.decode str
+        end
+      end
+    else
+      def Utils.decode_html_entities(str)
+        return str.gsub(/(&(.+?);)/) {
           symbol = $2
           # remove the 0-paddng from unicode integers
           if symbol =~ /^#(\d+)$/