X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=lib%2Frbot%2Fcore%2Futils%2Fextends.rb;h=7b733994b74d323b010db0bc2ee6d66dad1f598d;hb=a209adb0e00e2a335333c1cb696d5807a47072c6;hp=95569b71ff261b6e1bbfbb7e28273bab9eb8578c;hpb=8d51c4a1a5a75e8e660f85cce37efcdf993500af;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git
diff --git a/lib/rbot/core/utils/extends.rb b/lib/rbot/core/utils/extends.rb
index 95569b71..7b733994 100644
--- a/lib/rbot/core/utils/extends.rb
+++ b/lib/rbot/core/utils/extends.rb
@@ -13,6 +13,30 @@
# Please note that global symbols have to be prefixed by :: because this plugin
# will be read into an anonymous module
+# Extensions to the Module class
+#
+class ::Module
+
+ # Many plugins define Struct objects to hold their data. On rescans, lots of
+ # warnings are echoed because of the redefinitions. Using this method solves
+ # the problem, by checking if the Struct already exists, and if it has the
+ # same attributes
+ #
+ def define_structure(name, *members)
+ sym = name.to_sym
+ if Struct.const_defined?(sym)
+ kl = Struct.const_get(sym)
+ if kl.new.members.map { |member| member.intern } == members.map
+ debug "Struct #{sym} previously defined, skipping"
+ const_set(sym, kl)
+ return
+ end
+ end
+ debug "Defining struct #{sym} with members #{members.inspect}"
+ const_set(sym, Struct.new(name.to_s, *members))
+ end
+end
+
# Extensions to the Array class
#
@@ -27,62 +51,49 @@ class ::Array
end
end
-begin
- require 'iconv'
- $we_have_iconv = true
-rescue LoadError
- $we_have_iconv = false
-end
-
-# Extensions to the String class
-#
-# TODO make ircify_html() accept an Hash of options, and make riphtml() just
-# call ircify_html() with stronger purify options.
+# Extensions to the Range class
#
-class ::String
+class ::Range
- # This method will try to transcode a String supposed to hold an XML or HTML
- # document from the original charset to UTF-8.
+ # This method returns a random number between the lower and upper bound
#
- # To find the original encoding, it will first see if the String responds to
- # #http_headers(), and if it does it will assume that the charset indicated
- # there is the correct one. Otherwise, it will try to detect the charset from
- # some typical XML and HTML headers
- def utfy_xml
- return self unless $we_have_iconv
-
- charset = nil
-
- if self.respond_to?(:http_headers) and headers = self.http_headers
- if headers['content-type'].first.match(/charset="?(\S+?)"?\s*;?/i)
- charset = $1
- end
- end
-
- if not charset
- case self
- when /<\?xml.*encoding="(\S+)".*\?>/i
- charset = $1
- when / u
+ return self
end
+end
+
+# Extensions to the String class
+#
+# TODO make riphtml() just call ircify_html() with stronger purify options.
+#
+class ::String
# This method will return a purified version of the receiver, with all HTML
# stripped off and some of it converted to IRC formatting
#
- def ircify_html
- txt = self
+ def ircify_html(opts={})
+ txt = self.dup
# remove scripts
txt.gsub!(/