]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - lib/rbot/core/utils/extends.rb
Start implementing common regular expressions to be used by plugin map() requirements
[user/henk/code/ruby/rbot.git] / lib / rbot / core / utils / extends.rb
1 #-- vim:sw=2:et
2 #++
3 #
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
7
8 # Extensions to the Array class
9 #
10 class ::Array
11
12   # This method returns a random element from the array, or nil if the array is
13   # empty
14   #
15   def pick_one
16     return nil if self.empty?
17     self[rand(self.length)]
18   end
19 end
20
21 # Extensions to the String class
22 #
23 # TODO make ircify_html() accept an Hash of options, and make riphtml() just
24 # call ircify_html() with stronger purify options.
25 #
26 class ::String
27
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
30   #
31   def ircify_html
32     txt = self
33
34     # bold and strong -> bold
35     txt.gsub!(/<\/?(?:b|strong)\s*>/, "#{Bold}")
36
37     # italic, emphasis and underline -> underline
38     txt.gsub!(/<\/?(?:i|em|u)\s*>/, "#{Underline}")
39
40     ## This would be a nice addition, but the results are horrible
41     ## Maybe make it configurable?
42     # txt.gsub!(/<\/?a( [^>]*)?>/, "#{Reverse}")
43
44     # Paragraph and br tags are converted to whitespace.
45     txt.gsub!(/<\/?(p|br)\s*\/?\s*>/, ' ')
46     txt.gsub!("\n", ' ')
47
48     # All other tags are just removed
49     txt.gsub!(/<[^>]+>/, '')
50
51     # Remove double formatting options, since they only waste bytes
52     txt.gsub!(/#{Bold}(\s*)#{Bold}/, '\1')
53     txt.gsub!(/#{Underline}(\s*)#{Underline}/, '\1')
54
55     # And finally whitespace is squeezed
56     txt.gsub!(/\s+/, ' ')
57
58     # Decode entities and strip whitespace
59     return Utils.decode_html_entities(txt).strip!
60   end
61
62   # This method will strip all HTML crud from the receiver
63   #
64   def riphtml
65     self.gsub(/<[^>]+>/, '').gsub(/&amp;/,'&').gsub(/&quot;/,'"').gsub(/&lt;/,'<').gsub(/&gt;/,'>').gsub(/&ellip;/,'...').gsub(/&apos;/, "'").gsub("\n",'')
66   end
67 end
68
69 # Extensions to the Regexp class, with some commonly used regular expressions.
70 #
71 module ::Rx
72
73
74   # We start with some IRC related regular expressions, used to match
75   # Irc::User nicks and Irc::Channel names
76   #
77   # For each of them we define three versions of the regular expression:
78   #  * a generic one, which should match for any server but may turn out to match
79   #    more than a specific server would accept
80   #  * an RFC-compliant matcher
81   #  * TODO a server-specific one that uses the Irc::Server#supports method to build
82   #    a matcher valid for a particular server.
83   #
84   module Irc
85     CHAN_FIRST = /[#&+]/
86     CHAN_SAFE = /![A-Z0-9]{5}/
87     CHAN_ANY = /[^\x00\x07\x0A\x0D ,:]/
88     GENERIC_CHAN = /(?:#{CHAN_FIRST}|#{CHAN_SAFE})#{CHAN_ANY}+/
89     RFC_CHAN = /#{CHAN_FIRST}#{CHAN_ANY}{1,49}|#{CHAN_SAFE}#{CHAN_ANY}{1,44}/
90
91     SPECIAL_CHAR = /[\x5b-\x60\x7b-\x7d]/
92     NICK_FIRST = /#{SPECIAL_CHAR}|[[:alpha:]]/
93     NICK_ANY = /#{SPECIAL_CHAR}|[[:alnum:]]|-/
94     GENERIC_NICK = /#{NICK_FIRST}#{NICK_ANY}+/
95     RFC_NICK = /#{NICK_FIRST}#{NICK_ANY}{0,8}/
96   end
97
98
99   # Next, some general purpose ones
100   DIGITS = /[0-9]+/
101   HEX_DIGIT = /[0-9A-Za-z]/
102   HEX_DIGITS = /#{HEX_DIGIT}+/
103   HEX_OCTET = /#{HEX_DIGIT}#{HEX_DIGIT}?/
104   DEC_OCTET = /[01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5]/
105   DEC_IP = /#{DEC_OCTET}.#{DEC_OCTET}.#{DEC_OCTET}/
106   HEX_IP = /#{HEX_OCTET}.#{HEX_OCTET}.#{HEX_OCTET}/
107   IP = /#{DEC_IP}|#{HEX_IP}/
108
109 end
110
111