Skip to content

Commit d51bfc0

Browse files
authored
Apply markup in table cell (#1483)
In the documentation for methods like `test`, backtick are used within table cells. While each parser handles backquotes within table cells, the generator does not. As a result, the tags are displayed literally without being processed. To resolve this, I propose that tag expressions should be properly handled within table cells as well. ### Before ri --format=bs test ``` Character |Test ------------|--------------------------------------------------------------- <tt>'<'</tt>|Whether the `mtime` at `path0` is less than that at `path1`. <tt>'='</tt>|Whether the `mtime` at `path0` is equal to that at `path1`. <tt>'>'</tt>|Whether the `mtime` at `path0` is greater than that at `path1`. ``` ri --format=markdown test ``` Character |Test ------------|--------------------------------------------------------------- <tt>'<'</tt>|Whether the `mtime` at `path0` is less than that at `path1`. <tt>'='</tt>|Whether the `mtime` at `path0` is equal to that at `path1`. <tt>'>'</tt>|Whether the `mtime` at `path0` is greater than that at `path1`. ``` ri --format=ansi <img width="550" height="92" alt="image" src="https://github.com/user-attachments/assets/1f7f4ac4-7c15-4352-b272-9e8789a3320a" /> ### After ri --format=bs test ``` Character|Test ---------|--------------------------------------------------------------- '<' |Whether the `mtime` at `path0` is less than that at `path1`. '=' |Whether the `mtime` at `path0` is equal to that at `path1`. '>' |Whether the `mtime` at `path0` is greater than that at `path1`. ``` ri --format=markdown test ``` Character|Test ---------|--------------------------------------------------------------- `'<'` |Whether the `mtime` at `path0` is less than that at `path1`. `'='` |Whether the `mtime` at `path0` is equal to that at `path1`. `'>'` |Whether the `mtime` at `path0` is greater than that at `path1`. ``` ri --format=ansi <img width="522" height="90" alt="image" src="https://github.com/user-attachments/assets/bd1dc32d-fe67-4f95-b819-30454f78af65" />
1 parent f85cf5c commit d51bfc0

File tree

8 files changed

+31
-15
lines changed

8 files changed

+31
-15
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 & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,10 @@ def accept_verbatim(verbatim)
250250
# Adds +table+ to the output
251251

252252
def accept_table(header, body, aligns)
253+
header = header.map { |h| attributes h }
254+
body = body.map { |row| row.map { |t| attributes t } }
253255
widths = header.zip(*body).map do |cols|
254-
cols.map(&:size).max
256+
cols.map { |col| calculate_text_width(col) }.max
255257
end
256258
aligns = aligns.map do |a|
257259
case a
@@ -264,16 +266,22 @@ def accept_table(header, body, aligns)
264266
end
265267
end
266268
@res << header.zip(widths, aligns).map do |h, w, a|
267-
h.__send__(a, w)
269+
extra_width = h.size - calculate_text_width(h)
270+
h.__send__(a, w + extra_width)
268271
end.join("|").rstrip << "\n"
269272
@res << widths.map {|w| "-" * w }.join("|") << "\n"
270273
body.each do |row|
271274
@res << row.zip(widths, aligns).map do |t, w, a|
272-
t.__send__(a, w)
275+
extra_width = t.size - calculate_text_width(t)
276+
t.__send__(a, w + extra_width)
273277
end.join("|").rstrip << "\n"
274278
end
275279
end
276280

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

test/rdoc/markup/to_ansi_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,9 +352,9 @@ def accept_table_align
352352
expected = "\e[0m" + <<-EXPECTED
353353
AA |BB |CCCCC|DDDDD
354354
----|---|-----|-----
355-
|bbb| c|
355+
|bbb| \e[1mc\e[m|
356356
aaaa|b | | dd
357-
a | | cc| 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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,9 +350,9 @@ def accept_table_align
350350
expected = <<-EXPECTED
351351
AA |BB |CCCCC|DDDDD
352352
----|---|-----|-----
353-
|bbb| c|
353+
|bbb|**c**|
354354
aaaa|b | | dd
355-
a | | cc| dd
355+
a | | cc|`dd`
356356
EXPECTED
357357
assert_equal expected, @to.end_accepting
358358
end

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| dd
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@ 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'],
110-
['a', '', 'cc', 'dd']
110+
['a', '', 'cc', '<code>dd</code>']
111111
]
112112
aligns = [nil, :left, :right, :center]
113113
@to.start_accepting

0 commit comments

Comments
 (0)