Skip to content

Commit

Permalink
refactor: Pull _insert_contents() into top-level function
Browse files Browse the repository at this point in the history
  • Loading branch information
gadenbuie committed Oct 18, 2024
1 parent ddd25d5 commit b5d3c69
Showing 1 changed file with 33 additions and 16 deletions.
49 changes: 33 additions & 16 deletions quartodoc/autosummary.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,38 @@ def _is_valueless(obj: dc.Object):
return False


def _insert_contents(
x: dict | list,
contents: list,
sentinel: str = "{{ contents }}",
):
"""Splice `contents` into a list.
Splices `contents` into the first element in `x` that exactly matches
`sentinel`. This functions recurses into dictionaries, but because
`contents` is a list, we only look within lists in `x` for the sentinel
value where `contents` should be inserted.
Returns
-------
:
Returns `True` if `contents` was inserted into `x`, otherwise returns
`False`. Note that `x` is modified in place.
"""
if isinstance(x, dict):
for value in x.values():
if _insert_contents(value, contents):
return True
elif isinstance(x, list):
for i, item in enumerate(x):
if item == sentinel:
x[i : i + 1] = contents # noqa: E203
return True
elif _insert_contents(item, contents):
return True
return False


# pkgdown =====================================================================


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

def splice_contents_recursive(sidebar, contents):
"""Splice quartodoc contents into first element exactly '{{ contents }}'"""
if isinstance(sidebar, dict):
for value in sidebar.values():
if splice_contents_recursive(value, contents):
return True
elif isinstance(sidebar, list):
for i, item in enumerate(sidebar):
if item == "{{ contents }}":
sidebar[i : i + 1] = contents # noqa: E203
return True
elif splice_contents_recursive(item, contents):
return True
return False

if not splice_contents_recursive(sidebar["contents"], contents):
if not _insert_contents(sidebar["contents"], contents):
# otherwise append contents to existing list
sidebar["contents"].extend(contents)

Expand Down

0 comments on commit b5d3c69

Please sign in to comment.