From 917fe414e18be8ae20c1296d81baf20fdc64e2f5 Mon Sep 17 00:00:00 2001 From: ksss Date: Wed, 3 Dec 2025 15:20:50 +0900 Subject: [PATCH 1/2] Apply markup in table cell --- lib/rdoc/markup/to_rdoc.rb | 5 +++++ test/rdoc/markup/to_ansi_test.rb | 8 ++++---- test/rdoc/markup/to_markdown_test.rb | 2 +- test/rdoc/markup/to_rdoc_test.rb | 8 ++++---- test/rdoc/support/text_formatter_test_case.rb | 2 +- 5 files changed, 15 insertions(+), 10 deletions(-) diff --git a/lib/rdoc/markup/to_rdoc.rb b/lib/rdoc/markup/to_rdoc.rb index 255c57e0d1..358a0f11ea 100644 --- a/lib/rdoc/markup/to_rdoc.rb +++ b/lib/rdoc/markup/to_rdoc.rb @@ -250,6 +250,11 @@ def accept_verbatim(verbatim) # Adds +table+ to the output def accept_table(header, body, aligns) + body = body.map do |row| + row.map do |cell| + attributes cell + end + end widths = header.zip(*body).map do |cols| cols.map(&:size).max end diff --git a/test/rdoc/markup/to_ansi_test.rb b/test/rdoc/markup/to_ansi_test.rb index edfc9d5645..725990274b 100644 --- a/test/rdoc/markup/to_ansi_test.rb +++ b/test/rdoc/markup/to_ansi_test.rb @@ -350,11 +350,11 @@ def list_verbatim def accept_table_align expected = "\e[0m" + <<-EXPECTED - AA |BB |CCCCC|DDDDD -----|---|-----|----- + AA |BB |CCCCC| DDDDD +----|---|-----|--------- |bbb| c| -aaaa|b | | dd - a | | cc| dd +aaaa|b | | dd + a | | cc|\e[7mdd\e[m EXPECTED assert_equal expected, @to.end_accepting end diff --git a/test/rdoc/markup/to_markdown_test.rb b/test/rdoc/markup/to_markdown_test.rb index 2dcab59616..1052561c00 100644 --- a/test/rdoc/markup/to_markdown_test.rb +++ b/test/rdoc/markup/to_markdown_test.rb @@ -352,7 +352,7 @@ def accept_table_align ----|---|-----|----- |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..3f9c26e166 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 -----|---|-----|----- + AA |BB |CCCCC| DDDDD +----|---|-----|----------- |bbb| c| -aaaa|b | | dd - a | | cc| dd +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..fde2f7bd38 100644 --- a/test/rdoc/support/text_formatter_test_case.rb +++ b/test/rdoc/support/text_formatter_test_case.rb @@ -107,7 +107,7 @@ def test_accept_table_align body = [ ['', 'bbb', 'c', ''], ['aaaa', 'b', '', 'dd'], - ['a', '', 'cc', 'dd'] + ['a', '', 'cc', 'dd'] ] aligns = [nil, :left, :right, :center] @to.start_accepting From 45a81809065d604ee179736d4b94fba7c0365152 Mon Sep 17 00:00:00 2001 From: ksss Date: Fri, 5 Dec 2025 10:05:17 +0900 Subject: [PATCH 2/2] Adjust table width for each markup format --- lib/rdoc/markup/to_ansi.rb | 4 ++++ lib/rdoc/markup/to_bs.rb | 4 ++++ lib/rdoc/markup/to_rdoc.rb | 19 +++++++++++-------- test/rdoc/markup/to_ansi_test.rb | 10 +++++----- test/rdoc/markup/to_bs_test.rb | 2 +- test/rdoc/markup/to_markdown_test.rb | 2 +- test/rdoc/markup/to_rdoc_test.rb | 10 +++++----- test/rdoc/support/text_formatter_test_case.rb | 2 +- 8 files changed, 32 insertions(+), 21 deletions(-) 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 358a0f11ea..4185e8bea9 100644 --- a/lib/rdoc/markup/to_rdoc.rb +++ b/lib/rdoc/markup/to_rdoc.rb @@ -250,13 +250,10 @@ def accept_verbatim(verbatim) # Adds +table+ to the output def accept_table(header, body, aligns) - body = body.map do |row| - row.map do |cell| - attributes cell - end - end + 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 @@ -269,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 725990274b..19556a0f67 100644 --- a/test/rdoc/markup/to_ansi_test.rb +++ b/test/rdoc/markup/to_ansi_test.rb @@ -350,11 +350,11 @@ def list_verbatim def accept_table_align expected = "\e[0m" + <<-EXPECTED - AA |BB |CCCCC| DDDDD -----|---|-----|--------- - |bbb| c| -aaaa|b | | dd - a | | cc|\e[7mdd\e[m + AA |BB |CCCCC|DDDDD +----|---|-----|----- + |bbb| \e[1mc\e[m| +aaaa|b | | 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 1052561c00..b02c0251ac 100644 --- a/test/rdoc/markup/to_markdown_test.rb +++ b/test/rdoc/markup/to_markdown_test.rb @@ -350,7 +350,7 @@ def accept_table_align expected = <<-EXPECTED AA |BB |CCCCC|DDDDD ----|---|-----|----- - |bbb| c| + |bbb|**c**| aaaa|b | | dd a | | cc|`dd` EXPECTED diff --git a/test/rdoc/markup/to_rdoc_test.rb b/test/rdoc/markup/to_rdoc_test.rb index 3f9c26e166..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 fde2f7bd38..8da14f42f3 100644 --- a/test/rdoc/support/text_formatter_test_case.rb +++ b/test/rdoc/support/text_formatter_test_case.rb @@ -105,7 +105,7 @@ 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'] ]