# HG changeset patch # User Jeff Hammel # Date 1394744021 25200 # Node ID 5ffa0dbcb7fe5eae503d6c87388d7728160223bf # Parent 88a69d5873263980064a17ea6277c0e1c793372d from k0s.org/hg/config diff -r 88a69d587326 -r 5ffa0dbcb7fe textshaper/tablify.py --- /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 = '\n' + if header: + tag, invtag = '' + else: + tag, invtag = '' + if not hasattr(table_lines, '__iter__'): + table_lines = ( table_lines, ) + for i in table_lines: + table += '' + if not hasattr(i, '__iter__'): + i = (i,) + for j in i: + table += tag + str(j) + invtag + table += '\n' + tag = '' + table += '
', ' ', '
' + invtag = '
' + return table