Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GFM table #791

Merged
merged 2 commits into from
Mar 15, 2021
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
19 changes: 19 additions & 0 deletions lib/rdoc/generator/template/darkfish/css/rdoc.css
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,25 @@ pre {
border-radius: 0.2em;
}

table {
margin: 0;
border-spacing: 0;
border-collapse: collapse;
}

table tr th, table tr td {
padding: 0.2em 0.4em;
border: 1px solid #ccc;
}

table tr th {
background-color: #eceaed;
}

table tr:nth-child(even) td {
background-color: #f5f4f6;
}

/* @group Generic Classes */

.initially-hidden {
Expand Down
18 changes: 18 additions & 0 deletions lib/rdoc/markdown.kpeg
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,7 @@ Block = @BlankLine*
( BlockQuote
| Verbatim
| CodeFence
| Table
| Note
| Reference
| HorizontalRule
Expand Down Expand Up @@ -1195,6 +1196,23 @@ CodeFence = &{ github? }
verbatim
}

Table = &{ github? }
TableRow: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
}

DefinitionList = &{ definition_lists? }
( DefinitionListItem+:list )
{ RDoc::Markup::List.new :NOTE, *list.flatten }
Expand Down
1 change: 1 addition & 0 deletions lib/rdoc/markup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,7 @@ def convert input, formatter
autoload :List, 'rdoc/markup/list'
autoload :ListItem, 'rdoc/markup/list_item'
autoload :Paragraph, 'rdoc/markup/paragraph'
autoload :Table, 'rdoc/markup/table'
autoload :Raw, 'rdoc/markup/raw'
autoload :Rule, 'rdoc/markup/rule'
autoload :Verbatim, 'rdoc/markup/verbatim'
Expand Down
47 changes: 47 additions & 0 deletions lib/rdoc/markup/table.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# frozen_string_literal: true
##
# A section of table

class RDoc::Markup::Table
attr_accessor :header, :align, :body

def initialize header, align, body
@header, @align, @body = header, align, body
end

def == other
self.class == other.class and
@header == other.header and
@align == other.align and
@body == other.body
end

def accept visitor
visitor.accept_table @header, @body, @align
end

def pretty_print q # :nodoc:
q.group 2, '[Table: ', ']' do
q.group 2, '[Head: ', ']' do
q.seplist @header.zip(@align) do |text, align|
q.pp text
if align
q.text ":"
q.breakable
q.text align.to_s
end
end
end
q.breakable
q.group 2, '[Body: ', ']' do
q.seplist @body do |body|
q.group 2, '[', ']' do
q.seplist body do |text|
q.pp text
end
end
end
end
end
end
end
23 changes: 23 additions & 0 deletions lib/rdoc/markup/to_html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,29 @@ def accept_raw raw
@res << raw.parts.join("\n")
end

##
# Adds +table+ to the output

def accept_table header, body, aligns
@res << "\n<table role=\"table\">\n<thead>\n<tr>\n"
header.zip(aligns) do |text, align|
@res << '<th'
@res << ' align="' << align << '"' if align
@res << '>' << CGI.escapeHTML(text) << "</th>\n"
end
@res << "</tr>\n</thead>\n<tbody>\n"
body.each do |row|
@res << "<tr>\n"
row.zip(aligns) do |text, align|
@res << '<td'
@res << ' align="' << align << '"' if align
@res << '>' << CGI.escapeHTML(text) << "</td>\n"
end
@res << "</tr>\n"
end
@res << "</tbody>\n</table>\n"
end

# :section: Utilities

##
Expand Down
1 change: 1 addition & 0 deletions lib/rdoc/markup/to_joined_paragraph.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def accept_paragraph paragraph
alias accept_raw ignore
alias accept_rule ignore
alias accept_verbatim ignore
alias accept_table ignore

end

28 changes: 28 additions & 0 deletions lib/rdoc/markup/to_rdoc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,34 @@ def accept_verbatim verbatim
@res << "\n"
end

##
# Adds +table+ to the output

def accept_table header, body, aligns
widths = header.zip(body) do |h, b|
[h.size, b.size].max
end
aligns = aligns.map do |a|
case a
when nil
:center
when :left
:ljust
when :right
:rjust
end
end
@res << header.zip(widths, aligns) do |h, w, a|
h.__send__(a, w)
end.join("|").rstrip << "\n"
@res << widths.map {|w| "-" * w }.join("|") << "\n"
body.each do |row|
@res << row.zip(widths, aligns) do |t, w, a|
t.__send__(a, w)
end.join("|").rstrip << "\n"
end
end

##
# Applies attribute-specific markup to +text+ using RDoc::AttributeManager

Expand Down
1 change: 1 addition & 0 deletions lib/rdoc/markup/to_table_of_contents.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def suppressed? heading
alias accept_list_item_end ignore
alias accept_list_end_bullet ignore
alias accept_list_start ignore
alias accept_table ignore
# :startdoc:

end
Expand Down
23 changes: 23 additions & 0 deletions test/rdoc/test_rdoc_markdown.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,29 @@ def test_code_fence_with_unintended_array
assert_equal expected, doc
end

def test_gfm_table
doc = parse <<~MD
| | |compare-ruby|built-ruby|
|------|:----------------|-----------:|---------:|
|test | 1 | 11.618M| 10.757M|
| | | 1.08x| -|
|test | 10 | 1.849M| 1.723M|
| | | 1.07x| -|
MD

head = ["", "", "compare-ruby", "built-ruby"]
align = [nil, :left, :right, :right]
body = [
["test", "1", "11.618M", "10.757M"],
["", "", "1.08x", "-"],
["test", "10", "1.849M", "1.723M"],
["", "", "1.07x", "-"],
]
expected = doc(@RM::Table.new(head, align, body))

assert_equal expected, doc
end

def parse text
@parser.parse text
end
Expand Down