Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/rdoc/markup/to_ansi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions lib/rdoc/markup/to_bs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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+

Expand Down
14 changes: 11 additions & 3 deletions lib/rdoc/markup/to_rdoc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
4 changes: 2 additions & 2 deletions test/rdoc/markup/to_ansi_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/rdoc/markup/to_bs_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/rdoc/markup/to_markdown_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions test/rdoc/markup/to_rdoc_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|<b>c</b>|
aaaa|b | | dd
a | | cc|<tt>dd</tt>
EXPECTED
assert_equal expected, @to.end_accepting
end
Expand Down
4 changes: 2 additions & 2 deletions test/rdoc/support/text_formatter_test_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ def test_accept_paragraph_wrap
def test_accept_table_align
header = ['AA', 'BB', 'CCCCC', 'DDDDD']
body = [
['', 'bbb', 'c', ''],
['', 'bbb', '<b>c</b>', ''],
['aaaa', 'b', '', 'dd'],
['a', '', 'cc', 'dd']
['a', '', 'cc', '<code>dd</code>']
]
aligns = [nil, :left, :right, :center]
@to.start_accepting
Expand Down