]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - lib/rbot/core/utils/extends.rb
Module\#define_structure method: define a new Struct only if doesn't exist already...
[user/henk/code/ruby/rbot.git] / lib / rbot / core / utils / extends.rb
index 950ad678b8cd92a45429a39e1924b51e3cd2d6cf..1aa6d457a00e4f48fa7ad464128921cf6c0bfe32 100644 (file)
 # 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,6 +51,20 @@ class ::Array
   end
 end
 
+# Extensions to the Range class
+#
+class ::Range
+
+  # This method returns a random number between the lower and upper bound
+  #
+  def pick_one
+    len = self.last - self.first
+    len += 1 unless self.exclude_end?
+    self.first + Kernel::rand(len)
+  end
+  alias :rand :pick_one
+end
+
 # Extensions for the Numeric classes
 #
 class ::Numeric
@@ -55,7 +93,7 @@ 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
+  def ircify_html(opts={})
     txt = self.dup
 
     # remove scripts
@@ -73,6 +111,21 @@ class ::String
     ## This would be a nice addition, but the results are horrible
     ## Maybe make it configurable?
     # txt.gsub!(/<\/?a( [^>]*)?>/, "#{Reverse}")
+    case val = opts[:a_href]
+    when Reverse, Bold, Underline
+      txt.gsub!(/<(?:\/a\s*|a (?:[^>]*\s+)?href\s*=\s*(?:[^>]*\s*)?)>/, val)
+    when :link_out
+      # Not good for nested links, but the best we can do without something like hpricot
+      txt.gsub!(/<a (?:[^>]*\s+)?href\s*=\s*(?:([^"'>][^\s>]*)\s+|"((?:[^"]|\\")*)"|'((?:[^']|\\')*)')(?:[^>]*\s+)?>(.*?)<\/a>/) { |match|
+        debug match
+        debug [$1, $2, $3, $4].inspect
+        link = $1 || $2 || $3
+        str = $4
+        str + ": " + link
+      }
+    else
+      warn "unknown :a_href option #{val} passed to ircify_html" if val
+    end
 
     # Paragraph and br tags are converted to whitespace
     txt.gsub!(/<\/?(p|br)(?:\s+[^>]*)?\s*\/?\s*>/i, ' ')
@@ -110,9 +163,9 @@ class ::String
 
   # As above, but modify the receiver
   #
-  def ircify_html!
+  def ircify_html!(opts={})
     old_hash = self.hash
-    replace self.ircify_html
+    replace self.ircify_html(opts)
     return self unless self.hash == old_hash
   end