Skip to content

Commit

Permalink
refactor: get rid of show_source kwarg for decorators
Browse files Browse the repository at this point in the history
  • Loading branch information
phil65 committed Sep 19, 2023
1 parent 449b309 commit 694f9cd
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 83 deletions.
6 changes: 3 additions & 3 deletions mknodes/manual/cli_section.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,16 @@ def create_cli_section(root_nav: mk.MkNav):
page += mk.MkAdmonition(code, title=SECTION_CODE, collapsible=True, typ="quote")


@nav.route.page("build", show_source=True, icon="wrench")
@nav.route.page("build", icon="wrench")
def create_build_page(page: mk.MkPage):
page += mk.MkClickDoc("mknodes.cli:cli", prog_name="build")


@nav.route.page("serve", show_source=True, icon="web")
@nav.route.page("serve", icon="web")
def create_changelog_page(page: mk.MkPage):
page += mk.MkClickDoc("mknodes.cli:cli", prog_name="serve")


@nav.route.page("create-config", show_source=True, icon="folder-wrench")
@nav.route.page("create-config", icon="folder-wrench")
def create_coc_page(page: mk.MkPage):
page += mk.MkClickDoc("mknodes.cli:cli", prog_name="create-config")
18 changes: 9 additions & 9 deletions mknodes/manual/dev_section.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,47 +26,47 @@ def create_development_section(root_nav: mk.MkNav):
page += mk.MkCode.for_object(create_development_section, header=SECTION_CODE)


@nav.route.page("Module overview", show_source=True, icon="file-tree-outline")
@nav.route.page("Module overview", icon="file-tree-outline")
def create_module_overview_page(page: mk.MkPage):
page += mk.MkModuleOverview(maximum_depth=2)


@nav.route.page("Plugin flow", show_source=True, icon="dev-to")
@nav.route.page("Plugin flow", icon="dev-to")
def create_plugin_flow_page(page: mk.MkPage):
page += mk.MkPluginFlow()


@nav.route.page("Changelog", show_source=True, icon="format-list-group")
@nav.route.page("Changelog", icon="format-list-group")
def create_changelog_page(page: mk.MkPage):
page += mk.MkChangelog()


@nav.route.page("Code of conduct", show_source=True, icon="octicons/code-of-conduct-24")
@nav.route.page("Code of conduct", icon="octicons/code-of-conduct-24")
def create_coc_page(page: mk.MkPage):
page += mk.MkCodeOfConduct()


@nav.route.page("Contributing", show_source=True, icon="help")
@nav.route.page("Contributing", icon="help")
def create_contribute_page(page: mk.MkPage):
page += mk.MkCommitConventions()
page += mk.MkPullRequestGuidelines()


@nav.route.page("License", show_source=True, hide="toc", icon="license")
@nav.route.page("License", hide="toc", icon="license")
def create_license_page(page: mk.MkPage):
page += mk.MkLicense()


@nav.route.page("Dependencies", show_source=True, hide="toc", icon="database")
@nav.route.page("Dependencies", hide="toc", icon="database")
def create_dependencies_page(page: mk.MkPage):
page += mk.MkDependencyTable()


@nav.route.page("Development environment", show_source=True, icon="dev-to")
@nav.route.page("Development environment", icon="dev-to")
def create_dev_environment_page(page: mk.MkPage):
page += mk.MkDevEnvSetup()


@nav.route.page("Dev Tools", show_source=True, icon="wrench")
@nav.route.page("Dev Tools", icon="wrench")
def create_dev_tools_page(page: mk.MkPage):
page += mk.MkDevTools()
8 changes: 4 additions & 4 deletions mknodes/manual/internals_section.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@ def create_internals_section(root_nav: mk.MkNav):
page += mk.MkCode.for_object(create_internals_section, header=SECTION_CODE)


# @nav.route.page("Tree", show_source=True, hide="toc", icon="graph")
# @nav.route.page("Tree", hide="toc", icon="graph")
# def create_tree_page(page: mk.MkPage):
# page += mk.MkHeader("This is the tree we built up to now.", level=3)
# tree = page.root.get_tree_repr(detailed=False, max_depth=3)
# page += mk.MkCode(tree, language="")


