def BBCode.bbcode_to_ansi(string, usecolors = true)
return "" if string.nil? || string.to_s.strip.empty?
result = ""
return BBCode.strip_bbcode(string) if !usecolors
string.split("\n").each do |line|
ANSINAME2BBCODE.each do |key,val|
line.gsub!(/\[#{val}\]/, ANSINAME2CODE[key])
line.gsub!(/\[\/#{val}\]/, ANSINAME2CODE["reset"])
end
line.gsub!(/\[SIZE=\d\]/, "")
line.gsub!(/\[\/SIZE\]/, "")
line.gsub!(/\[FONT=[^\]]*\]/, "")
line.gsub!(/\[\/FONT\]/, "")
colors = line.scan(/\[COLOR=(.*?)\]/i)
colors = colors.collect{|s| s[0].to_s} if !colors.nil?
colors.each do |col|
name = BBCOLOR2ANSI[col.downcase]
name = BBCOLOR2ANSI["white"] if name.nil?
code = ANSINAME2CODE[name]
line.gsub!(/\[COLOR=#{col}\]/i, code)
end
line.gsub!(/\[\/COLOR\]/, ANSINAME2CODE["reset"])
result << sprintf("%s\n", line)
end
return result
end