diff --git a/lib/rdoc/markup/to_ansi.rb b/lib/rdoc/markup/to_ansi.rb index 518aef15a6..1f25638916 100644 --- a/lib/rdoc/markup/to_ansi.rb +++ b/lib/rdoc/markup/to_ansi.rb @@ -81,6 +81,10 @@ def accept_list_item_start(list_item) end end + def calculate_text_width(text) + text.gsub(/\e\[[\d;]*m/, '').size + end + ## # Starts accepting with a reset screen diff --git a/lib/rdoc/markup/to_bs.rb b/lib/rdoc/markup/to_bs.rb index d4e43dc89b..e6c8a48217 100644 --- a/lib/rdoc/markup/to_bs.rb +++ b/lib/rdoc/markup/to_bs.rb @@ -65,6 +65,10 @@ def accept_list_item_start(list_item) end end + def calculate_text_width(text) + text.gsub(/_\x08/, '').gsub(/\x08./, '').size + end + ## # Turns on or off regexp handling for +convert_string+ diff --git a/lib/rdoc/markup/to_rdoc.rb b/lib/rdoc/markup/to_rdoc.rb index 255c57e0d1..4185e8bea9 100644 --- a/lib/rdoc/markup/to_rdoc.rb +++ b/lib/rdoc/markup/to_rdoc.rb @@ -250,8 +250,10 @@ def accept_verbatim(verbatim) # Adds +table+ to the output def accept_table(header, body, aligns) + header = header.map { |h| attributes h } + body = body.map { |row| row.map { |t| attributes t } } widths = header.zip(*body).map do |cols| - cols.map(&:size).max + cols.map { |col| calculate_text_width(col) }.max end aligns = aligns.map do |a| case a @@ -264,16 +266,22 @@ def accept_table(header, body, aligns) end end @res << header.zip(widths, aligns).map do |h, w, a| - h.__send__(a, w) + extra_width = h.size - calculate_text_width(h) + h.__send__(a, w + extra_width) end.join("|").rstrip << "\n" @res << widths.map {|w| "-" * w }.join("|") << "\n" body.each do |row| @res << row.zip(widths, aligns).map do |t, w, a| - t.__send__(a, w) + extra_width = t.size - calculate_text_width(t) + t.__send__(a, w + extra_width) end.join("|").rstrip << "\n" end end + def calculate_text_width(text) + text.size + end + ## # Applies attribute-specific markup to +text+ using RDoc::AttributeManager diff --git a/test/rdoc/markup/to_ansi_test.rb b/test/rdoc/markup/to_ansi_test.rb index edfc9d5645..19556a0f67 100644 --- a/test/rdoc/markup/to_ansi_test.rb +++ b/test/rdoc/markup/to_ansi_test.rb @@ -352,9 +352,9 @@ def accept_table_align expected = "\e[0m" + <<-EXPECTED AA |BB |CCCCC|DDDDD ----|---|-----|----- - |bbb| c| + |bbb| \e[1mc\e[m| aaaa|b | | dd - a | | cc| dd + a | | cc| \e[7mdd\e[m EXPECTED assert_equal expected, @to.end_accepting end diff --git a/test/rdoc/markup/to_bs_test.rb b/test/rdoc/markup/to_bs_test.rb index bbdcfc164d..8456225598 100644 --- a/test/rdoc/markup/to_bs_test.rb +++ b/test/rdoc/markup/to_bs_test.rb @@ -353,7 +353,7 @@ def accept_table_align expected = <<-EXPECTED AA |BB |CCCCC|DDDDD ----|---|-----|----- - |bbb| c| + |bbb| c\bc| aaaa|b | | dd a | | cc| dd EXPECTED diff --git a/test/rdoc/markup/to_markdown_test.rb b/test/rdoc/markup/to_markdown_test.rb index 2dcab59616..b02c0251ac 100644 --- a/test/rdoc/markup/to_markdown_test.rb +++ b/test/rdoc/markup/to_markdown_test.rb @@ -350,9 +350,9 @@ def accept_table_align expected = <<-EXPECTED AA |BB |CCCCC|DDDDD ----|---|-----|----- - |bbb| c| + |bbb|**c**| aaaa|b | | dd - a | | cc| dd + a | | cc|`dd` EXPECTED assert_equal expected, @to.end_accepting end diff --git a/test/rdoc/markup/to_rdoc_test.rb b/test/rdoc/markup/to_rdoc_test.rb index 4f44e57d34..1ee6e0b26a 100644 --- a/test/rdoc/markup/to_rdoc_test.rb +++ b/test/rdoc/markup/to_rdoc_test.rb @@ -348,11 +348,11 @@ def list_verbatim def accept_table_align expected = <<-EXPECTED - AA |BB |CCCCC|DDDDD -----|---|-----|----- - |bbb| c| -aaaa|b | | dd - a | | cc| dd + AA |BB | CCCCC| DDDDD +----|---|--------|----------- + |bbb|c| +aaaa|b | | dd + a | | cc|dd EXPECTED assert_equal expected, @to.end_accepting end diff --git a/test/rdoc/support/text_formatter_test_case.rb b/test/rdoc/support/text_formatter_test_case.rb index c45078ce46..8da14f42f3 100644 --- a/test/rdoc/support/text_formatter_test_case.rb +++ b/test/rdoc/support/text_formatter_test_case.rb @@ -105,9 +105,9 @@ def test_accept_paragraph_wrap def test_accept_table_align header = ['AA', 'BB', 'CCCCC', 'DDDDD'] body = [ - ['', 'bbb', 'c', ''], + ['', 'bbb', 'c', ''], ['aaaa', 'b', '', 'dd'], - ['a', '', 'cc', 'dd'] + ['a', '', 'cc', 'dd'] ] aligns = [nil, :left, :right, :center] @to.start_accepting