diff --git a/lib/rdoc/markdown.kpeg b/lib/rdoc/markdown.kpeg index e1193fbb00..ba03000224 100644 --- a/lib/rdoc/markdown.kpeg +++ b/lib/rdoc/markdown.kpeg @@ -1198,19 +1198,26 @@ CodeFence = &{ github? } } Table = &{ github? } - TableRow:header TableLine:line TableRow+:body + TableHead:header TableLine:line TableRow+:body { table = RDoc::Markup::Table.new(header, line, body) } -TableRow = TableItem+:row "|" @Newline - { row } -TableItem = "|" < (!"|" !@Newline .)+ > - { text.strip } - -TableLine = TableColumn+:line "|" @Newline - { line } -TableColumn = "|" < ( "-"+ ":"? | ":" "-"* ) > - { text.start_with?(":") ? :left : - text.end_with?(":") ? :right : nil +TableHead = TableItem2+:items "|"? @Newline + { items } + +TableRow = ( ( TableItem:item1 TableItem2*:items { [item1, *items] } ):row | TableItem2+:row ) "|"? @Newline + { row } +TableItem2 = "|" TableItem +TableItem = < /(?:\\.|[^|\n])+/ > + { text.strip.gsub(/\\(.)/, '\1') } + +TableLine = ( ( TableAlign:align1 TableAlign2*:aligns {[align1, *aligns] } ):line | TableAlign2+:line ) "|"? @Newline + { line } +TableAlign2 = "|" @Sp TableAlign +TableAlign = < /:?-+:?/ > @Sp + { + text.start_with?(":") ? + (text.end_with?(":") ? :center : :left) : + (text.end_with?(":") ? :right : nil) } DefinitionList = &{ definition_lists? } diff --git a/test/rdoc/test_rdoc_markdown.rb b/test/rdoc/test_rdoc_markdown.rb index ca76c34f43..dd6f312fa9 100644 --- a/test/rdoc/test_rdoc_markdown.rb +++ b/test/rdoc/test_rdoc_markdown.rb @@ -1062,6 +1062,27 @@ def test_gfm_table assert_equal expected, doc end + def test_gfm_table_2 + doc = parse <<~'MD' + | Cmd | Returns | Meaning + ----- | :-----: | ------- + |"b" | boolean | True if file1 is a block device + "c" | boolean | True if file1 is a character device + |"\|" | boolean | escaped bar \| test + MD + + head = %w[Cmd Returns Meaning] + align = [nil, :center, nil] + body = [ + ['"b"', 'boolean', 'True if file1 is a block device'], + ['"c"', 'boolean', 'True if file1 is a character device'], + ['"|"', 'boolean', 'escaped bar | test'], + ] + expected = doc(@RM::Table.new(head, align, body)) + + assert_equal expected, doc + end + def parse text @parser.parse text end