#494: Fix the `maxdepth option for the toctree() template callable when used with collapse=True`.

This commit is contained in:
Georg Brandl 2010-08-23 15:59:20 +00:00
parent ab334a2a6f
commit 6ef0e9ea67
2 changed files with 45 additions and 12 deletions

View File

@ -1,6 +1,9 @@
Release 1.0.3 (in development) Release 1.0.3 (in development)
============================== ==============================
* #494: Fix the ``maxdepth`` option for the ``toctree()`` template
callable when used with ``collapse=True``.
* #507: Fix crash parsing Python argument lists containing brackets * #507: Fix crash parsing Python argument lists containing brackets
in string literals. in string literals.

View File

@ -1137,32 +1137,62 @@ class BuildEnvironment:
def _walk_depth(node, depth, maxdepth): def _walk_depth(node, depth, maxdepth):
"""Utility: Cut a TOC at a specified depth.""" """Utility: Cut a TOC at a specified depth."""
# For reading this function, it is useful to keep in mind the node
# structure of a toctree (using HTML-like node names for brevity):
#
# <ul>
# <li>
# <p><a></p>
# <p><a></p>
# ...
# <ul>
# ...
# </ul>
# </li>
# </ul>
for subnode in node.children[:]: for subnode in node.children[:]:
if isinstance(subnode, (addnodes.compact_paragraph, if isinstance(subnode, (addnodes.compact_paragraph,
nodes.list_item)): nodes.list_item)):
# for <p> and <li>, just indicate the depth level and
# recurse to children
subnode['classes'].append('toctree-l%d' % (depth-1)) subnode['classes'].append('toctree-l%d' % (depth-1))
_walk_depth(subnode, depth, maxdepth) _walk_depth(subnode, depth, maxdepth)
elif isinstance(subnode, nodes.bullet_list): elif isinstance(subnode, nodes.bullet_list):
# for <ul>, determine if the depth is too large or if the
# entry is to be collapsed
if maxdepth > 0 and depth > maxdepth: if maxdepth > 0 and depth > maxdepth:
subnode.parent.replace(subnode, []) subnode.parent.replace(subnode, [])
else: else:
# to find out what to collapse, *first* walk subitems,
# since that determines which children point to the
# current page
_walk_depth(subnode, depth+1, maxdepth) _walk_depth(subnode, depth+1, maxdepth)
# cull sub-entries whose parents aren't 'current' # cull sub-entries whose parents aren't 'current'
if (collapse and if (collapse and depth > 1 and
depth > 1 and 'iscurrent' not in subnode.parent):
'current' not in subnode.parent['classes']):
subnode.parent.remove(subnode) subnode.parent.remove(subnode)
elif isinstance(subnode, nodes.reference): elif isinstance(subnode, nodes.reference):
# identify the toc entry pointing to the current document # for <a>, identify which entries point to the current
if subnode['refuri'] == docname and \ # document and therefore may not be collapsed
not subnode['anchorname']: if subnode['refuri'] == docname:
# tag the whole branch as 'current' if not subnode['anchorname']:
p = subnode # give the whole branch a 'current' class
while p: # (useful for styling it differently)
p['classes'].append('current') branchnode = subnode
p = p.parent while branchnode:
branchnode['classes'].append('current')
branchnode = branchnode.parent
# mark the list_item as "on current page"
if subnode.parent.parent.get('iscurrent'):
# but only if it's not already done
return
while subnode:
subnode['iscurrent'] = True
subnode = subnode.parent
def _entries_from_toctree(toctreenode, separate=False, subtree=False): def _entries_from_toctree(toctreenode, separate=False, subtree=False):
"""Return TOC entries for a toctree node.""" """Return TOC entries for a toctree node."""