4 # Extensions to standard classes, to be used by the various plugins
5 # Please note that global symbols have to be prefixed by :: because this plugin
6 # will be read into an anonymous module
8 # Extensions to the Array class
12 # This method returns a random element from the array, or nil if the array is
16 return nil if self.empty?
17 self[rand(self.length)]
21 # Extensions to the String class
23 # TODO make ircify_html() accept an Hash of options, and make riphtml() just
24 # call ircify_html() with stronger purify options.
28 # This method will return a purified version of the receiver, with all HTML
29 # stripped off and some of it converted to IRC formatting
34 # bold and strong -> bold
35 txt.gsub!(/<\/?(?:b|strong)\s*>/, "#{Bold}")
37 # italic, emphasis and underline -> underline
38 txt.gsub!(/<\/?(?:i|em|u)\s*>/, "#{Underline}")
40 ## This would be a nice addition, but the results are horrible
41 ## Maybe make it configurable?
42 # txt.gsub!(/<\/?a( [^>]*)?>/, "#{Reverse}")
44 # Paragraph and br tags are converted to whitespace.
45 txt.gsub!(/<\/?(p|br)\s*\/?\s*>/, ' ')
48 # All other tags are just removed
49 txt.gsub!(/<[^>]+>/, '')
51 # Remove double formatting options, since they only waste bytes
52 txt.gsub!(/#{Bold}(\s*)#{Bold}/, '\1')
53 txt.gsub!(/#{Underline}(\s*)#{Underline}/, '\1')
55 # And finally whitespace is squeezed
58 # Decode entities and strip whitespace
59 return Utils.decode_html_entities(txt).strip!
62 # This method will strip all HTML crud from the receiver
65 self.gsub(/<[^>]+>/, '').gsub(/&/,'&').gsub(/"/,'"').gsub(/</,'<').gsub(/>/,'>').gsub(/&ellip;/,'...').gsub(/'/, "'").gsub("\n",'')