Skip to content

Commit 9dc933d

Browse files
committed
Support GFM table
1 parent d23594e commit 9dc933d

File tree

8 files changed

+142
-0
lines changed

8 files changed

+142
-0
lines changed

lib/rdoc/markdown.kpeg

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,7 @@ Block = @BlankLine*
506506
( BlockQuote
507507
| Verbatim
508508
| CodeFence
509+
| Table
509510
| Note
510511
| Reference
511512
| HorizontalRule
@@ -1195,6 +1196,23 @@ CodeFence = &{ github? }
11951196
verbatim
11961197
}
11971198

1199+
Table = &{ github? }
1200+
TableRow:header TableLine:line TableRow+:body
1201+
{ table = RDoc::Markup::Table.new(header, line, body) }
1202+
1203+
TableRow = < TableItem+:row > "|" @Newline
1204+
{ row }
1205+
TableItem = "|" < (!"|" !@Newline .)+ >
1206+
{ text.strip }
1207+
1208+
TableLine = TableColumn+:line "|" @Newline
1209+
{ line }
1210+
TableColumn = "|" < ( "-"+ ":"? | ":" "-"* ) >
1211+
{
1212+
text.start_with?(":") ? :left :
1213+
text.end_with?(":") ? :right : nil
1214+
}
1215+
11981216
DefinitionList = &{ definition_lists? }
11991217
( DefinitionListItem+:list )
12001218
{ RDoc::Markup::List.new :NOTE, *list.flatten }

lib/rdoc/markup.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,7 @@ def convert input, formatter
843843
autoload :List, 'rdoc/markup/list'
844844
autoload :ListItem, 'rdoc/markup/list_item'
845845
autoload :Paragraph, 'rdoc/markup/paragraph'
846+
autoload :Table, 'rdoc/markup/table'
846847
autoload :Raw, 'rdoc/markup/raw'
847848
autoload :Rule, 'rdoc/markup/rule'
848849
autoload :Verbatim, 'rdoc/markup/verbatim'

lib/rdoc/markup/table.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# frozen_string_literal: true
2+
##
3+
# A section of table
4+
5+
class RDoc::Markup::Table
6+
attr_accessor :header, :align, :body
7+
8+
def initialize header, align, body
9+
@header, @align, @body = header, align, body
10+
end
11+
12+
def == other
13+
self.class == other.class and
14+
@header == other.header and
15+
@align == other.align and
16+
@body == other.body
17+
end
18+
19+
def accept visitor
20+
visitor.accept_table @header, @body, @align
21+
end
22+
23+
def pretty_print q # :nodoc:
24+
q.group 2, '[Table: ', ']' do
25+
q.group 2, '[Head: ', ']' do
26+
q.seplist @header.zip(@align) do |text, align|
27+
q.pp text
28+
if align
29+
q.text ":"
30+
q.breakable
31+
q.text align.to_s
32+
end
33+
end
34+
end
35+
q.breakable
36+
q.group 2, '[Body: ', ']' do
37+
q.seplist @body do |body|
38+
q.group 2, '[', ']' do
39+
q.seplist body do |text|
40+
q.pp text
41+
end
42+
end
43+
end
44+
end
45+
end
46+
end
47+
end

lib/rdoc/markup/to_html.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,29 @@ def accept_raw raw
314314
@res << raw.parts.join("\n")
315315
end
316316

317+
##
318+
# Adds +table+ to the output
319+
320+
def accept_table header, body, aligns
321+
@res << "\n<table role=\"table\">\n<thead>\n<tr>\n"
322+
header.zip(aligns) do |text, align|
323+
@res << '<th'
324+
@res << ' align="' << align << '"' if align
325+
@res << '>' << CGI.escapeHTML(text) << "</th>\n"
326+
end
327+
@res << "</tr>\n</thead>\n<tbody>\n"
328+
body.each do |row|
329+
@res << "<tr>\n"
330+
row.zip(aligns) do |text, align|
331+
@res << '<td'
332+
@res << ' align="' << align << '"' if align
333+
@res << '>' << CGI.escapeHTML(text) << "</td>\n"
334+
end
335+
@res << "</tr>\n"
336+
end
337+
@res << "</tbody>\n</table>\n"
338+
end
339+
317340
# :section: Utilities
318341

319342
##

lib/rdoc/markup/to_joined_paragraph.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def accept_paragraph paragraph
4141
alias accept_raw ignore
4242
alias accept_rule ignore
4343
alias accept_verbatim ignore
44+
alias accept_table ignore
4445

4546
end
4647

lib/rdoc/markup/to_rdoc.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,34 @@ def accept_verbatim verbatim
237237
@res << "\n"
238238
end
239239

240+
##
241+
# Adds +table+ to the output
242+
243+
def accept_table header, body, aligns
244+
widths = header.zip(body) do |h, b|
245+
[h.size, b.size].max
246+
end
247+
aligns = aligns.map do |a|
248+
case a
249+
when nil
250+
:center
251+
when :left
252+
:ljust
253+
when :right
254+
:rjust
255+
end
256+
end
257+
@res << header.zip(widths, aligns) do |h, w, a|
258+
h.__send__(a, w)
259+
end.join("|").rstrip << "\n"
260+
@res << widths.map {|w| "-" * w }.join("|") << "\n"
261+
body.each do |row|
262+
@res << row.zip(widths, aligns) do |t, w, a|
263+
t.__send__(a, w)
264+
end.join("|").rstrip << "\n"
265+
end
266+
end
267+
240268
##
241269
# Applies attribute-specific markup to +text+ using RDoc::AttributeManager
242270

lib/rdoc/markup/to_table_of_contents.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ def suppressed? heading
8282
alias accept_list_item_end ignore
8383
alias accept_list_end_bullet ignore
8484
alias accept_list_start ignore
85+
alias accept_table ignore
8586
# :startdoc:
8687

8788
end

test/rdoc/test_rdoc_markdown.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,29 @@ def test_code_fence_with_unintended_array
10121012
assert_equal expected, doc
10131013
end
10141014

1015+
def test_gfm_table
1016+
doc = parse <<~MD
1017+
| | |compare-ruby|built-ruby|
1018+
|------|:----------------|-----------:|---------:|
1019+
|test | 1 | 11.618M| 10.757M|
1020+
| | | 1.08x| -|
1021+
|test | 10 | 1.849M| 1.723M|
1022+
| | | 1.07x| -|
1023+
MD
1024+
1025+
head = ["", "", "compare-ruby", "built-ruby"]
1026+
align = [nil, :left, :right, :right]
1027+
body = [
1028+
["test", "1", "11.618M", "10.757M"],
1029+
["", "", "1.08x", "-"],
1030+
["test", "10", "1.849M", "1.723M"],
1031+
["", "", "1.07x", "-"],
1032+
]
1033+
expected = doc(@RM::Table.new(head, align, body))
1034+
1035+
assert_equal expected, doc
1036+
end
1037+
10151038
def parse text
10161039
@parser.parse text
10171040
end

0 commit comments

Comments
 (0)