aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVincent Driessen <vincent@datafox.nl>2010-08-19 10:49:51 +0200
committerVincent Driessen <vincent@datafox.nl>2010-08-19 10:49:51 +0200
commitbc3620adf956828562071f9feafc5923de567ea9 (patch)
tree317d3d80697a3982915e652e66522adde00e06e0
parent3a4e12229e09e72619dc1bcc5403db1e452d9ecb (diff)
downloadvim-rst-tables-bc3620adf956828562071f9feafc5923de567ea9.tar.gz
vim-rst-tables-bc3620adf956828562071f9feafc5923de567ea9.tar.bz2
vim-rst-tables-bc3620adf956828562071f9feafc5923de567ea9.zip
Add ability to remove all empty columns automatically.
-rw-r--r--ftplugin/rst_tables.vim18
-rw-r--r--src/rst_tables.py18
-rw-r--r--tests/fixtures/multiline-cells.txt6
-rw-r--r--tests/test_rst_tables.py18
-rw-r--r--tutorial.rst14
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.