@nav.route.page("Requirements", hide="toc", icon="puzzle-edit", show_source=True)
@nav.route.page("Requirements", hide="toc", icon="puzzle-edit")
def create_requirements_page(page: mk.MkPage):
page += mk.MkJinjaTemplate("requirements.md")


@nav.route.page("Build Log", show_source=True, hide="toc", icon="puzzle-edit")
@nav.route.page("Build Log", hide="toc", icon="puzzle-edit")
def create_log_page(page: mk.MkPage):
page += mk.MkText("log() | MkCode", is_jinja_expression=True)


@nav.route.nav("Complete code", show_source=True)
@nav.route.nav("Complete code")
def create_complete_code_section(nav: mk.MkNav):
nav.parse.module("mknodes/manual/", hide="toc")
18 changes: 6 additions & 12 deletions mknodes/manual/navs_section.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def create_navs_section(root_nav: mk.MkNav):
page += mk.MkCode(text, header="The resulting MkNav")


@nav.route.nav("From file", show_source=True)
@nav.route.nav("From file")
def create_from_file_section(nav: mk.MkNav):
"""Load an existing SUMMARY.md and attach it to given MkNav."""
# We will now demonstate loading an existing Nav tree.
Expand Down Expand Up @@ -122,7 +122,7 @@ def create_from_file_section(nav: mk.MkNav):
page += file_content_node


@nav.route.nav("From folder", show_source=True)
@nav.route.nav("From folder")
def create_from_folder_section(nav: mk.MkNav):
"""Create a MkNav based on a folder tree containing markup files."""
# We are using a part of the previous nav tree. It's a subfolder without a SUMMARY.md.
Expand All @@ -135,26 +135,21 @@ def create_from_folder_section(nav: mk.MkNav):
page += mk.MkDocStrings(mk.MkTreeView)


@pages_nav.route.page("MkClassPage", show_source=True)
@pages_nav.route.page("MkClassPage")
def create_mkclasspage_page(page: mk.MkPage):
class_page = mk.MkClassPage(mk.MkCode, inclusion_level=False)
page += mk.MkReprRawRendered(class_page)


@pages_nav.route.page("MkModulePage", show_source=True)
@pages_nav.route.page("MkModulePage")
def create_mkmodulepage_page(page: mk.MkPage):
import mkdocs.config

module_page = mk.MkModulePage(mkdocs.config, inclusion_level=False)
page += mk.MkReprRawRendered(module_page)


@pages_nav.route.page(
"Adding to MkPages",
hide="toc, nav",
status="new",
show_source=True,
)
@pages_nav.route.page("Adding to MkPages", hide="toc, nav", status="new")
def create_adding_to_mkpages_page(page: mk.MkPage):
"""Create the "Adding to MkPages" MkPage and attach it to given MkNav."""
page += mk.MkAdmonition("You can add other MkNodes to a page sequentially.")
Expand All @@ -170,7 +165,6 @@ def create_adding_to_mkpages_page(page: mk.MkPage):
search_boost=2.0,
subtitle="Subtitle",
description="Description",
show_source=True,
)
def create_metadata_page(page: mk.MkPage):
"""Create the "Metadata" MkPage and attach it to given nav."""
Expand All @@ -192,7 +186,7 @@ def create_mkdefaultwebsite_section(nav: mk.MkNav):
nav += website_nav


@nav.route.nav("MkDoc", show_source=True)
@nav.route.nav("MkDoc")
def create_mkdoc_section(nav: mk.MkNav):
"""Create the "Metadata" sub-MkNav and attach it to given nav."""
nav = nav.add_nav("MkDoc")
Expand Down
14 changes: 7 additions & 7 deletions mknodes/manual/nodes_section.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def create_class_page(kls: type[mk.MkNode], page: mk.MkPage):
page += admonition


