]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - lib/rbot/core/utils/extends.rb
HttpUtil: require 'cgi' as it is now used in most querying plugins
[user/henk/code/ruby/rbot.git] / lib / rbot / core / utils / extends.rb
index 7022fb9123ac17b2d3df543e0d0f66002bbea342..a749f92adcd64ae7a14a3d466a10ce3a34cde5ae 100644 (file)
@@ -27,6 +27,23 @@ class ::Array
   end
 end
 
+# Extensions for the Numeric classes
+#
+class ::Numeric
+
+  # This method forces a real number to be not more than a given positive
+  # number or not less than a given positive number, or between two any given
+  # numbers
+  #
+  def clip(left,right=0)
+    raise ArgumentError unless left.kind_of?(Numeric) and right.kind_of?(Numeric)
+    l = [left,right].min
+    u = [left,right].max
+    return l if self < l
+    return u if self > u
+    return self
+  end
+end
 
 # Extensions to the String class
 #
@@ -38,24 +55,51 @@ 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!(/<script(?:\s+[^>]*)?>.*?<\/script>/im, "")
+
+    # remove styles
+    txt.gsub!(/<style(?:\s+[^>]*)?>.*?<\/style>/im, "")
 
     # bold and strong -> bold
-    txt.gsub!(/<\/?(?:b|strong)\s*>/, "#{Bold}")
+    txt.gsub!(/<\/?(?:b|strong)(?:\s+[^>]*)?>/im, "#{Bold}")
 
     # italic, emphasis and underline -> underline
-    txt.gsub!(/<\/?(?:i|em|u)\s*>/, "#{Underline}")
+    txt.gsub!(/<\/?(?:i|em|u)(?:\s+[^>]*)?>/im, "#{Underline}")
 
     ## 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*>/, ' ')
+    txt.gsub!(/<\/?(p|br)(?:\s+[^>]*)?\s*\/?\s*>/i, ' ')
     txt.gsub!("\n", ' ')
     txt.gsub!("\r", ' ')
 
+    # Superscripts and subscripts are turned into ^{...} and _{...}
+    # where the {} are omitted for single characters
+    txt.gsub!(/<sup>(.*?)<\/sup>/, '^{\1}')
+    txt.gsub!(/<sub>(.*?)<\/sub>/, '_{\1}')
+    txt.gsub!(/(^|_)\{(.)\}/, '\1\2')
+
     # All other tags are just removed
     txt.gsub!(/<[^>]+>/, '')
 
@@ -79,6 +123,14 @@ class ::String
     return txt.strip
   end
 
+  # As above, but modify the receiver
+  #
+  def ircify_html!(opts={})
+    old_hash = self.hash
+    replace self.ircify_html(opts)
+    return self unless self.hash == old_hash
+  end
+
   # This method will strip all HTML crud from the receiver
   #
   def riphtml