def BBCode.html_to_bbcode(string)
return "" if string.nil? || string.to_s.strip.empty?
result = ""
string.split(/<br *\/?>/i).each do |line|
styles = { "strong" => "b", "b" => "b",
"em" => "i", "i" => "i",
"u" => "u" }
styles.each do |html,code|
line.gsub!(/<#{html}>/i, "[#{code.upcase}]")
line.gsub!(/<\/#{html}>/i, "[/#{code.upcase}]")
end
line.gsub!(/<a +href *= *\"mailto:(.*?)\".*?>.*?<\/a>/i, "[EMAIL]\\1[/EMAIL]")
line.gsub!(/<a +href *= *\"((?:https?|ftp):\/\/.*?)\".*?>(.*?)<\/a>/i, "[URL=\\1]\\2[/URL]")
line.gsub!(/<a +href *= *\".*?\".*?>/i, "")
line.gsub!(/<\/a>/i, "")
line.gsub!(/<img +src *= *\"(.*?)\".*?\/?>/i, "[IMG=\\1]")
line.gsub!(/<center>/i, "[ALIGN=center]")
line.gsub!(/<\/center>/i, "[/ALIGN]")
line.gsub!(/<(?:xmp|pre)>/i, "[QUOTE]")
line.gsub!(/<\/(?:xmp|pre)>/i, "[/QUOTE]")
line.gsub!(/<ul>/i, "\n[LIST]\n")
line.gsub!(/<\/ul>/i, "\n[/LIST]\n")
line.gsub!(/<li *\/?> */i, "\n[*] ")
line.gsub!(/<.*? *\/?>/, "")
result << sprintf("%s<br />\n", line)
end
return result.gsub!(/<br *\/?>/i, "\n")
end