From e447f25f5240e256fb0ec7491cde1313519f0896 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Reynolds?= Date: Mon, 4 May 2015 15:32:05 -0300 Subject: [PATCH] to_markdown for markdown tables --- lib/text-table/table.rb | 13 +++++++++++++ spec/integration/boundaries_spec.rb | 10 ++++++++++ 2 files changed, 23 insertions(+) diff --git a/lib/text-table/table.rb b/lib/text-table/table.rb index 427e611..03011b7 100644 --- a/lib/text-table/table.rb +++ b/lib/text-table/table.rb @@ -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)] diff --git a/spec/integration/boundaries_spec.rb b/spec/integration/boundaries_spec.rb index 2287f2b..69a5020 100644 --- a/spec/integration/boundaries_spec.rb +++ b/spec/integration/boundaries_spec.rb @@ -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