Mercurial > hg > TextShaper
changeset 35:5ffa0dbcb7fe
from k0s.org/hg/config
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Thu, 13 Mar 2014 13:53:41 -0700 |
parents | 88a69d587326 |
children | 55e0579e2143 |
files | textshaper/tablify.py |
diffstat | 1 files changed, 35 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/textshaper/tablify.py Thu Mar 13 13:53:41 2014 -0700 @@ -0,0 +1,35 @@ +def format_cols(rows, header=None, right_align=()): + if header: + raise NotImplementedError('TODO') # -> record TODO items + if not rows: + return # XXX header + assert len(set([len(row) for row in rows])) == 1 + rows = [[str(col) for col in row] + for row in rows] + lengths = [max([len(row[i]) for row in rows]) + for i in range(len(rows[0]))] + rows = [[col + ' '*(length-len(col)) for col, length in zip(row, lengths)] + for row in rows] + return rows + + +def tablify(table_lines, header=True): + """=> HTML table""" + table = '<table>\n' + if header: + tag, invtag = '<th> ', ' </th>' + else: + tag, invtag = '<td> ', ' </td>' + if not hasattr(table_lines, '__iter__'): + table_lines = ( table_lines, ) + for i in table_lines: + table += '<tr>' + if not hasattr(i, '__iter__'): + i = (i,) + for j in i: + table += tag + str(j) + invtag + table += '</tr>\n' + tag = '<td> ' + invtag = ' </td>' + table += '</table>' + return table