Skip to content

Commit a7647c1

Browse files
authored
Merge pull request #791 from nobu/gfm-table
GFM table
2 parents 8ea6109 + 2219c5a commit a7647c1

File tree

9 files changed

+161
-0
lines changed

9 files changed

+161
-0
lines changed

lib/rdoc/generator/template/darkfish/css/rdoc.css

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,25 @@ pre {
7979
border-radius: 0.2em;
8080
}
8181

82+
table {
83+
margin: 0;
84+
border-spacing: 0;
85+
border-collapse: collapse;
86+
}
87+
88+
table tr th, table tr td {
89+
padding: 0.2em 0.4em;
90+
border: 1px solid #ccc;
91+
}
92+
93+
table tr th {
94+
background-color: #eceaed;
95+
}
96+
97+
table tr:nth-child(even) td {
98+
background-color: #f5f4f6;
99+
}
100+
82101
/* @group Generic Classes */
83102

84103
.initially-hidden {

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
@@ -1196,6 +1197,23 @@ CodeFence = &{ github? }
11961197
verbatim
11971198
}
11981199

1200+
Table = &{ github? }
1201+
TableRow:header TableLine:line TableRow+:body
1202+
{ table = RDoc::Markup::Table.new(header, line, body) }
1203+
1204+
TableRow = < TableItem+:row > "|" @Newline
1205+
{ row }
1206+
TableItem = "|" < (!"|" !@Newline .)+ >
1207+
{ text.strip }
1208+
1209+
TableLine = TableColumn+:line "|" @Newline
1210+
{ line }
1211+
TableColumn = "|" < ( "-"+ ":"? | ":" "-"* ) >
1212+
{
1213+
text.start_with?(":") ? :left :
1214+
text.end_with?(":") ? :right : nil
1215+
}
1216+
11991217
DefinitionList = &{ definition_lists? }
12001218
( DefinitionListItem+:list )
12011219
{ 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
@@ -1040,6 +1040,29 @@ def test_code_fence_with_unintended_array
10401040
assert_equal expected, doc
10411041
end
10421042

1043+
def test_gfm_table
1044+
doc = parse <<~MD
1045+
| | |compare-ruby|built-ruby|
1046+
|------|:----------------|-----------:|---------:|
1047+
|test | 1 | 11.618M| 10.757M|
1048+
| | | 1.08x| -|
1049+
|test | 10 | 1.849M| 1.723M|
1050+
| | | 1.07x| -|
1051+
MD
1052+
1053+
head = ["", "", "compare-ruby", "built-ruby"]
1054+
align = [nil, :left, :right, :right]
1055+
body = [
1056+
["test", "1", "11.618M", "10.757M"],
1057+
["", "", "1.08x", "-"],
1058+
["test", "10", "1.849M", "1.723M"],
1059+
["", "", "1.07x", "-"],
1060+
]
1061+
expected = doc(@RM::Table.new(head, align, body))
1062+
1063+
assert_equal expected, doc
1064+
end
1065+
10431066
def parse text
10441067
@parser.parse text
10451068
end

0 commit comments

Comments
 (0)