]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - lib/rbot/core/utils/extends.rb
filters: DataStream can now be initialized with a String followed by a Hash
[user/henk/code/ruby/rbot.git] / lib / rbot / core / utils / extends.rb
index 80bad3836b8725b0b6fac45c8f57e4bc49389435..f0b713ba700c7fbe877e9907875453640c465fee 100644 (file)
@@ -38,6 +38,50 @@ class ::Module
 end
 
 
+# DottedIndex mixin: extend a Hash or Array class with this module
+# to achieve [] and []= methods that automatically split indices
+# at dots (indices are automatically converted to symbols, too)
+#
+# You have to define the single_retrieve(_key_) and
+# single_assign(_key_,_value_) methods (usually aliased at the
+# original :[] and :[]= methods)
+#
+module ::DottedIndex
+  def rbot_index_split(*ar)
+    keys = ([] << ar).flatten
+    keys.map! { |k|
+      k.to_s.split('.').map { |kk| kk.to_sym rescue nil }.compact
+    }.flatten
+  end
+
+  def [](*ar)
+    keys = self.rbot_index_split(ar)
+    return self.single_retrieve(keys.first) if keys.length == 1
+    h = self
+    while keys.length > 1
+      k = keys.shift
+      h[k] ||= self.class.new
+      h = h[k]
+    end
+    h[keys.last]
+  end
+
+  def []=(*arr)
+    val = arr.last
+    ar = arr[0..-2]
+    keys = self.rbot_index_split(ar)
+    return self.single_assign(keys.first, val) if keys.length == 1
+    h = self
+    while keys.length > 1
+      k = keys.shift
+      h[k] ||= self.class.new
+      h = h[k]
+    end
+    h[keys.last] = val
+  end
+end
+
+
 # Extensions to the Array class
 #
 class ::Array
@@ -49,6 +93,14 @@ class ::Array
     return nil if self.empty?
     self[rand(self.length)]
   end
+
+  # This method returns a random element from the array, deleting it from the
+  # array itself. The method returns nil if the array is empty
+  #
+  def delete_one
+    return nil if self.empty?
+    self.delete_at(rand(self.length))
+  end
 end
 
 # Extensions to the Range class
@@ -123,7 +175,7 @@ class ::String
         str + ": " + link
       }
     else
-      warn "unknown :a_href option #{val} passed to ircify_html" if val
+      warning "unknown :a_href option #{val} passed to ircify_html" if val
     end
 
     # Paragraph and br tags are converted to whitespace
@@ -137,6 +189,10 @@ class ::String
     txt.gsub!(/<sub>(.*?)<\/sub>/, '_{\1}')
     txt.gsub!(/(^|_)\{(.)\}/, '\1\2')
 
+    # List items are converted to *). We don't have special support for
+    # nested or ordered lists.
+    txt.gsub!(/<li>/, ' *) ')
+
     # All other tags are just removed
     txt.gsub!(/<[^>]+>/, '')
 
@@ -144,6 +200,14 @@ class ::String
     # such as &nbsp;
     txt = Utils.decode_html_entities(txt)
 
+    # Keep unbreakable spaces or conver them to plain spaces?
+    case val = opts[:nbsp]
+    when :space, ' '
+      txt.gsub!([160].pack('U'), ' ')
+    else
+      warning "unknown :nbsp option #{val} passed to ircify_html" if val
+    end
+
     # Remove double formatting options, since they only waste bytes
     txt.gsub!(/#{Bold}(\s*)#{Bold}/, '\1')
     txt.gsub!(/#{Underline}(\s*)#{Underline}/, '\1')
@@ -178,6 +242,23 @@ class ::String
   def riphtml
     self.gsub(/<[^>]+>/, '').gsub(/&amp;/,'&').gsub(/&quot;/,'"').gsub(/&lt;/,'<').gsub(/&gt;/,'>').gsub(/&ellip;/,'...').gsub(/&apos;/, "'").gsub("\n",'')
   end
+
+  # This method tries to find an HTML title in the string,
+  # and returns it if found
+  def get_html_title
+    if defined? ::Hpricot
+      Hpricot(self).at("title").inner_html
+    else
+      return unless Irc::Utils::TITLE_REGEX.match(self)
+      $1
+    end
+  end
+
+  # This method returns the IRC-formatted version of an
+  # HTML title found in the string
+  def ircify_html_title
+    self.get_html_title.ircify_html rescue nil
+  end
 end