]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - lib/rbot/core/utils/extends.rb
httputil: try to guess content-type from extension if it's not defined
[user/henk/code/ruby/rbot.git] / lib / rbot / core / utils / extends.rb
index 1aa6d457a00e4f48fa7ad464128921cf6c0bfe32..7b733994b74d323b010db0bc2ee6d66dad1f598d 100644 (file)
@@ -85,8 +85,7 @@ 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.
+# TODO make riphtml() just call ircify_html() with stronger purify options.
 #
 class ::String
 
@@ -124,7 +123,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
@@ -138,6 +137,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!(/<[^>]+>/, '')
 
@@ -145,6 +148,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')
@@ -156,9 +167,14 @@ class ::String
 
     # And finally whitespace is squeezed
     txt.gsub!(/\s+/, ' ')
+    txt.strip!
+
+    if opts[:limit] && txt.size > opts[:limit]
+      txt = txt.slice(0, opts[:limit]) + "#{Reverse}...#{Reverse}"
+    end
 
     # Decode entities and strip whitespace
-    return txt.strip
+    return txt
   end
 
   # As above, but modify the receiver
@@ -174,6 +190,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