diff options
author | Giuseppe Bilotta <giuseppe.bilotta@gmail.com> | 2011-04-02 10:54:15 +0200 |
---|---|---|
committer | Giuseppe Bilotta <giuseppe.bilotta@gmail.com> | 2011-04-02 10:54:15 +0200 |
commit | 979dfca5faff9e9ea52588220a862bed19a8c731 (patch) | |
tree | 88329d8213488d852918eb8fae1fbbd16967a26f | |
parent | 2e03322bb615cb8f2875691356b25d89f0f77d57 (diff) |
utils: support new HTMLEntities interface
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.
-rw-r--r-- | lib/rbot/core/utils/utils.rb | 18 |
1 files changed, 14 insertions, 4 deletions
diff --git a/lib/rbot/core/utils/utils.rb b/lib/rbot/core/utils/utils.rb index ec3b3c5d..7fe83410 100644 --- a/lib/rbot/core/utils/utils.rb +++ b/lib/rbot/core/utils/utils.rb @@ -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+)$/ |