Skip to content

Commit

Permalink
feat: MkHeader support for LinkProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
phil65 committed Sep 28, 2023
1 parent 094cd7e commit 9b4630b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 14 deletions.
12 changes: 9 additions & 3 deletions mknodes/basenodes/mknode.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import re
import time

from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Any

from mknodes.basenodes import processors
from mknodes.data import datatypes
Expand Down Expand Up @@ -151,7 +151,13 @@ def parent_navs(self) -> list[mk.MkNav]:
navs = [i for i in self.ancestors if isinstance(i, mk.MkNav)]
return list(reversed(navs))

def to_child_node(self, other): # type: ignore[return]
@property
def parent_page(self) -> mk.MkPage | None:
import mknodes as mk

return next((i for i in self.ancestors if isinstance(i, mk.MkPage)), None)

def to_child_node(self, other: Any): # type: ignore[return]
"""Convert given nodes / strings to child nodes.
Either converts text to an MkNode sets parent of node to self.
Expand All @@ -175,7 +181,7 @@ def to_child_node(self, other): # type: ignore[return]
raise TypeError(other)

@classmethod
def get_node(cls, name: str):
def get_node(cls, name: str) -> MkNode:
"""Get a node from name registry."""
return cls._name_registry[name]

Expand Down
3 changes: 2 additions & 1 deletion mknodes/buildcollector.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ def collect(self, root: mk.MkNode, theme):
logger.debug("Collecting theme requirements...")
iterator = itertools.chain(theme.iter_nodes(), root.iter_nodes())
nodes = [i[1] for i in iterator]

for node in nodes:
self.node_counter.update([node.__class__.__name__])
self.extra_files |= node.files
Expand All @@ -61,6 +60,8 @@ def collect(self, root: mk.MkNode, theme):
)

def collect_page(self, page: mk.MkPage):
if not page.inclusion_level:
return
path = page.resolved_file_path
self.mapping[path] = page
req = page.get_requirements()
Expand Down
38 changes: 28 additions & 10 deletions mknodes/info/linkprovider.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@


if TYPE_CHECKING:
import mknodes
import mknodes as mk

LinkableType = str | mknodes.MkPage | mknodes.MkNav | types.ModuleType | type
LinkableType = str | mk.MkPage | mk.MkNav | mk.MkHeader | types.ModuleType | type
"""A type which can get linked by the LinkProvider."""


Expand Down Expand Up @@ -154,7 +154,7 @@ def link_for_klass(self, kls: type) -> str:
return linked(url, qual_name)
return linked(qual_name)

def url_for_nav(self, nav: mknodes.MkNav) -> str:
def url_for_nav(self, nav: mk.MkNav) -> str:
"""Return the final URL for given MkNav.
Arguments:
Expand All @@ -167,7 +167,7 @@ def url_for_nav(self, nav: mknodes.MkNav) -> str:
path = nav.resolved_file_path
return self.base_url + path

def url_for_page(self, page: mknodes.MkPage) -> str:
def url_for_page(self, page: mk.MkPage) -> str:
"""Return the final URL for given MkPage.
Arguments:
Expand All @@ -180,6 +180,19 @@ def url_for_page(self, page: mknodes.MkPage) -> str:
path = path.replace(".md", ".html")
return self.base_url + path

def url_for_header(self, header: mk.MkHeader) -> str:
"""Return the final URL for given MkHeader.
Arguments:
header: The Header to link to
"""
page = header.parent_page
if page is None:
msg = "Need a parent page for MkHeader in order to link to it"
raise RuntimeError(msg)
suffix = "#" + helpers.slugify(header.text)
return self.url_for_page(page) + suffix

def get_link(self, target: LinkableType, title: str | None = None):
"""Return a markdown link for given target.
Expand All @@ -200,13 +213,15 @@ def get_url(self, target: LinkableType) -> str: # type: ignore # noqa: PLR0911
Arguments:
target: The thing to link to
"""
import mknodes
import mknodes as mk

match target:
case mknodes.MkPage():
case mk.MkPage():
return self.url_for_page(target)
case mknodes.MkNav():
case mk.MkNav():
return self.url_for_nav(target)
case mk.MkHeader():
return self.url_for_header(target)
case type():
return self.url_for_klass(target) or ""
case types.ModuleType():
Expand All @@ -215,9 +230,12 @@ def get_url(self, target: LinkableType) -> str: # type: ignore # noqa: PLR0911
return self.base_url.rstrip("/") + target
case str() if helpers.is_url(target):
return target
case str() if target in mknodes.MkNode._name_registry:
node = mknodes.MkNode.get_node(target)
return self.get_url(node)
case str() if target in mk.MkNode._name_registry:
node = mk.MkNode.get_node(target)
if isinstance(node, mk.MkPage | mk.MkNav):
return self.get_url(node)
msg = f"Cannot get link for {node!r}"
raise TypeError(msg)
case str():
return f"{target}.md"
case _:
Expand Down
1 change: 1 addition & 0 deletions mknodes/utils/jinjahelpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def load_file(path: str | os.PathLike) -> str:
ENVIRONMENT_FILTERS = {
"dump_yaml": yamlhelpers.dump_yaml,
"styled": helpers.styled,
"str": str,
"rstrip": str.rstrip,
"lstrip": str.lstrip,
"removesuffix": str.removesuffix,
Expand Down

0 comments on commit 9b4630b

Please sign in to comment.