Permalink
Browse files

Add ability to remove all empty columns automatically.

  • Loading branch information...
1 parent 3a4e122 commit bc3620adf956828562071f9feafc5923de567ea9 @nvie committed Aug 19, 2010
Showing with 63 additions and 11 deletions.
  1. +18 −0 ftplugin/rst_tables.vim
  2. +18 −0 src/rst_tables.py
  3. +6 −0 tests/fixtures/multiline-cells.txt
  4. +14 −4 tests/test_rst_tables.py
  5. +7 −7 tutorial.rst
View
@@ -42,12 +42,30 @@ def get_table_bounds():
def unify_table(table):
max_fields = max(map(lambda row: len(row), table))
+ empty_cols = [True] * max_fields
output = []
for row in table:
curr_len = len(row)
if curr_len < max_fields:
row += [''] * (max_fields - curr_len)
output.append(row)
+
+ # register empty columns (to be removed at the end)
+ for i in range(len(row)):
+ if row[i].strip():
+ empty_cols[i] = False
+
+ # remove empty columns from all rows
+ table = output
+ output = []
+ for row in table:
+ cols = []
+ for i in range(len(row)):
+ should_remove = empty_cols[i]
+ if not should_remove:
+ cols.append(row[i])
+ output.append(cols)
+
return output
View
@@ -27,12 +27,30 @@ def get_table_bounds():
def unify_table(table):
max_fields = max(map(lambda row: len(row), table))
+ empty_cols = [True] * max_fields
output = []
for row in table:
curr_len = len(row)
if curr_len < max_fields:
row += [''] * (max_fields - curr_len)
output.append(row)
+
+ # register empty columns (to be removed at the end)
+ for i in range(len(row)):
+ if row[i].strip():
+ empty_cols[i] = False
+
+ # remove empty columns from all rows
+ table = output
+ output = []
+ for row in table:
+ cols = []
+ for i in range(len(row)):
+ should_remove = empty_cols[i]
+ if not should_remove:
+ cols.append(row[i])
+ output.append(cols)
+
return output
@@ -0,0 +1,6 @@
+Feature| Description |
+Ease of use | Drop dead simple!
+| Foo | Bar, qux, mux|
+Predictability | Lorem ipsum dolor sit amet, consectetur adipiscing elit. |
+| | Nullam congue dapibus aliquet. Integer ut rhoncus leo. In hac
+| | habitasse platea dictumst. Phasellus pretium iaculis.
View
@@ -22,7 +22,7 @@ def fake_eval(x):
# Load test subjects
from rst_tables import get_table_bounds, create_table, parse_table, \
draw_table, table_line, get_column_widths, \
- pad_fields
+ pad_fields, unify_table
class TestRSTTableFormatter(unittest.TestCase):
@@ -75,6 +75,16 @@ def testParseTableUnifiesColumns(self):
expected = [['x', 'y', ''], ['a', 'b', 'c'], ['only one', '', '']]
self.assertEquals(expected, parse_table(input))
+ def testUnifyTables(self):
+ input = [[' x ', ' y'], ['xxx', ' yyyy ', 'zz']]
+ expected = [[' x ', ' y', ''], ['xxx', ' yyyy ', 'zz']]
+ self.assertEquals(expected, unify_table(input))
+
+ def testUnifyTablesRemovesEmptyColumns(self):
+ input = [['x', '', 'y'], ['xxx', '', 'yyyy', 'zz', ' ']]
+ expected = [['x', 'y', ''], ['xxx', 'yyyy', 'zz']]
+ self.assertEquals(expected, unify_table(input))
+
def testParseDealsWithSpacesAtLineEnd(self):
input = ['x y ', 'a b ', 'only one']
expected = [['x', 'y'], ['a', 'b'], ['only one', '']]
@@ -107,9 +117,9 @@ def testParseCorruptedTable(self):
'| x | This became somewhat larger |',
'blah | A new line|| ',
'+-----+----+']
- expect = [['Foo', 'Mu', ''],
- ['x', 'This became somewhat larger', ''],
- ['blah', 'A new line', '']]
+ expect = [['Foo', 'Mu'],
+ ['x', 'This became somewhat larger'],
+ ['blah', 'A new line']]
self.assertEquals(expect, parse_table(input))
def testTableLine(self):
View
@@ -47,12 +47,12 @@ Feature| Description |
Ease of use | Drop dead simple!
Foo | Bar, qux, mux
Predictability | Deploykdj sfkljsdjf ljdflsk jsdflkj dsflkj sdlkj dfslkj
- | dfslkjds flkdjsfl sdfjlk jdfslk djsfl dfjslk jsdflk jfdslk jdfslkds fjlkds
- | jldkfj ldfsjlsd jldsf jlkjds flds fjlk jdflk jdsflj dslfjs dlfj sdflkj sdlfj
- |dfslkjds flkdjsfl sdfjlk jdfslk djsfl dfjslk jsdflk jfdslk jdfslkds fjlkds
- |jldkfj ldfsjlsd jldsf jlkjds flds fjlk jdflk jdsflj dslfjs dlfj sdflkj sdlfj
- |dfslkjds flkdjsfl sdfjlk jdfslk djsfl dfjslk jsdflk jfdslk jdfslkds fjlkds
- |jldkfj ldfsjlsd jldsf jlkjds flds fjlk jdflk jdsflj dslfjs dlfj sdflkj sdlfj
- |ldfsj dlfsj dfskjdfs ldsfj
+| | dfslkjds flkdjsfl sdfjlk jdfslk djsfl dfjslk jsdflk jfdslk jdfslkds fjlkds
+| | jldkfj ldfsjlsd jldsf jlkjds flds fjlk jdflk jdsflj dslfjs dlfj sdflkj sdlfj
+| |dfslkjds flkdjsfl sdfjlk jdfslk djsfl dfjslk jsdflk jfdslk jdfslkds fjlkds
+| |jldkfj ldfsjlsd jldsf jlkjds flds fjlk jdflk jdsflj dslfjs dlfj sdflkj sdlfj
+| |dfslkjds flkdjsfl sdfjlk jdfslk djsfl dfjslk jsdflk jfdslk jdfslkds fjlkds
+| |jldkfj ldfsjlsd jldsf jlkjds flds fjlk jdflk jdsflj dslfjs dlfj sdflkj sdlfj |
+| |ldfsj dlfsj dfskjdfs ldsfj
And some para-text below it.

0 comments on commit bc3620a

Please sign in to comment.