diff options
author | Nobuyoshi Nakada <[email protected]> | 2021-01-26 00:31:00 +0900 |
---|---|---|
committer | Nobuyoshi Nakada <[email protected]> | 2021-03-16 15:47:27 +0900 |
commit | 3651f678a719ae3a35825bcb4e0dabbc7c60d8df (patch) | |
tree | f8705c06212e4778780c1db52b99ae17d319e33d /lib/rdoc/markup | |
parent | 05898c5b9001c0b1e8bd7bf0d12b42a8e7c388b8 (diff) |
[ruby/rdoc] Support GFM table
https://github.com/ruby/rdoc/commit/9dc933df16
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/4274
Diffstat (limited to 'lib/rdoc/markup')
-rw-r--r-- | lib/rdoc/markup/table.rb | 47 | ||||
-rw-r--r-- | lib/rdoc/markup/to_html.rb | 23 | ||||
-rw-r--r-- | lib/rdoc/markup/to_joined_paragraph.rb | 1 | ||||
-rw-r--r-- | lib/rdoc/markup/to_rdoc.rb | 28 | ||||
-rw-r--r-- | lib/rdoc/markup/to_table_of_contents.rb | 1 |
5 files changed, 100 insertions, 0 deletions
diff --git a/lib/rdoc/markup/table.rb b/lib/rdoc/markup/table.rb new file mode 100644 index 0000000000..7bcb10aff3 --- /dev/null +++ b/lib/rdoc/markup/table.rb @@ -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 diff --git a/lib/rdoc/markup/to_html.rb b/lib/rdoc/markup/to_html.rb index 3b1b0e9d40..e2a00bd8a1 100644 --- a/lib/rdoc/markup/to_html.rb +++ b/lib/rdoc/markup/to_html.rb @@ -314,6 +314,29 @@ class RDoc::Markup::ToHtml < RDoc::Markup::Formatter @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 ## diff --git a/lib/rdoc/markup/to_joined_paragraph.rb b/lib/rdoc/markup/to_joined_paragraph.rb index 795f3f62ee..46e07c94ad 100644 --- a/lib/rdoc/markup/to_joined_paragraph.rb +++ b/lib/rdoc/markup/to_joined_paragraph.rb @@ -41,6 +41,7 @@ class RDoc::Markup::ToJoinedParagraph < RDoc::Markup::Formatter alias accept_raw ignore alias accept_rule ignore alias accept_verbatim ignore + alias accept_table ignore end diff --git a/lib/rdoc/markup/to_rdoc.rb b/lib/rdoc/markup/to_rdoc.rb index 81b16c4973..3cdf4fd08b 100644 --- a/lib/rdoc/markup/to_rdoc.rb +++ b/lib/rdoc/markup/to_rdoc.rb @@ -238,6 +238,34 @@ class RDoc::Markup::ToRdoc < RDoc::Markup::Formatter 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 def attributes text diff --git a/lib/rdoc/markup/to_table_of_contents.rb b/lib/rdoc/markup/to_table_of_contents.rb index f68b90bcf6..eb8e8faa16 100644 --- a/lib/rdoc/markup/to_table_of_contents.rb +++ b/lib/rdoc/markup/to_table_of_contents.rb @@ -82,6 +82,7 @@ class RDoc::Markup::ToTableOfContents < RDoc::Markup::Formatter alias accept_list_item_end ignore alias accept_list_end_bullet ignore alias accept_list_start ignore + alias accept_table ignore # :startdoc: end |