Skip to content

Commit e0ba4f2

Browse files
committed
Use grid-style tables for member lists
Supports multi-line attribute descriptions (Fixes numpy#104) - Attribute type spec is now on a separate line to description
1 parent 057ef57 commit e0ba4f2

File tree

2 files changed

+79
-19
lines changed

2 files changed

+79
-19
lines changed

numpydoc/docscrape_sphinx.py

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import pydoc
88
import collections
99
import os
10+
from itertools import zip_longest
1011

1112
from jinja2 import FileSystemLoader
1213
from jinja2.sandbox import SandboxedEnvironment
@@ -21,6 +22,53 @@
2122
sixu = lambda s: unicode(s, 'unicode_escape')
2223

2324

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+
2472
class SphinxDocString(NumpyDocString):
2573
def __init__(self, docstring, config={}):
2674
NumpyDocString.__init__(self, docstring, config=config)
@@ -143,16 +191,12 @@ def _str_member_list(self, name):
143191
out += [''] + autosum
144192

145193
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 = []
150195
for param, param_type, desc in others:
151-
desc = sixu(" ").join(x.strip() for x in desc).strip()
152196
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)
156200
out += ['']
157201
return out
158202

numpydoc/tests/test_docscrape.py

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -896,7 +896,10 @@ def test_duplicate_signature():
896896
t : float
897897
Current time.
898898
y : ndarray
899-
Current variable values.
899+
Current variable values:
900+
901+
- foo
902+
- bar
900903
x : float
901904
Some parameter
902905
@@ -933,7 +936,10 @@ def test_class_members_doc():
933936
t : float
934937
Current time.
935938
y : ndarray
936-
Current variable values.
939+
Current variable values:
940+
941+
- foo
942+
- bar
937943
x : float
938944
Some parameter
939945
@@ -982,18 +988,28 @@ def x(self):
982988
983989
x
984990
985-
===== ==========
986-
**t** (float) Current time.
987-
**y** (ndarray) Current variable values.
988-
===== ==========
991+
+-------+--------------------------+
992+
| **t** | float |
993+
| | |
994+
| | Current time. |
995+
+-------+--------------------------+
996+
| **y** | ndarray |
997+
| | |
998+
| | Current variable values: |
999+
| | |
1000+
| | - foo |
1001+
| | - bar |
1002+
+-------+--------------------------+
9891003
9901004
.. rubric:: Methods
9911005
992-
===== ==========
993-
**a**
994-
**b**
995-
**c**
996-
===== ==========
1006+
+-------+--+
1007+
| **a** | |
1008+
+-------+--+
1009+
| **b** | |
1010+
+-------+--+
1011+
| **c** | |
1012+
+-------+--+
9971013
9981014
""")
9991015

0 commit comments

Comments
 (0)