def BBCode.ansi_to_bbcode(string)
return "" if string.nil? || string.to_s.strip.empty?
result = ""
tagstack = []
string.split("\n").each do |line|
ansi = line.scan(/\e\[[0-9;]+m/)
continue if ansi.nil? || ansi.empty?
ansi.each do |seq|
ansiname = ANSINAME2CODE.invert["#{seq}"]
if ansiname == "reset"
lasttag = tagstack.pop
bbname = "/" + String.new( lasttag.split("=")[0] )
else
bbname = ANSINAME2BBCODE[ansiname]
tagstack.push(bbname)
end
replace = sprintf("[%s]", bbname)
line.sub!("#{Regexp.quote(seq)}", replace)
end
result << sprintf("%s\n", line)
end
while !tagstack.empty?
result << sprintf("[/%s]", String.new(tagstack.pop.split("=")[0]) )
end
return result
end