@nav.route.nav("Base nodes", show_source=True)
@nav.route.nav("Base nodes")
def create_basic_nodes_section(nav: mk.MkNav):
"""Add a sub-MkNav containing all base node pages to given MkNav."""
klasses = [
Expand All @@ -111,7 +111,7 @@ def create_basic_nodes_section(nav: mk.MkNav):
page += mk.MkAdmonition(code, title=NODE_PAGE_TEXT, collapsible=True, typ="quote")


@nav.route.nav("Container nodes", show_source=True)
@nav.route.nav("Container nodes")
def create_container_nodes_section(nav: mk.MkNav):
"""Add a sub-MkNav containing all template node pages to given MkNav."""
klasses = [
Expand All @@ -137,7 +137,7 @@ def create_container_nodes_section(nav: mk.MkNav):
page += mk.MkAdmonition(code, title=NODE_PAGE_TEXT, collapsible=True, typ="quote")


@nav.route.nav("Presentation nodes", show_source=True)
@nav.route.nav("Presentation nodes")
def create_presentation_nodes_section(nav: mk.MkNav):
"""Add a sub-MkNav containing all template node pages to given MkNav."""
klasses = [
Expand All @@ -153,7 +153,7 @@ def create_presentation_nodes_section(nav: mk.MkNav):
page += mk.MkAdmonition(code, title=NODE_PAGE_TEXT, collapsible=True, typ="quote")


@nav.route.nav("Documentation nodes", show_source=True)
@nav.route.nav("Documentation nodes")
def create_documentation_nodes_section(nav: mk.MkNav):
"""Add a sub-MkNav containing all template node pages to given MkNav."""
klasses = [
Expand All @@ -173,7 +173,7 @@ def create_documentation_nodes_section(nav: mk.MkNav):
page += mk.MkAdmonition(code, title=NODE_PAGE_TEXT, collapsible=True, typ="quote")


@nav.route.nav("About-the-project nodes", show_source=True)
@nav.route.nav("About-the-project nodes")
def create_about_nodes_section(nav: mk.MkNav):
"""Add a sub-MkNav containing all template node pages to given MkNav."""
klasses = [
Expand All @@ -199,7 +199,7 @@ def create_about_nodes_section(nav: mk.MkNav):
page += mk.MkAdmonition(code, title=NODE_PAGE_TEXT, collapsible=True, typ="quote")


@nav.route.nav("Special nodes", show_source=True)
@nav.route.nav("Special nodes")
def create_special_nodes_section(nav: mk.MkNav):
"""Add a sub-MkNav containing all template node pages to given MkNav."""
klasses = [mk.MkSnippet, mk.MkInclude, mk.MkIFrame, mk.MkCommandOutput, mk.MkCallable]
Expand All @@ -209,7 +209,7 @@ def create_special_nodes_section(nav: mk.MkNav):
page += mk.MkAdmonition(code, title=NODE_PAGE_TEXT, collapsible=True, typ="quote")


@nav.route.nav("Block nodes", show_source=True)
@nav.route.nav("Block nodes")
def create_block_nodes_section(nav: mk.MkNav):
"""Add a sub-MkNav containing all template node pages to given MkNav."""
klasses = [
Expand Down
6 changes: 3 additions & 3 deletions mknodes/manual/routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ def create_routing_section(root_nav: mk.MkNav):
page += mk.MkDocStrings(navrouter.NavRouter, header="MkNav.route Docstrings")


@nav.route.page("Routed page", show_source=True)
@nav.route.page("Routed page")
def routed_page(page: mk.MkPage):
"""Builds a MkPage and attaches it to the router MkNav."""
page += mk.MkAdmonition("I'm a page added via decorators!")


@nav.route.page("Routed", "Deeply", "Nested", "Nested page", show_source=True)
@nav.route.page("Routed", "Deeply", "Nested", "Nested page")
def routed_nested_page(page: mk.MkPage):
"""Builds a nested MkPage and attaches it to the router MkNav."""
page += mk.MkAdmonition("I'm a nested page added via decorators!")


@nav.route.nav("Routed", "Deeply", "Nested", "Nav", show_source=True)
@nav.route.nav("Routed", "Deeply", "Nested", "Nav")
def routed_nav(nav: mk.MkNav):
"""Builds a nested MkNav and attaches it to the router MkNav."""
index_page = nav.add_index_page()
Expand Down
4 changes: 2 additions & 2 deletions mknodes/manual/use_cases_section.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def create_use_cases_section(root_nav: mk.MkNav):
page += mk.MkAdmonition(code, title=SECTION_CODE, collapsible=True)


@nav.route.page("Creating a sample website", show_source=True, hide="toc")
@nav.route.page("Creating a sample website", hide="toc")
def create_default_website_section(page: mk.MkPage):
"""Create the "Creating a sample website" MkPage."""
page += mk.MkText("The following config...")
Expand All @@ -64,7 +64,7 @@ def create_default_website_section(page: mk.MkPage):
page += mk.MkLink(url, as_button=True, icon="material/web")


@nav.route.page("Creating a website via config", show_source=True, hide="toc")
@nav.route.page("Creating a website via config", hide="toc")
def create_custom_website_by_config(page: mk.MkPage):
"""Create the "Creating a sample website via config" MkPage."""
page += mk.MkText("The following config...")
Expand Down
48 changes: 5 additions & 43 deletions mknodes/navs/navrouter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from collections.abc import Callable
from typing import TYPE_CHECKING, Any

from mknodes.basenodes import mkadmonition, mkcode, mklink
from mknodes.basenodes import mklink
from mknodes.navs import mknav
from mknodes.pages import mkpage
from mknodes.utils import log
Expand All @@ -29,7 +29,6 @@ def __init__(self, nav: mknav.MkNav):
def __call__(
self,
*path: str,
show_source: bool = False,
) -> Callable[[Callable], Callable]:
"""Decorator method to use for routing.
Expand All @@ -41,29 +40,15 @@ def __call__(
Arguments:
path: The section path for the returned `MkNav` / `MkPage`
show_source: If True, a section containing the code will be inserted
at the start of the page (or index page if node is a Nav)
"""

def decorator(fn: Callable[..., NavSubType], path=path) -> Callable:
node = fn()
node.parent = self._nav
if show_source and isinstance(node, mkpage.MkPage):
if isinstance(node, mkpage.MkPage):
node.created_by = fn
code = mkcode.MkCode.for_object(fn, header="Code for this page")
code.parent = node
node.items.insert(0, code)
elif show_source and isinstance(node, mknav.MkNav) and node.index_page:
elif isinstance(node, mknav.MkNav) and node.index_page:
node.index_page.created_by = fn
code = mkcode.MkCode.for_object(fn)
details = mkadmonition.MkAdmonition(
code,
title="Code for this section",
collapsible=True,
typ="quote",
)
details.parent = node.index_page
node.index_page.items.append(details)
self._nav.nav[path] = node
return fn

Expand All @@ -72,7 +57,6 @@ def decorator(fn: Callable[..., NavSubType], path=path) -> Callable:
def nav(
self,
*path: str,
show_source: bool = False,
**kwargs: Any,
) -> Callable[[Callable], Callable]:
"""Decorator method to use for routing Navs.
Expand All @@ -91,8 +75,6 @@ def _(nav: mk.MkNav):
Arguments:
path: The section path for the returned MkNav
show_source: If True, a section containing the code will be inserted
at the end of the index page of the MkNav)
kwargs: Keyword arguments passed to the MkNav constructor.
"""

Expand All @@ -104,16 +86,8 @@ def decorator(
node = mknav.MkNav(path[-1], parent=self._nav, **kwargs)
node = fn(node) or node
node.parent = self._nav # in case a new MkPage was generated
if show_source and node.index_page:
if node.index_page:
node.index_page.created_by = fn
code = mkcode.MkCode.for_object(fn)
details = mkadmonition.MkAdmonition(
code,
title="Code for this section",
collapsible=True,
typ="quote",
)
node.index_page.append(details)
self._nav.nav[path] = node
return fn

Expand All @@ -122,7 +96,6 @@ def decorator(
def page(
self,
*path: str,
show_source: bool = False,
**kwargs: Any,
) -> Callable[[Callable], Callable]:
"""Decorator method to use for routing Pages.
Expand All @@ -141,8 +114,6 @@ def _(page: mk.MkPage):
Arguments:
path: The section path for the returned MkPage
show_source: If True, a section containing the code will be inserted
at the end of the page
kwargs: Keyword arguments passed to the MkPage constructor.
"""

Expand All @@ -154,16 +125,7 @@ def decorator(
node = mkpage.MkPage(path[-1], parent=self._nav, **kwargs)
node = fn(node) or node
node.parent = self._nav # in case a new MkPage was generated
if show_source:
node.created_by = fn
code = mkcode.MkCode.for_object(fn)
details = mkadmonition.MkAdmonition(
code,
title="Code for this page",
collapsible=True,
typ="quote",
)
node.append(details)
node.created_by = fn
self._nav.nav[path] = node
return fn

Expand Down
Loading

0 comments on commit 694f9cd

Please sign in to comment.