Skip to content

Commit 45a8180

Browse files
committed
Adjust table width for each markup format
1 parent 917fe41 commit 45a8180

File tree

8 files changed

+32
-21
lines changed

8 files changed

+32
-21
lines changed

lib/rdoc/markup/to_ansi.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ def accept_list_item_start(list_item)
8181
end
8282
end
8383

84+
def calculate_text_width(text)
85+
text.gsub(/\e\[[\d;]*m/, '').size
86+
end
87+
8488
##
8589
# Starts accepting with a reset screen
8690

lib/rdoc/markup/to_bs.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ def accept_list_item_start(list_item)
6565
end
6666
end
6767

68+
def calculate_text_width(text)
69+
text.gsub(/_\x08/, '').gsub(/\x08./, '').size
70+
end
71+
6872
##
6973
# Turns on or off regexp handling for +convert_string+
7074

lib/rdoc/markup/to_rdoc.rb

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -250,13 +250,10 @@ def accept_verbatim(verbatim)
250250
# Adds +table+ to the output
251251

252252
def accept_table(header, body, aligns)
253-
body = body.map do |row|
254-
row.map do |cell|
255-
attributes cell
256-
end
257-
end
253+
header = header.map { |h| attributes h }
254+
body = body.map { |row| row.map { |t| attributes t } }
258255
widths = header.zip(*body).map do |cols|
259-
cols.map(&:size).max
256+
cols.map { |col| calculate_text_width(col) }.max
260257
end
261258
aligns = aligns.map do |a|
262259
case a
@@ -269,16 +266,22 @@ def accept_table(header, body, aligns)
269266
end
270267
end
271268
@res << header.zip(widths, aligns).map do |h, w, a|
272-
h.__send__(a, w)
269+
extra_width = h.size - calculate_text_width(h)
270+
h.__send__(a, w + extra_width)
273271
end.join("|").rstrip << "\n"
274272
@res << widths.map {|w| "-" * w }.join("|") << "\n"
275273
body.each do |row|
276274
@res << row.zip(widths, aligns).map do |t, w, a|
277-
t.__send__(a, w)
275+
extra_width = t.size - calculate_text_width(t)
276+
t.__send__(a, w + extra_width)
278277
end.join("|").rstrip << "\n"
279278
end
280279
end
281280

281+
def calculate_text_width(text)
282+
text.size
283+
end
284+
282285
##
283286
# Applies attribute-specific markup to +text+ using RDoc::AttributeManager
284287

test/rdoc/markup/to_ansi_test.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -350,11 +350,11 @@ def list_verbatim
350350

351351
def accept_table_align
352352
expected = "\e[0m" + <<-EXPECTED
353-
AA |BB |CCCCC| DDDDD
354-
----|---|-----|---------
355-
|bbb| c|
356-
aaaa|b | | dd
357-
a | | cc|\e[7mdd\e[m
353+
AA |BB |CCCCC|DDDDD
354+
----|---|-----|-----
355+
|bbb| \e[1mc\e[m|
356+
aaaa|b | | dd
357+
a | | cc| \e[7mdd\e[m
358358
EXPECTED
359359
assert_equal expected, @to.end_accepting
360360
end

test/rdoc/markup/to_bs_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ def accept_table_align
353353
expected = <<-EXPECTED
354354
AA |BB |CCCCC|DDDDD
355355
----|---|-----|-----
356-
|bbb| c|
356+
|bbb| c\bc|
357357
aaaa|b | | dd
358358
a | | cc| dd
359359
EXPECTED

test/rdoc/markup/to_markdown_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ def accept_table_align
350350
expected = <<-EXPECTED
351351
AA |BB |CCCCC|DDDDD
352352
----|---|-----|-----
353-
|bbb| c|
353+
|bbb|**c**|
354354
aaaa|b | | dd
355355
a | | cc|`dd`
356356
EXPECTED

test/rdoc/markup/to_rdoc_test.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -348,11 +348,11 @@ def list_verbatim
348348

349349
def accept_table_align
350350
expected = <<-EXPECTED
351-
AA |BB |CCCCC| DDDDD
352-
----|---|-----|-----------
353-
|bbb| c|
354-
aaaa|b | | dd
355-
a | | cc|<tt>dd</tt>
351+
AA |BB | CCCCC| DDDDD
352+
----|---|--------|-----------
353+
|bbb|<b>c</b>|
354+
aaaa|b | | dd
355+
a | | cc|<tt>dd</tt>
356356
EXPECTED
357357
assert_equal expected, @to.end_accepting
358358
end

test/rdoc/support/text_formatter_test_case.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def test_accept_paragraph_wrap
105105
def test_accept_table_align
106106
header = ['AA', 'BB', 'CCCCC', 'DDDDD']
107107
body = [
108-
['', 'bbb', 'c', ''],
108+
['', 'bbb', '<b>c</b>', ''],
109109
['aaaa', 'b', '', 'dd'],
110110
['a', '', 'cc', '<code>dd</code>']
111111
]

0 commit comments

Comments
 (0)