diff options
| -rw-r--r-- | sphinx/ext/autosummary/__init__.py | 26 | ||||
| -rw-r--r-- | tests/test_autosummary.py | 8 |
2 files changed, 29 insertions, 5 deletions
diff --git a/sphinx/ext/autosummary/__init__.py b/sphinx/ext/autosummary/__init__.py index aad8b9f6..82637583 100644 --- a/sphinx/ext/autosummary/__init__.py +++ b/sphinx/ext/autosummary/__init__.py @@ -322,13 +322,29 @@ class Autosummary(Directive): def mangle_signature(sig, max_chars=30): """Reformat a function signature to a more compact form.""" - sig = re.sub(r"^\((.*)\)$", r"\1", sig) + ", " - r = re.compile(r"(?P<name>[a-zA-Z0-9_*]+)(?P<default>=.*?)?, ") - items = r.findall(sig) + s = re.sub(r"^\((.*)\)$", r"\1", sig).strip() + + # Strip strings (which can contain things that confuse the code below) + s = re.sub(r"\\\\", "", s) + s = re.sub(r"\\'", "", s) + s = re.sub(r"'[^']*'", "", s) + + # Parse the signature to arguments + options + args = [] + opts = [] + + opt_re = re.compile(r"^(.*, |)([a-zA-Z0-9_*]+)=") + while s: + m = opt_re.search(s) + if not m: + # The rest are arguments + args = s.split(', ') + break - args = [name for name, default in items if not default] - opts = [name for name, default in items if default] + opts.insert(0, m.group(2)) + s = m.group(1)[:-2] + # Produce a more compact signature sig = limited_join(", ", args, max_chars=max_chars-2) if opts: if not sig: diff --git a/tests/test_autosummary.py b/tests/test_autosummary.py index 7783cc7b..048940d0 100644 --- a/tests/test_autosummary.py +++ b/tests/test_autosummary.py @@ -23,6 +23,14 @@ def test_mangle_signature(): :: (a, b[, aaa, bbb, ccc, ...]) (a, b, c=(), d=<foo>) :: (a, b[, c, d]) (a, b, c='foobar()', d=123) :: (a, b[, c, d]) + (a, b[, c]) :: (a, b[, c]) + (a, b[, cxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx]) :: (a, b[, ...) + (a, b='c=d, e=f, g=h', c=3) :: (a[, b, c]) + (a, b='c=d, \\'e=f,\\' g=h', c=3) :: (a[, b, c]) + (a, b='c=d, ', e='\\\\' g=h, c=3) :: (a[, b, e, c]) + (a, b={'c=d, ': 3, '\\\\': 3}) :: (a[, b]) + (a=1, b=2, c=3) :: ([a, b, c]) + (a=1, b=<SomeClass: a, b, c>, c=3) :: ([a, b, c]) """ TEST = [map(lambda x: x.strip(), x.split("::")) for x in TEST.split("\n") |
