end
+# DottedIndex mixin: extend a Hash or Array class with this module
+# to achieve [] and []= methods that automatically split indices
+# at dots (indices are automatically converted to symbols, too)
+#
+# You have to define the single_retrieve(_key_) and
+# single_assign(_key_,_value_) methods (usually aliased at the
+# original :[] and :[]= methods)
+#
+module ::DottedIndex
+ def rbot_index_split(*ar)
+ keys = ([] << ar).flatten
+ keys.map! { |k|
+ k.to_s.split('.').map { |kk| kk.to_sym rescue nil }.compact
+ }.flatten
+ end
+
+ def [](*ar)
+ keys = self.rbot_index_split(ar)
+ return self.single_retrieve(keys.first) if keys.length == 1
+ h = self
+ while keys.length > 1
+ k = keys.shift
+ h[k] ||= self.class.new
+ h = h[k]
+ end
+ h[keys.last]
+ end
+
+ def []=(*arr)
+ val = arr.last
+ ar = arr[0..-2]
+ keys = self.rbot_index_split(ar)
+ return self.single_assign(keys.first, val) if keys.length == 1
+ h = self
+ while keys.length > 1
+ k = keys.shift
+ h[k] ||= self.class.new
+ h = h[k]
+ end
+ h[keys.last] = val
+ end
+end
+
+
# Extensions to the Array class
#
class ::Array
return nil if self.empty?
self[rand(self.length)]
end
+
+ # This method returns a random element from the array, deleting it from the
+ # array itself. The method returns nil if the array is empty
+ #
+ def delete_one
+ return nil if self.empty?
+ self.delete_at(rand(self.length))
+ end
end
# Extensions to the Range class
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
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!(/<[^>]+>/, '')
# 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')
def riphtml
self.gsub(/<[^>]+>/, '').gsub(/&/,'&').gsub(/"/,'"').gsub(/</,'<').gsub(/>/,'>').gsub(/&ellip;/,'...').gsub(/'/, "'").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