diff --git a/mercurial/minirst.py b/mercurial/minirst.py --- a/mercurial/minirst.py +++ b/mercurial/minirst.py @@ -553,3 +553,19 @@ def decorateblocks(blocks, width): 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) diff --git a/tests/test-minirst.py b/tests/test-minirst.py --- a/tests/test-minirst.py +++ b/tests/test-minirst.py @@ -232,14 +232,13 @@ Empty comment above 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) diff --git a/tests/test-minirst.py.out b/tests/test-minirst.py.out --- a/tests/test-minirst.py.out +++ b/tests/test-minirst.py.out @@ -388,15 +388,21 @@ Some text. 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 ----------------------------------------------------------------------