Skip to content
Open
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
13 changes: 13 additions & 0 deletions lib/text-table/table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,19 @@ def to_s
rendered_rows.join
end

# Renders a markdown styled table
def to_markdown
b = @boundary_intersection
@boundary_intersection = '|'

rendered_rows = [separator] + text_table_rows.map(&:to_s)
rendered_rows.unshift [text_table_head.to_s] if head

@boundary_intersection = b

rendered_rows.join.gsub('|-', '| ').gsub('-|', ' |')
end

# Aligns the cells and the footer of a column.
#
# table = Text::Table.new :rows => [%w(a bb), %w(aa bbb), %w(aaa b)]
Expand Down
10 changes: 10 additions & 0 deletions spec/integration/boundaries_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,14 @@
+----------+----------+----------+----------+
}) }
end

describe 'markdown style table' do
subject { table.to_markdown }
it { should == deindent(%q{
| a | bb | ccc | dddd |
| ---- | ---- | ---- | ---- |
| aa | bbb | cccc | d |
| aaa | bbbb | c | dd |
}) }
end
end