##// END OF EJS Templates
minirst: add a helper function to build an RST table from an array
Matt Mackall -
r15039:c981f4a9 default
parent child Browse files
Show More
@@ -553,3 +553,19 b' def decorateblocks(blocks, width):'
553 text = formatblocks(s[2], width)
553 text = formatblocks(s[2], width)
554 lines.append([(section, l) for l in text.splitlines(True)])
554 lines.append([(section, l) for l in text.splitlines(True)])
555 return lines
555 return lines
556
557 def maketable(data, indent=0, header=False):
558 '''Generate an RST table for the given table data'''
559
560 widths = [max(encoding.colwidth(e) for e in c) for c in zip(*data)]
561 indent = ' ' * indent
562 f = indent + ' '.join('%%-%ds' % w for w in widths) + '\n'
563 div = indent + ' '.join('=' * w for w in widths) + '\n'
564
565 out = [div]
566 for row in data:
567 out.append(f % tuple(row))
568 if header and len(data) > 1:
569 out.insert(2, div)
570 out.append(div)
571 return ''.join(out)
@@ -232,14 +232,13 b' Empty comment above'
232
232
233 debugformat('comments', comments, 30)
233 debugformat('comments', comments, 30)
234
234
235 table = """
235
236 === === ===
236 data = [['a', 'b', 'c'],
237 a b c
237 ['1', '2', '3'],
238 === === ===
238 ['foo', 'bar', 'baz this list is very very very long man']]
239 1 2 3
239
240 foo bar baz
240 table = minirst.maketable(data, 2, True)
241 aa bb sdfsdfsdf this line is way too long for this cell.
241
242 === === ===
242 print table
243 """
244
243
245 debugformat('table', table, 30)
244 debugformat('table', table, 30)
@@ -388,15 +388,21 b' Some text.'
388 Empty comment above
388 Empty comment above
389 ----------------------------------------------------------------------
389 ----------------------------------------------------------------------
390
390
391 === === ========================================
392 a b c
393 === === ========================================
394 1 2 3
395 foo bar baz this list is very very very long man
396 === === ========================================
397
391 table formatted to fit within 30 characters:
398 table formatted to fit within 30 characters:
392 ----------------------------------------------------------------------
399 ----------------------------------------------------------------------
393 a b c
400 a b c
394 ------------------------------
401 ------------------------------
395 1 2 3
402 1 2 3
396 foo bar baz
403 foo bar baz this list is
397 aa bb sdfsdfsdf this line
404 very very very long
398 is way too long for
405 man
399 this cell.
400
406
401 ----------------------------------------------------------------------
407 ----------------------------------------------------------------------
402
408
General Comments 0
You need to be logged in to leave comments. Login now