Skip to content

Use grid-style tables for member lists #105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 55 additions & 8 deletions numpydoc/docscrape_sphinx.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
import pydoc
import collections
import os
try:
from itertools import zip_longest
except ImportError:
from itertools import izip_longest as zip_longest

from jinja2 import FileSystemLoader
from jinja2.sandbox import SandboxedEnvironment
Expand All @@ -21,6 +25,53 @@
sixu = lambda s: unicode(s, 'unicode_escape')


def _grid_table_rst(data_rows, header_row=None):
"""
>>> t = _grid_table_rst([['Hello', ['- cells', '- contain', '- blocks']],
... ['World', 'Shorter']],
... header_row=['Cell 1', 'Cell 2'])
>>> print('\\n'.join(t))
+--------+-----------+
| Cell 1 | Cell 2 |
+========+===========+
| Hello | - cells |
| | - contain |
| | - blocks |
+--------+-----------+
| World | Shorter |
+--------+-----------+
"""
if header_row is not None:
rows = [header_row] + data_rows
else:
rows = data_rows

rule_idxs = []
lines = []
for row in rows:
# TODO: check equal number of cells per row
row = [cell if isinstance(cell, list) else [cell] for cell in row]
lines.extend(zip_longest(*row, fillvalue=''))
rule_idxs.append(len(lines))

col_widths = [max(len(l) for l in col) for col in zip(*lines)]
fmt = ('| {:<%ds} ' * len(col_widths) + '|') % tuple(col_widths)

lines = [fmt.format(*line) for line in lines]

rule = '+%s+' % ('+'.join((w + 2) * '-' for w in col_widths))
if header_row is not None:
header_idx = rule_idxs[0]
else:
header_idx = None
rule_idxs.insert(0, 0)
for idx in reversed(rule_idxs):
lines.insert(idx, (rule if idx is not header_idx
else rule.replace('-', '=')))

return lines


class SphinxDocString(NumpyDocString):
def __init__(self, docstring, config={}):
NumpyDocString.__init__(self, docstring, config=config)
Expand Down Expand Up @@ -143,16 +194,12 @@ def _str_member_list(self, name):
out += [''] + autosum

if others:
maxlen_0 = max(3, max([len(x[0]) + 4 for x in others]))
hdr = sixu("=") * maxlen_0 + sixu(" ") + sixu("=") * 10
fmt = sixu('%%%ds %%s ') % (maxlen_0,)
out += ['', '', hdr]
table = []
for param, param_type, desc in others:
desc = sixu(" ").join(x.strip() for x in desc).strip()
if param_type:
desc = "(%s) %s" % (param_type, desc)
out += [fmt % ("**" + param.strip() + "**", desc)]
out += [hdr]
desc = [param_type, ''] + desc
table.append(["**" + param.strip() + "**", desc])
out += ['', ''] + _grid_table_rst(table)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason to use grid tables? simple tables are supposed to support multi-line rows:
http://docutils.sourceforge.net/docs/ref/rst/restructuredtext.html#tables

(unless that might be too fragile?)

out += ['']
return out

Expand Down
38 changes: 27 additions & 11 deletions numpydoc/tests/test_docscrape.py
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,10 @@ def test_duplicate_signature():
t : float
Current time.
y : ndarray
Current variable values.
Current variable values:

- foo
- bar
x : float
Some parameter

Expand Down Expand Up @@ -933,7 +936,10 @@ def test_class_members_doc():
t : float
Current time.
y : ndarray
Current variable values.
Current variable values:

- foo
- bar
x : float
Some parameter

Expand Down Expand Up @@ -982,18 +988,28 @@ def x(self):

x

===== ==========
**t** (float) Current time.
**y** (ndarray) Current variable values.
===== ==========
+-------+--------------------------+
| **t** | float |
| | |
| | Current time. |
+-------+--------------------------+
| **y** | ndarray |
| | |
| | Current variable values: |
| | |
| | - foo |
| | - bar |
+-------+--------------------------+

.. rubric:: Methods

===== ==========
**a**
**b**
**c**
===== ==========
+-------+--+
| **a** | |
+-------+--+
| **b** | |
+-------+--+
| **c** | |
+-------+--+

""")

Expand Down