diff options
-rw-r--r-- | ftplugin/rst_tables.vim | 18 | ||||
-rw-r--r-- | src/rst_tables.py | 18 | ||||
-rw-r--r-- | tests/fixtures/multiline-cells.txt | 6 | ||||
-rw-r--r-- | tests/test_rst_tables.py | 18 | ||||
-rw-r--r-- | tutorial.rst | 14 |
5 files changed, 63 insertions, 11 deletions
diff --git a/ftplugin/rst_tables.vim b/ftplugin/rst_tables.vim index 4892945..38f830e 100644 --- a/ftplugin/rst_tables.vim +++ b/ftplugin/rst_tables.vim @@ -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 diff --git a/src/rst_tables.py b/src/rst_tables.py index 1cceb91..695d5bd 100644 --- a/src/rst_tables.py +++ b/src/rst_tables.py @@ -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 diff --git a/tests/fixtures/multiline-cells.txt b/tests/fixtures/multiline-cells.txt new file mode 100644 index 0000000..78465fd --- /dev/null +++ b/tests/fixtures/multiline-cells.txt @@ -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. diff --git a/tests/test_rst_tables.py b/tests/test_rst_tables.py index 1eb61a6..0517769 100644 --- a/tests/test_rst_tables.py +++ b/tests/test_rst_tables.py @@ -22,7 +22,7 @@ import unittest # 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 @@ class TestRSTTableFormatter(unittest.TestCase): 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 @@ class TestRSTTableFormatter(unittest.TestCase): '| 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): diff --git a/tutorial.rst b/tutorial.rst index 014338f..dbdf44f 100644 --- a/tutorial.rst +++ b/tutorial.rst @@ -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. |