view textshaper/tablify.py @ 36:55e0579e2143

STUB: textshaper/main.py textshaper/whitespace.py
author Jeff Hammel <k0scist@gmail.com>
date Sat, 03 May 2014 14:47:16 -0700
parents 5ffa0dbcb7fe
children 8cf9a26b9aaa
line wrap: on
line source

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