summaryrefslogtreecommitdiff
path: root/lib/rbot
diff options
context:
space:
mode:
authorGiuseppe Bilotta <giuseppe.bilotta@gmail.com>2007-04-08 22:26:28 +0000
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>2007-04-08 22:26:28 +0000
commit6003bc06c9b692306753f49637edf8d18cfa56cd (patch)
tree14f0e4f151c38e72a4e365e2bc8d3a38e4dec137 /lib/rbot
parentfdab6548b5c7c71e4ce816635f152ad222db0836 (diff)
HTML IRCification: support options. currently only option is :a_href which can be either :link_out to show the actual link or any of Reverse, Bold, Underline to mark the link using the corresponding attribute
Diffstat (limited to 'lib/rbot')
-rw-r--r--lib/rbot/core/utils/extends.rb21
1 files changed, 18 insertions, 3 deletions
diff --git a/lib/rbot/core/utils/extends.rb b/lib/rbot/core/utils/extends.rb
index 950ad678..a749f92a 100644
--- a/lib/rbot/core/utils/extends.rb
+++ b/lib/rbot/core/utils/extends.rb
@@ -55,7 +55,7 @@ class ::String
# This method will return a purified version of the receiver, with all HTML
# stripped off and some of it converted to IRC formatting
#
- def ircify_html
+ def ircify_html(opts={})
txt = self.dup
# remove scripts
@@ -73,6 +73,21 @@ class ::String
## This would be a nice addition, but the results are horrible
## Maybe make it configurable?
# txt.gsub!(/<\/?a( [^>]*)?>/, "#{Reverse}")
+ case val = opts[:a_href]
+ when Reverse, Bold, Underline
+ txt.gsub!(/<(?:\/a\s*|a (?:[^>]*\s+)?href\s*=\s*(?:[^>]*\s*)?)>/, val)
+ when :link_out
+ # Not good for nested links, but the best we can do without something like hpricot
+ txt.gsub!(/<a (?:[^>]*\s+)?href\s*=\s*(?:([^"'>][^\s>]*)\s+|"((?:[^"]|\\")*)"|'((?:[^']|\\')*)')(?:[^>]*\s+)?>(.*?)<\/a>/) { |match|
+ debug match
+ debug [$1, $2, $3, $4].inspect
+ link = $1 || $2 || $3
+ str = $4
+ str + ": " + link
+ }
+ else
+ warn "unknown :a_href option #{val} passed to ircify_html" if val
+ end
# Paragraph and br tags are converted to whitespace
txt.gsub!(/<\/?(p|br)(?:\s+[^>]*)?\s*\/?\s*>/i, ' ')
@@ -110,9 +125,9 @@ class ::String
# As above, but modify the receiver
#
- def ircify_html!
+ def ircify_html!(opts={})
old_hash = self.hash
- replace self.ircify_html
+ replace self.ircify_html(opts)
return self unless self.hash == old_hash
end