|
7 | 7 | import pydoc
|
8 | 8 | import collections
|
9 | 9 | import os
|
| 10 | +from itertools import zip_longest |
10 | 11 |
|
11 | 12 | from jinja2 import FileSystemLoader
|
12 | 13 | from jinja2.sandbox import SandboxedEnvironment
|
|
21 | 22 | sixu = lambda s: unicode(s, 'unicode_escape')
|
22 | 23 |
|
23 | 24 |
|
| 25 | +def _grid_table_rst(data_rows, header_row=None): |
| 26 | + """ |
| 27 | + >>> t = _grid_table_rst([['Hello', ['- cells', '- contain', '- blocks']], |
| 28 | + ... ['World', 'Shorter']], |
| 29 | + ... header_row=['Cell 1', 'Cell 2']) |
| 30 | + >>> print('\\n'.join(t)) |
| 31 | + +--------+-----------+ |
| 32 | + | Cell 1 | Cell 2 | |
| 33 | + +========+===========+ |
| 34 | + | Hello | - cells | |
| 35 | + | | - contain | |
| 36 | + | | - blocks | |
| 37 | + +--------+-----------+ |
| 38 | + | World | Shorter | |
| 39 | + +--------+-----------+ |
| 40 | + """ |
| 41 | + if header_row is not None: |
| 42 | + rows = [header_row] + data_rows |
| 43 | + else: |
| 44 | + rows = data_rows |
| 45 | + |
| 46 | + rule_idxs = [] |
| 47 | + lines = [] |
| 48 | + for row in rows: |
| 49 | + # TODO: check equal number of cells per row |
| 50 | + row = [cell if isinstance(cell, list) else [cell] for cell in row] |
| 51 | + lines.extend(zip_longest(*row, fillvalue='')) |
| 52 | + rule_idxs.append(len(lines)) |
| 53 | + |
| 54 | + col_widths = [max(len(l) for l in col) for col in zip(*lines)] |
| 55 | + fmt = ('| {:<%ds} ' * len(col_widths) + '|') % tuple(col_widths) |
| 56 | + |
| 57 | + lines = [fmt.format(*line) for line in lines] |
| 58 | + |
| 59 | + rule = '+%s+' % ('+'.join((w + 2) * '-' for w in col_widths)) |
| 60 | + if header_row is not None: |
| 61 | + header_idx = rule_idxs[0] |
| 62 | + else: |
| 63 | + header_idx = None |
| 64 | + rule_idxs.insert(0, 0) |
| 65 | + for idx in reversed(rule_idxs): |
| 66 | + lines.insert(idx, (rule if idx is not header_idx |
| 67 | + else rule.replace('-', '='))) |
| 68 | + |
| 69 | + return lines |
| 70 | + |
| 71 | + |
24 | 72 | class SphinxDocString(NumpyDocString):
|
25 | 73 | def __init__(self, docstring, config={}):
|
26 | 74 | NumpyDocString.__init__(self, docstring, config=config)
|
@@ -143,16 +191,12 @@ def _str_member_list(self, name):
|
143 | 191 | out += [''] + autosum
|
144 | 192 |
|
145 | 193 | if others:
|
146 |
| - maxlen_0 = max(3, max([len(x[0]) + 4 for x in others])) |
147 |
| - hdr = sixu("=") * maxlen_0 + sixu(" ") + sixu("=") * 10 |
148 |
| - fmt = sixu('%%%ds %%s ') % (maxlen_0,) |
149 |
| - out += ['', '', hdr] |
| 194 | + table = [] |
150 | 195 | for param, param_type, desc in others:
|
151 |
| - desc = sixu(" ").join(x.strip() for x in desc).strip() |
152 | 196 | if param_type:
|
153 |
| - desc = "(%s) %s" % (param_type, desc) |
154 |
| - out += [fmt % ("**" + param.strip() + "**", desc)] |
155 |
| - out += [hdr] |
| 197 | + desc = [param_type, ''] + desc |
| 198 | + table.append(["**" + param.strip() + "**", desc]) |
| 199 | + out += ['', ''] + _grid_table_rst(table) |
156 | 200 | out += ['']
|
157 | 201 | return out
|
158 | 202 |
|
|
0 commit comments