Skip to content

Commit b5d3c69

Browse files
committed
refactor: Pull _insert_contents() into top-level function
1 parent ddd25d5 commit b5d3c69

File tree

1 file changed

+33
-16
lines changed

1 file changed

+33
-16
lines changed

quartodoc/autosummary.py

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,38 @@ def _is_valueless(obj: dc.Object):
399399
return False
400400

401401

402+
def _insert_contents(
403+
x: dict | list,
404+
contents: list,
405+
sentinel: str = "{{ contents }}",
406+
):
407+
"""Splice `contents` into a list.
408+
409+
Splices `contents` into the first element in `x` that exactly matches
410+
`sentinel`. This functions recurses into dictionaries, but because
411+
`contents` is a list, we only look within lists in `x` for the sentinel
412+
value where `contents` should be inserted.
413+
414+
Returns
415+
-------
416+
:
417+
Returns `True` if `contents` was inserted into `x`, otherwise returns
418+
`False`. Note that `x` is modified in place.
419+
"""
420+
if isinstance(x, dict):
421+
for value in x.values():
422+
if _insert_contents(value, contents):
423+
return True
424+
elif isinstance(x, list):
425+
for i, item in enumerate(x):
426+
if item == sentinel:
427+
x[i : i + 1] = contents # noqa: E203
428+
return True
429+
elif _insert_contents(item, contents):
430+
return True
431+
return False
432+
433+
402434
# pkgdown =====================================================================
403435

404436

@@ -713,22 +745,7 @@ def _generate_sidebar(
713745
if not isinstance(sidebar["contents"], list):
714746
raise TypeError("`sidebar.contents` must be a list")
715747

716-
def splice_contents_recursive(sidebar, contents):
717-
"""Splice quartodoc contents into first element exactly '{{ contents }}'"""
718-
if isinstance(sidebar, dict):
719-
for value in sidebar.values():
720-
if splice_contents_recursive(value, contents):
721-
return True
722-
elif isinstance(sidebar, list):
723-
for i, item in enumerate(sidebar):
724-
if item == "{{ contents }}":
725-
sidebar[i : i + 1] = contents # noqa: E203
726-
return True
727-
elif splice_contents_recursive(item, contents):
728-
return True
729-
return False
730-
731-
if not splice_contents_recursive(sidebar["contents"], contents):
748+
if not _insert_contents(sidebar["contents"], contents):
732749
# otherwise append contents to existing list
733750
sidebar["contents"].extend(contents)
734751

0 commit comments

Comments
 (0)