Skip to content

Commit

Permalink
Expand documentation about nav generation
Browse files Browse the repository at this point in the history
  • Loading branch information
oprypin committed Aug 9, 2023
1 parent e3db5e7 commit 8731626
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
2 changes: 2 additions & 0 deletions docs/extras.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@
options:
show_root_heading: false
show_root_toc_entry: false
members_order: source
group_by_category: false
22 changes: 16 additions & 6 deletions mkdocs_gen_files/nav.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,8 @@ def __setitem__(self, keys: str | tuple[str, ...], value: str):
cur = cur.setdefault(key, {})
cur[None] = os.fspath(value)

@dataclasses.dataclass
class Item:
level: int
title: str
filename: str | None

def items(self) -> Iterable[Item]:
"""Allows viewing the nav as a flattened sequence."""
return self._items(self._data, 0)

@classmethod
Expand All @@ -60,6 +55,10 @@ def _items(cls, data: Mapping, level: int) -> Iterable[Item]:
_markdown_escape_chars = tuple("!#()*+-[\\]_`{}")

def build_literate_nav(self, indentation: int | str = "") -> Iterable[str]:
"""Builds a file suitable for https://github.com/oprypin/mkdocs-literate-nav, as a sequence of lines to be written into a file.
For an example, see https://mkdocstrings.github.io/recipes/#generate-a-literate-navigation-file
"""
if isinstance(indentation, int):
indentation = " " * indentation
for item in self.items():
Expand All @@ -69,3 +68,14 @@ def build_literate_nav(self, indentation: int | str = "") -> Iterable[str]:
if item.filename is not None:
line = f"[{line}]({item.filename})"
yield indentation + " " * item.level + "* " + line + "\n"

@dataclasses.dataclass
class Item:
"""An item in the navigation."""

level: int
"""The nestedness level of the item. 0 is the topmost level."""
title: str
"""The title of the item."""
filename: str | None
"""The path the item links to (or it can be a section index without a link)."""

0 comments on commit 8731626

Please sign in to comment.