Skip to content

Commit

Permalink
chore: misc
Browse files Browse the repository at this point in the history
  • Loading branch information
phil65 committed Sep 17, 2023
1 parent 36f2708 commit 16e7387
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
22 changes: 12 additions & 10 deletions mknodes/navs/navbuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@
import os


MD_ESCAPE_CHARS = tuple("!#()*+-[\\]_`{}")


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


class NavBuilder:
"""An object representing MkDocs navigation.
Expand Down Expand Up @@ -41,24 +51,16 @@ 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]:
return self._items(self._data, 0)

@classmethod
def _items(cls, data: Mapping, level: int) -> Iterable[Item]:
for key, value in data.items():
if key is not None:
yield cls.Item(level=level, title=key, filename=value.get(None))
yield Item(level=level, title=key, filename=value.get(None))
yield from cls._items(value, level + 1)

_markdown_escape_chars = tuple("!#()*+-[\\]_`{}")

def build_literate_nav(self, indentation: int | str = "") -> Iterable[str]:
"""Convert data to a literate-nav formatted markdown list.
Expand All @@ -69,7 +71,7 @@ def build_literate_nav(self, indentation: int | str = "") -> Iterable[str]:
indentation = " " * indentation
for item in self.items():
line = item.title
if line.startswith(self._markdown_escape_chars):
if line.startswith(MD_ESCAPE_CHARS):
line = "\\" + line
if item.filename is not None:
line = f"[{line}]({item.filename})"
Expand Down
4 changes: 2 additions & 2 deletions mknodes/theme/mkblog.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ def as_dict(self):
class MkBlog(mknav.MkNav):
"""Class representing the blog provided by the MkDocs-Material blog plugin."""

def __init__(self, **kwargs):
super().__init__(**kwargs)
def __init__(self, section: str | None = "Blog", **kwargs):
super().__init__(section=section, **kwargs)
self.authors: dict[str, Author] = {}

def add_author(
Expand Down

0 comments on commit 16e7387

Please sign in to comment.