]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - lib/rbot/core/utils/extends.rb
first_html_par: make Hpricot handling more robust
[user/henk/code/ruby/rbot.git] / lib / rbot / core / utils / extends.rb
index 31a348a94ff8594e756a5b29cc55c4d5bb7551a7..4e5459d850b2493e8435b6b4f545ce836b1c9f4d 100644 (file)
@@ -144,6 +144,14 @@ class ::String
     # such as  
     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 +186,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
 
 
@@ -227,79 +252,6 @@ end
 
 module ::Irc
 
-  # Define standard IRC attriubtes (not so standard actually,
-  # but the closest thing we have ...)
-  Bold = "\002"
-  Underline = "\037"
-  Reverse = "\026"
-  Italic = "\011"
-  NormalText = "\017"
-
-  # Color is prefixed by \003 and followed by optional
-  # foreground and background specifications, two-digits-max
-  # numbers separated by a comma. One of the two parts
-  # must be present.
-  Color = "\003"
-  ColorRx = /#{Color}\d?\d?(?:,\d\d?)?/
-
-  # Standard color codes
-  ColorCode = {
-    :black      => 1,
-    :blue       => 2,
-    :navyblue   => 2,
-    :navy_blue  => 2,
-    :green      => 3,
-    :red        => 4,
-    :brown      => 5,
-    :purple     => 6,
-    :olive      => 7,
-    :yellow     => 8,
-    :limegreen  => 9,
-    :lime_green => 9,
-    :teal       => 10,
-    :aqualight  => 11,
-    :aqua_light => 11,
-    :royal_blue => 12,
-    :hotpink    => 13,
-    :hot_pink   => 13,
-    :darkgray   => 14,
-    :dark_gray  => 14,
-    :lightgray  => 15,
-    :light_gray => 15,
-    :white      => 16
-  }
-
-  # Convert a String or Symbol into a color number
-  def Irc.find_color(data)
-    if Integer === data
-      data
-    else
-      f = if String === data
-            data.intern
-          else
-            data
-          end
-      if ColorCode.key?(f)
-        ColorCode[f] 
-      else
-        0
-      end
-    end
-  end
-
-  # Insert the full color code for a given
-  # foreground/background combination.
-  def Irc.color(fg=nil,bg=nil)
-    str = Color.dup
-    if fg
-     str << Irc.find_color(fg).to_s
-    end
-    if bg
-      str << "," << Irc.find_color(bg).to_s
-    end
-    return str
-  end
-
 
   class BasicUserMessage