Mercurial > hg-stable
changeset 15039:c981f4a9ea74
minirst: add a helper function to build an RST table from an array
author | Matt Mackall <mpm@selenic.com> |
---|---|
date | Thu, 11 Aug 2011 22:40:43 -0500 |
parents | 3f4d337cb80a |
children | 3afe5edda4e3 |
files | mercurial/minirst.py tests/test-minirst.py tests/test-minirst.py.out |
diffstat | 3 files changed, 34 insertions(+), 13 deletions(-) [+] |
line wrap: on
line diff
--- a/mercurial/minirst.py Thu Aug 11 22:40:41 2011 -0500 +++ b/mercurial/minirst.py Thu Aug 11 22:40:43 2011 -0500 @@ -553,3 +553,19 @@ text = formatblocks(s[2], width) lines.append([(section, l) for l in text.splitlines(True)]) return lines + +def maketable(data, indent=0, header=False): + '''Generate an RST table for the given table data''' + + widths = [max(encoding.colwidth(e) for e in c) for c in zip(*data)] + indent = ' ' * indent + f = indent + ' '.join('%%-%ds' % w for w in widths) + '\n' + div = indent + ' '.join('=' * w for w in widths) + '\n' + + out = [div] + for row in data: + out.append(f % tuple(row)) + if header and len(data) > 1: + out.insert(2, div) + out.append(div) + return ''.join(out)
--- a/tests/test-minirst.py Thu Aug 11 22:40:41 2011 -0500 +++ b/tests/test-minirst.py Thu Aug 11 22:40:43 2011 -0500 @@ -232,14 +232,13 @@ debugformat('comments', comments, 30) -table = """ - === === === - a b c - === === === - 1 2 3 - foo bar baz - aa bb sdfsdfsdf this line is way too long for this cell. - === === === -""" + +data = [['a', 'b', 'c'], + ['1', '2', '3'], + ['foo', 'bar', 'baz this list is very very very long man']] + +table = minirst.maketable(data, 2, True) + +print table debugformat('table', table, 30)
--- a/tests/test-minirst.py.out Thu Aug 11 22:40:41 2011 -0500 +++ b/tests/test-minirst.py.out Thu Aug 11 22:40:43 2011 -0500 @@ -388,15 +388,21 @@ Empty comment above ---------------------------------------------------------------------- + === === ======================================== + a b c + === === ======================================== + 1 2 3 + foo bar baz this list is very very very long man + === === ======================================== + table formatted to fit within 30 characters: ---------------------------------------------------------------------- a b c ------------------------------ 1 2 3 - foo bar baz - aa bb sdfsdfsdf this line - is way too long for - this cell. + foo bar baz this list is + very very very long + man ----------------------------------------------------------------------