From 4353027f5c7dd4235151baced074a32545d886b6 Mon Sep 17 00:00:00 2001 From: Vincent Driessen Date: Thu, 19 Aug 2010 09:51:42 +0200 Subject: Added ability to parse existing tables. --- ftplugin/rst_tables.vim | 16 ++++++++++++---- src/rst_tables.py | 16 ++++++++++++---- tests/test_rst_tables.py | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 8 deletions(-) diff --git a/ftplugin/rst_tables.vim b/ftplugin/rst_tables.vim index 0722816..4892945 100644 --- a/ftplugin/rst_tables.vim +++ b/ftplugin/rst_tables.vim @@ -51,11 +51,19 @@ def unify_table(table): return output +def split_table_row(row_string): + if row_string.find("|") >= 0: + # first, strip off the outer table drawings + row_string = re.sub(r'^\s*\||\|\s*$', '', row_string) + return re.split(r'\s*\|\s*', row_string.strip()) + return re.split(r'\s\s+', row_string.rstrip()) + + def parse_table(raw_lines): - mkfields = lambda line: re.split('\s\s+', line.rstrip()) - output = map(mkfields, raw_lines) - output = unify_table(output) - return output + select_data_lines = lambda line: not re.match('^[\t +=-]+$', line) + lines = filter(select_data_lines, raw_lines) + lines = map(split_table_row, lines) + return unify_table(lines) def table_line(widths, header=False): diff --git a/src/rst_tables.py b/src/rst_tables.py index e1e3842..1cceb91 100644 --- a/src/rst_tables.py +++ b/src/rst_tables.py @@ -36,11 +36,19 @@ def unify_table(table): return output +def split_table_row(row_string): + if row_string.find("|") >= 0: + # first, strip off the outer table drawings + row_string = re.sub(r'^\s*\||\|\s*$', '', row_string) + return re.split(r'\s*\|\s*', row_string.strip()) + return re.split(r'\s\s+', row_string.rstrip()) + + def parse_table(raw_lines): - mkfields = lambda line: re.split('\s\s+', line.rstrip()) - output = map(mkfields, raw_lines) - output = unify_table(output) - return output + select_data_lines = lambda line: not re.match('^[\t +=-]+$', line) + lines = filter(select_data_lines, raw_lines) + lines = map(split_table_row, lines) + return unify_table(lines) def table_line(widths, header=False): diff --git a/tests/test_rst_tables.py b/tests/test_rst_tables.py index b3df5ad..f7f5280 100644 --- a/tests/test_rst_tables.py +++ b/tests/test_rst_tables.py @@ -82,6 +82,38 @@ a line ending. expected = [['x', 'y'], ['a', 'b'], ['only one', '']] self.assertEquals(expected, parse_table(input)) + def testParseValidTable(self): + input = ['+=====+====+', + '| Foo | Mu |', + '+=====+====+', + '| x | y |', + '+-----+----+'] + expect = [['Foo', 'Mu'], ['x', 'y']] + self.assertEquals(expect, parse_table(input)) + + def testParseCorruptedTable(self): + input = ['+===+-----====+', + '| 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)) + + input = ['+===+-----====+', + '| 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): self.assertEquals('', table_line([], True)) self.assertEquals('++', table_line([0], True)) -- cgit v1.2.3