]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/commitdiff
Move extensions to standard classes into a specific extends.rb util module
authorGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Tue, 6 Feb 2007 14:31:26 +0000 (14:31 +0000)
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Tue, 6 Feb 2007 14:31:26 +0000 (14:31 +0000)
data/rbot/plugins/rss.rb
data/rbot/plugins/salut.rb
data/rbot/plugins/search.rb
lib/rbot/core/utils/extends.rb [new file with mode: 0644]

index 08b2c775761571136807e28067f54b0b729b0557..acebda9cc0d030d48afee4ea63c7fda2dac8c13a 100644 (file)
@@ -19,12 +19,6 @@ require 'rss/dublincore'
 #   warning "Unable to load RSS libraries, RSS plugin functionality crippled"\r
 # end\r
 \r
-class ::String\r
-  def riphtml\r
-    self.gsub(/<[^>]+>/, '').gsub(/&amp;/,'&').gsub(/&quot;/,'"').gsub(/&lt;/,'<').gsub(/&gt;/,'>').gsub(/&ellip;/,'...').gsub(/&apos;/, "'").gsub("\n",'')\r
-  end\r
-end\r
-\r
 class ::RssBlob\r
   attr_accessor :url\r
   attr_accessor :handle\r
index e4295a06655c84323fe9d6fb52a886701a5cfd75..b0bf67e7769fd47d0cc5ecf80d75bc93161e6383 100644 (file)
@@ -4,17 +4,6 @@
 # TODO *REMEMBER* to set @changed to true after edit\r
 # TODO or changes won't be saved\r
 \r
-unless Array.new.respond_to?(:pick_one)\r
-  debug "Defining the pick_one method for Array"\r
-  class ::Array\r
-    def pick_one\r
-      return nil if self.empty?\r
-      self[rand(self.length)]\r
-    end\r
-  end\r
-end\r
-\r
-\r
 class SalutPlugin < Plugin\r
   BotConfig.register BotConfigBooleanValue.new('salut.all_languages',\r
     :default => true, \r
index 27f9519c37b4d5e3bec5bbda080380526371bed9..6fb1959aeec31b76f87083c78260cbf02269519b 100644 (file)
@@ -4,39 +4,6 @@ Net::HTTP.version_1_2
 
 GOOGLE_WAP_LINK = /<a accesskey="(\d)" href=".*?u=(.*?)">(.*?)<\/a>/im
 
-class ::String
-  def ircify_html
-    txt = self
-
-    # bold and strong -> bold
-    txt.gsub!(/<\/?(?:b|strong)\s*>/, "#{Bold}")
-
-    # italic, emphasis and underline -> underline
-    txt.gsub!(/<\/?(?:i|em|u)\s*>/, "#{Underline}")
-
-    ## This would be a nice addition, but the results are horrible
-    ## Maybe make it configurable?
-    # txt.gsub!(/<\/?a( [^>]*)?>/, "#{Reverse}")
-
-    # Paragraph and br tags are converted to whitespace.
-    txt.gsub!(/<\/?(p|br)\s*\/?\s*>/, ' ')
-    txt.gsub!("\n", ' ')
-
-    # All other tags are just removed
-    txt.gsub!(/<[^>]+>/, '')
-
-    # Remove double formatting options, since they only waste bytes
-    txt.gsub!(/#{Bold}\s*#{Bold}/,"")
-    txt.gsub!(/#{Underline}\s*#{Underline}/,"")
-
-    # And finally whitespace is squeezed
-    txt.gsub!(/\s+/, ' ')
-
-    # Decode entities and strip whitespace
-    return Utils.decode_html_entities(txt).strip!
-  end
-end
-
 class SearchPlugin < Plugin
   BotConfig.register BotConfigIntegerValue.new('google.hits',
     :default => 3,
diff --git a/lib/rbot/core/utils/extends.rb b/lib/rbot/core/utils/extends.rb
new file mode 100644 (file)
index 0000000..b1da19c
--- /dev/null
@@ -0,0 +1,69 @@
+#-- vim:sw=2:et
+#++
+#
+# Extensions to standard classes, to be used by the various plugins
+# Please note that global symbols have to be prefixed by :: because this plugin
+# will be read into an anonymous module
+
+# Extensions to the Array class
+#
+class ::Array
+
+  # This method returns a random element from the array, or nil if the array is
+  # empty
+  #
+  def pick_one
+    return nil if self.empty?
+    self[rand(self.length)]
+  end
+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.
+#
+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
+
+    # bold and strong -> bold
+    txt.gsub!(/<\/?(?:b|strong)\s*>/, "#{Bold}")
+
+    # italic, emphasis and underline -> underline
+    txt.gsub!(/<\/?(?:i|em|u)\s*>/, "#{Underline}")
+
+    ## This would be a nice addition, but the results are horrible
+    ## Maybe make it configurable?
+    # txt.gsub!(/<\/?a( [^>]*)?>/, "#{Reverse}")
+
+    # Paragraph and br tags are converted to whitespace.
+    txt.gsub!(/<\/?(p|br)\s*\/?\s*>/, ' ')
+    txt.gsub!("\n", ' ')
+
+    # All other tags are just removed
+    txt.gsub!(/<[^>]+>/, '')
+
+    # Remove double formatting options, since they only waste bytes
+    txt.gsub!(/#{Bold}\s*#{Bold}/,"")
+    txt.gsub!(/#{Underline}\s*#{Underline}/,"")
+
+    # And finally whitespace is squeezed
+    txt.gsub!(/\s+/, ' ')
+
+    # Decode entities and strip whitespace
+    return Utils.decode_html_entities(txt).strip!
+  end
+
+  # This method will strip all HTML crud from the receiver
+  #
+  def riphtml
+    self.gsub(/<[^>]+>/, '').gsub(/&amp;/,'&').gsub(/&quot;/,'"').gsub(/&lt;/,'<').gsub(/&gt;/,'>').gsub(/&ellip;/,'...').gsub(/&apos;/, "'").gsub("\n",'')
+  end
+end
+
+