From 1f7c5699cf693b15eaa054bd82a9d193d847cd1b Mon Sep 17 00:00:00 2001 From: Pete Jemian Date: Tue, 3 Apr 2018 11:06:22 -0500 Subject: [PATCH] #12 constructs simple HTML table --- CHANGES.rst | 5 +++ src/pyRestTable/rest_table.py | 20 +++++++++-- tests/test_results.py | 64 +++++++++++++++++++++++++++++------ 3 files changed, 77 insertions(+), 12 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index bc718a2..a714698 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -7,6 +7,11 @@ Change History Production ********** +:2018.4.0: + + * `#12 `_ + provide HTML table output format + :2017.2.0: * `#9 `_ diff --git a/src/pyRestTable/rest_table.py b/src/pyRestTable/rest_table.py index 01696dc..0be2390 100644 --- a/src/pyRestTable/rest_table.py +++ b/src/pyRestTable/rest_table.py @@ -4,7 +4,7 @@ #----------------------------------------------------------------------------- # :author: Pete R. Jemian # :email: prjemian@gmail.com -# :copyright: (c) 2014-2017, Pete R. Jemian +# :copyright: (c) 2014-2018, Pete R. Jemian # # Distributed under the terms of the Creative Commons Attribution 4.0 International Public License. # @@ -32,7 +32,8 @@ def _prepare_results_(t): s += t.reST(fmt='plain') + '\n' s += t.reST(fmt='simple') + '\n' s += t.reST(fmt='grid') + '\n' - s += t.reST(fmt='list-table') + s += t.reST(fmt='list-table') + '\n' + s += t.reST(fmt='html') return s @@ -150,6 +151,7 @@ def reST(self, indentation = '', fmt = 'simple'): 'complex': self.grid_table, # alias for `grid`, do not deprecate 'grid': self.grid_table, 'list-table': self.list_table, + 'html': self.html_table, }[fmt](indentation) def plain_table(self, indentation = ''): @@ -278,6 +280,20 @@ def multiline(cell, prefix, indentation, fmt): return '\n'.join(rest) + def html_table(self, indentation = ''): + """render the table in *HTML*""" + html = "\n" + html += ' \n' # start the labels + html += "".join([" \n".format(k) for k in self.labels]) # labels + html += ' \n' # end the labels + for row in self.rows: + html += ' \n' # start each row + # TODO: consider column and row spans + html += "".join([" \n".format(k) for k in row]) # each row + html += ' \n' # end each row + html += '
{}
{}
' # end of table + return html + def _row(self, row, fmt, indentation = ''): """ Given a list of entry nodes in this table row, diff --git a/tests/test_results.py b/tests/test_results.py index 264a255..55375d1 100644 --- a/tests/test_results.py +++ b/tests/test_results.py @@ -80,14 +80,25 @@ - 2\ ''' +MINIMAL_HTML_RESULT = '''\ + + + + + + + + + +
xy
12
\ +''' + -EXAMPLE_MINIMAL_RESULT = MINIMAL_PLAIN_RESULT -EXAMPLE_MINIMAL_RESULT += '\n' -EXAMPLE_MINIMAL_RESULT += MINIMAL_SIMPLE_RESULT -EXAMPLE_MINIMAL_RESULT += '\n' -EXAMPLE_MINIMAL_RESULT += MINIMAL_GRID_RESULT -EXAMPLE_MINIMAL_RESULT += '\n' -EXAMPLE_MINIMAL_RESULT += MINIMAL_LISTTABLE_RESULT +EXAMPLE_MINIMAL_RESULT = MINIMAL_PLAIN_RESULT + '\n' +EXAMPLE_MINIMAL_RESULT += MINIMAL_SIMPLE_RESULT + '\n' +EXAMPLE_MINIMAL_RESULT += MINIMAL_GRID_RESULT + '\n' +EXAMPLE_MINIMAL_RESULT += MINIMAL_LISTTABLE_RESULT + '\n' +EXAMPLE_MINIMAL_RESULT += MINIMAL_HTML_RESULT EXAMPLE_BASIC_RESULT = '''\ @@ -136,7 +147,36 @@ - 3,3 * - 4,1 - 4,2 - - 4,3''' + - 4,3 +\ + + + + + + + + + + + + + + + + + + + + + + + + + + +
onetwothree
1,11,21,3
2,12,22,3
3,13,23,3
4,14,24,3
\ +''' EXAMPLE_COMPLICATED_RESULT = ''' @@ -170,7 +210,7 @@ def setUp(self): def apply_test(self, table, reference_text, style='simple'): text = table.reST(fmt=style) - self.assertTrue(text == reference_text) + self.assertTrue(text.strip() == reference_text.strip()) def population_table(self): t = pyRestTable.Table() @@ -231,10 +271,14 @@ def test_minimal_grid(self): def test_minimal_listtable(self): self.apply_test(pyRestTable.rest_table.example_minimal(), MINIMAL_LISTTABLE_RESULT, 'list-table') + def test_minimal_htmltable(self): + table = pyRestTable.rest_table.example_minimal() + self.apply_test(table, MINIMAL_HTML_RESULT, 'html') + def test_example_basic(self): t = pyRestTable.rest_table.example_basic() s = pyRestTable.rest_table._prepare_results_(t) - self.assertEqual(s, EXAMPLE_BASIC_RESULT) + self.assertEqual(s.strip(), EXAMPLE_BASIC_RESULT.strip()) def test_example_complicated(self): t = pyRestTable.rest_table.example_complicated()