Skip to content

Commit

Permalink
#12 constructs simple HTML table
Browse files Browse the repository at this point in the history
  • Loading branch information
prjemian committed Apr 3, 2018
1 parent 3e477f6 commit 1f7c569
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 12 deletions.
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ Change History
Production
**********

:2018.4.0:

* `#12 <https://github.com/prjemian/pyRestTable/issues/12>`_
provide HTML table output format

:2017.2.0:

* `#9 <https://github.com/prjemian/pyRestTable/issues/9>`_
Expand Down
20 changes: 18 additions & 2 deletions src/pyRestTable/rest_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
#
Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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 = ''):
Expand Down Expand Up @@ -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 = "<table>\n"
html += ' <tr>\n' # start the labels
html += "".join([" <th>{}</th>\n".format(k) for k in self.labels]) # labels
html += ' </tr>\n' # end the labels
for row in self.rows:
html += ' <tr>\n' # start each row
# TODO: consider column and row spans
html += "".join([" <td>{}</td>\n".format(k) for k in row]) # each row
html += ' </tr>\n' # end each row
html += '</table>' # end of table
return html

def _row(self, row, fmt, indentation = ''):
"""
Given a list of entry nodes in this table row,
Expand Down
64 changes: 54 additions & 10 deletions tests/test_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,25 @@
- 2\
'''

MINIMAL_HTML_RESULT = '''\
<table>
<tr>
<th>x</th>
<th>y</th>
</tr>
<tr>
<td>1</td>
<td>2</td>
</tr>
</table>\
'''


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 = '''\
Expand Down Expand Up @@ -136,7 +147,36 @@
- 3,3
* - 4,1
- 4,2
- 4,3'''
- 4,3
\
<table>
<tr>
<th>one</th>
<th>two</th>
<th>three</th>
</tr>
<tr>
<td>1,1</td>
<td>1,2</td>
<td>1,3</td>
</tr>
<tr>
<td>2,1</td>
<td>2,2</td>
<td>2,3</td>
</tr>
<tr>
<td>3,1</td>
<td>3,2</td>
<td>3,3</td>
</tr>
<tr>
<td>4,1</td>
<td>4,2</td>
<td>4,3</td>
</tr>
</table>\
'''


EXAMPLE_COMPLICATED_RESULT = '''
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 1f7c569

Please sign in to comment.