# Please note that global symbols have to be prefixed by :: because this plugin
# will be read into an anonymous module
+# Extensions to the Module class
+#
+class ::Module
+
+ # Many plugins define Struct objects to hold their data. On rescans, lots of
+ # warnings are echoed because of the redefinitions. Using this method solves
+ # the problem, by checking if the Struct already exists, and if it has the
+ # same attributes
+ #
+ def define_structure(name, *members)
+ sym = name.to_sym
+ if Struct.const_defined?(sym)
+ kl = Struct.const_get(sym)
+ if kl.new.members.map { |member| member.intern } == members.map
+ debug "Struct #{sym} previously defined, skipping"
+ const_set(sym, kl)
+ return
+ end
+ end
+ debug "Defining struct #{sym} with members #{members.inspect}"
+ const_set(sym, Struct.new(name.to_s, *members))
+ end
+end
+
# Extensions to the Array class
#
# 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
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')
# 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
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