]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - lib/rbot/core/utils/extends.rb
HTML IRCification: try to handle super and subscripts
[user/henk/code/ruby/rbot.git] / lib / rbot / core / utils / extends.rb
1 #-- vim:sw=2:et
2 #++
3 #
4 # :title: Standard classes extensions
5 #
6 # Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
7 # Copyright:: (C) 2006,2007 Giuseppe Bilotta
8 # License:: GPL v2
9 #
10 # This file collects extensions to standard Ruby classes and to some core rbot
11 # classes to be used by the various plugins
12 #
13 # Please note that global symbols have to be prefixed by :: because this plugin
14 # will be read into an anonymous module
15
16
17 # Extensions to the Array class
18 #
19 class ::Array
20
21   # This method returns a random element from the array, or nil if the array is
22   # empty
23   #
24   def pick_one
25     return nil if self.empty?
26     self[rand(self.length)]
27   end
28 end
29
30 # Extensions to the String class
31 #
32 # TODO make ircify_html() accept an Hash of options, and make riphtml() just
33 # call ircify_html() with stronger purify options.
34 #
35 class ::String
36
37   # This method will return a purified version of the receiver, with all HTML
38   # stripped off and some of it converted to IRC formatting
39   #
40   def ircify_html
41     txt = self
42
43     # remove scripts
44     txt.gsub!(/<script(?:\s+[^>]*)?>.*?<\/script>/im, "")
45
46     # remove styles
47     txt.gsub!(/<style(?:\s+[^>]*)?>.*?<\/style>/im, "")
48
49     # bold and strong -> bold
50     txt.gsub!(/<\/?(?:b|strong)(?:\s+[^>]*)?>/im, "#{Bold}")
51
52     # italic, emphasis and underline -> underline
53     txt.gsub!(/<\/?(?:i|em|u)(?:\s+[^>]*)?>/im, "#{Underline}")
54
55     ## This would be a nice addition, but the results are horrible
56     ## Maybe make it configurable?
57     # txt.gsub!(/<\/?a( [^>]*)?>/, "#{Reverse}")
58
59     # Paragraph and br tags are converted to whitespace
60     txt.gsub!(/<\/?(p|br)(?:\s+[^>]*)?\s*\/?\s*>/i, ' ')
61     txt.gsub!("\n", ' ')
62     txt.gsub!("\r", ' ')
63
64     # Superscripts and subscripts are turned into ^{...} and _{...}
65     # where the {} are omitted for single characters
66     txt.gsub!(/<sup>(.*?)<\/sup>/, '^{\1}')
67     txt.gsub!(/<sub>(.*?)<\/sub>/, '_{\1}')
68     txt.gsub!(/(^|_)\{(.)\}/, '\1\2')
69
70     # All other tags are just removed
71     txt.gsub!(/<[^>]+>/, '')
72
73     # Convert HTML entities. We do it now to be able to handle stuff
74     # such as &nbsp;
75     txt = Utils.decode_html_entities(txt)
76
77     # Remove double formatting options, since they only waste bytes
78     txt.gsub!(/#{Bold}(\s*)#{Bold}/, '\1')
79     txt.gsub!(/#{Underline}(\s*)#{Underline}/, '\1')
80
81     # Simplify whitespace that appears on both sides of a formatting option
82     txt.gsub!(/\s+(#{Bold}|#{Underline})\s+/, ' \1')
83     txt.sub!(/\s+(#{Bold}|#{Underline})\z/, '\1')
84     txt.sub!(/\A(#{Bold}|#{Underline})\s+/, '\1')
85
86     # And finally whitespace is squeezed
87     txt.gsub!(/\s+/, ' ')
88
89     # Decode entities and strip whitespace
90     return txt.strip
91   end
92
93   # This method will strip all HTML crud from the receiver
94   #
95   def riphtml
96     self.gsub(/<[^>]+>/, '').gsub(/&amp;/,'&').gsub(/&quot;/,'"').gsub(/&lt;/,'<').gsub(/&gt;/,'>').gsub(/&ellip;/,'...').gsub(/&apos;/, "'").gsub("\n",'')
97   end
98 end
99
100
101 # Extensions to the Regexp class, with some common and/or complex regular
102 # expressions.
103 #
104 class ::Regexp
105
106   # A method to build a regexp that matches a list of something separated by
107   # optional commas and/or the word "and", an optionally repeated prefix,
108   # and whitespace.
109   def Regexp.new_list(reg, pfx = "")
110     if pfx.kind_of?(String) and pfx.empty?
111       return %r(#{reg}(?:,?(?:\s+and)?\s+#{reg})*)
112     else
113       return %r(#{reg}(?:,?(?:\s+and)?(?:\s+#{pfx})?\s+#{reg})*)
114     end
115   end
116
117   IN_ON = /in|on/
118
119   module Irc
120     # Match a list of channel anmes separated by optional commas, whitespace
121     # and optionally the word "and"
122     CHAN_LIST = Regexp.new_list(GEN_CHAN)
123
124     # Match "in #channel" or "on #channel" and/or "in private" (optionally
125     # shortened to "in pvt"), returning the channel name or the word 'private'
126     # or 'pvt' as capture
127     IN_CHAN = /#{IN_ON}\s+(#{GEN_CHAN})|(here)|/
128     IN_CHAN_PVT = /#{IN_CHAN}|in\s+(private|pvt)/
129
130     # As above, but with channel lists
131     IN_CHAN_LIST_SFX = Regexp.new_list(/#{GEN_CHAN}|here/, IN_ON)
132     IN_CHAN_LIST = /#{IN_ON}\s+#{IN_CHAN_LIST_SFX}|anywhere|everywhere/
133     IN_CHAN_LIST_PVT_SFX = Regexp.new_list(/#{GEN_CHAN}|here|private|pvt/, IN_ON)
134     IN_CHAN_LIST_PVT = /#{IN_ON}\s+#{IN_CHAN_LIST_PVT_SFX}|anywhere|everywhere/
135
136     # Match a list of nicknames separated by optional commas, whitespace and
137     # optionally the word "and"
138     NICK_LIST = Regexp.new_list(GEN_NICK)
139
140   end
141
142 end
143
144
145 module ::Irc
146
147
148   class BasicUserMessage
149
150     # We extend the BasicUserMessage class with a method that parses a string
151     # which is a channel list as matched by IN_CHAN(_LIST) and co. The method
152     # returns an array of channel names, where 'private' or 'pvt' is replaced
153     # by the Symbol :"?", 'here' is replaced by the channel of the message or
154     # by :"?" (depending on whether the message target is the bot or a
155     # Channel), and 'anywhere' and 'everywhere' are replaced by Symbol :*
156     #
157     def parse_channel_list(string)
158       return [:*] if [:anywhere, :everywhere].include? string.to_sym
159       string.scan(
160       /(?:^|,?(?:\s+and)?\s+)(?:in|on\s+)?(#{Regexp::Irc::GEN_CHAN}|here|private|pvt)/
161                  ).map { |chan_ar|
162         chan = chan_ar.first
163         case chan.to_sym
164         when :private, :pvt
165           :"?"
166         when :here
167           case self.target
168           when Channel
169             self.target.name
170           else
171             :"?"
172           end
173         else
174           chan
175         end
176       }.uniq
177     end
178   end
179 end