1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
From: Antonio Terceiro <terceiro@debian.org>
Date: Thu, 24 Oct 2024 11:38:41 -0300
Subject: Fix handling of ANSI codes with no background color
Assuming that \e[32m is the same as \e[0;32m
---
lib/ansi/bbcode.rb | 5 +++++
test/case_bbcode.rb | 9 +++++++++
2 files changed, 14 insertions(+)
diff --git a/lib/ansi/bbcode.rb b/lib/ansi/bbcode.rb
index 2d7124c..294f267 100644
--- a/lib/ansi/bbcode.rb
+++ b/lib/ansi/bbcode.rb
@@ -109,6 +109,11 @@ module ANSI
line.scan(/\e\[[0-9;]+m/).each do |seq|
ansiname = ANSINAME2CODE.invert["#{seq}"]
+ if !ansiname && seq =~ /^\e\[(\d+)m$/
+ newseq = "\e[0;#{$1}m"
+ ansiname = ANSINAME2CODE.invert[newseq]
+ end
+
## Pop last tag and form closing tag
if ansiname == "reset"
lasttag = tagstack.pop
diff --git a/test/case_bbcode.rb b/test/case_bbcode.rb
index daf15b8..995dddf 100644
--- a/test/case_bbcode.rb
+++ b/test/case_bbcode.rb
@@ -19,6 +19,15 @@ testcase ANSI::BBCode do
end
end
+ class_method :ansi_to_bbcode do
+ test "example from RSpec" do
+ str = "\e[32m.\e[0m\e[32m.\e[0m"
+ expected = "[COLOR=green].[/COLOR][COLOR=green].[/COLOR]\n"
+ out = ANSI::BBCode.ansi_to_bbcode(str)
+ out.assert == expected
+ end
+ end
+
class_method :ansi_to_html do
test do
str = "this is \e[0;31mred\e[0m, this is \e[1mbold\e[0m\n" +
|