From 4c81fb1df579143bdd1ec7f5b2a1779d3c3c58fd Mon Sep 17 00:00:00 2001 From: Vincent Driessen Date: Thu, 19 Aug 2010 11:36:53 +0200 Subject: Add join_rows helper function (necessary for joining long lines later on). --- ftplugin/rst_tables.vim | 24 ++++++++++++++++++++++++ src/rst_tables.py | 24 ++++++++++++++++++++++++ tests/test_rst_tables.py | 30 +++++++++++++++++++++++++++++- 3 files changed, 77 insertions(+), 1 deletion(-) diff --git a/ftplugin/rst_tables.vim b/ftplugin/rst_tables.vim index 38f830e..43b674f 100644 --- a/ftplugin/rst_tables.vim +++ b/ftplugin/rst_tables.vim @@ -39,8 +39,32 @@ def get_table_bounds(): return (upper, lower) +def join_rows(rows, sep='\n'): + """Given a list of rows (a list of lists) this function returns a + flattened list where each the individual columns of all rows are joined + together using the line separator. + + """ + output = [] + for row in rows: + # grow output array, if necessary + if len(output) <= len(row): + for i in range(len(row) - len(output)): + output.extend([[]]) + + for i, field in enumerate(row): + field_text = field.strip() + if field_text: + output[i].append(field_text) + return map(lambda lines: sep.join(lines), output) + def unify_table(table): + """Given a list of rows (i.e. a table), this function returns a new table + in which all rows have an equal amount of columns. If all full column is + empty (i.e. all rows have that field empty), the column is removed. + + """ max_fields = max(map(lambda row: len(row), table)) empty_cols = [True] * max_fields output = [] diff --git a/src/rst_tables.py b/src/rst_tables.py index 695d5bd..3fb8923 100644 --- a/src/rst_tables.py +++ b/src/rst_tables.py @@ -24,8 +24,32 @@ def get_table_bounds(): return (upper, lower) +def join_rows(rows, sep='\n'): + """Given a list of rows (a list of lists) this function returns a + flattened list where each the individual columns of all rows are joined + together using the line separator. + + """ + output = [] + for row in rows: + # grow output array, if necessary + if len(output) <= len(row): + for i in range(len(row) - len(output)): + output.extend([[]]) + + for i, field in enumerate(row): + field_text = field.strip() + if field_text: + output[i].append(field_text) + return map(lambda lines: sep.join(lines), output) + def unify_table(table): + """Given a list of rows (i.e. a table), this function returns a new table + in which all rows have an equal amount of columns. If all full column is + empty (i.e. all rows have that field empty), the column is removed. + + """ max_fields = max(map(lambda row: len(row), table)) empty_cols = [True] * max_fields output = [] diff --git a/tests/test_rst_tables.py b/tests/test_rst_tables.py index f57761f..6c486dd 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, unify_table + pad_fields, unify_table, join_rows class TestRSTTableFormatter(unittest.TestCase): @@ -58,6 +58,15 @@ class TestRSTTableFormatter(unittest.TestCase): vim.current.window.cursor = (8, 0) self.assertEquals((8, 9), get_table_bounds()) + def testJoinSimpleRows(self): + input_rows = [['x', 'y', 'z'], ['foo', 'bar']] + expected = ['x\nfoo', 'y\nbar', 'z'] + self.assertEquals(expected, join_rows(input_rows)) + + input_rows.append(['apple', '', 'pear']) + expected = ['x foo apple', 'y bar', 'z pear'] + self.assertEquals(expected, join_rows(input_rows, sep=' ')) + def testParseSimpleTable(self): self.assertEquals([['x y z']], parse_table(['x y z'])) self.assertEquals([['x', 'y z']], parse_table(['x y z'])) @@ -191,3 +200,22 @@ a line ending. """.split('\n') create_table() self.assertEquals(expect, vim.current.buffer) + + def notestCreateComplexTable(self): + raw_lines = self.read_fixture('multiline-cells') + expect = """ ++================+===============================================================+ +| 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. | ++----------------+---------------------------------------------------------------+ +""".strip().split('\n') + self.assertEquals(expect, draw_table(parse_table(raw_lines))) -- cgit v1.2.3