Skip to content

Commit

Permalink
chore: change show_code_admonition setting to a more general show_pag…
Browse files Browse the repository at this point in the history
…e_info setting
  • Loading branch information
phil65 committed Sep 28, 2023
1 parent e147451 commit e80afab
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 23 deletions.
2 changes: 1 addition & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ plugins:
- literate-nav
- mknodes:
build_fn: mknodes.manual.root:build
show_code_admonition: true
show_page_info: true
# build_fn: mknodes.navs.mkdefaultwebsite:MkDefaultWebsite.for_project
# repo_path: https://github.com/pallets/jinja2.git
- mkdocstrings:
Expand Down
2 changes: 1 addition & 1 deletion mknodes/basenodes/mkadmonition.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def __init__(
self,
content: str | list | mknode.MkNode,
*,
typ: datatypes.AdmonitionTypeStr = "info",
typ: datatypes.AdmonitionTypeStr | str = "info",
title: str | None = None,
collapsible: bool = False,
expanded: bool = False,
Expand Down
41 changes: 28 additions & 13 deletions mknodes/buildcollector.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
class BuildCollector:
"""MkNodes Project."""

def __init__(self, nodes: list[mk.MkNode], show_code_admonition: bool = False):
def __init__(self, nodes: list[mk.MkNode], show_page_info: bool = False):
"""The main project to create a website.
Arguments:
nodes: The theme to use
show_code_admonition: Add a code admonition box to each page.
show_page_info: Add a code admonition box to each page.
"""
self.nodes = nodes
self.show_code_admonition = show_code_admonition
self.show_page_info = show_page_info
self.node_files: dict[str, str | bytes] = {}
self.extra_files: dict[str, str | bytes] = {}
for node in self.nodes:
Expand All @@ -34,16 +34,7 @@ def __init__(self, nodes: list[mk.MkNode], show_code_admonition: bool = False):

def collect_page(self, page: mk.MkPage):
path = page.resolved_file_path
if self.show_code_admonition and page.created_by:
code = mk.MkCode.for_object(page.created_by)
typ = "section" if page.is_index() else "page"
details = mk.MkAdmonition(
code,
title=f"Code for this {typ}",
collapsible=True,
typ="quote",
)
page.append(details)
req = page.get_requirements()
if page.inclusion_level:
if page.template:
node_path = pathlib.Path(path)
Expand All @@ -62,6 +53,30 @@ def collect_page(self, page: mk.MkPage):
parent_path = p.with_suffix(".html").as_posix()
page.template.extends = parent_path
break
if self.show_page_info:
adm = mk.MkAdmonition([], title="Page info", typ="theme", collapsible=True)

if page.created_by:
typ = "section" if page.is_index() else "page"
details = mk.MkAdmonition(
mk.MkCode.for_object(page.created_by),
title=f"Code for this {typ}",
collapsible=True,
typ="quote",
)
adm += details

title = "Requirements"
pretty = mk.MkPrettyPrint(req)
details = mk.MkAdmonition(pretty, title=title, collapsible=True, typ="quote")
adm += details

title = "Metadata"
code = mk.MkCode(str(page.metadata), language="yaml")
details = mk.MkAdmonition(code, title=title, collapsible=True, typ="quote")
adm += details

page += adm
md = page.to_markdown()
self.node_files[path] = md

Expand Down
2 changes: 1 addition & 1 deletion mknodes/plugin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def on_config(self, config: MkDocsConfig):
clone_depth=self.config.clone_depth,
)
logger.info("Generating pages...")
self.build_info = self.project.build(self.config.show_code_admonition)
self.build_info = self.project.build(self.config.show_page_info)
# now we add our stuff to the MkDocs build environment
cfg = mkdocsconfig.Config(config)

Expand Down
13 changes: 8 additions & 5 deletions mknodes/plugin/pluginconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,14 @@ class PluginConfig(base.Config):
"""Folder to create the Markdown files in.
If no folder is set, **MkNodes** will generate a temporary dir."""
show_code_admonition = c.Type(bool, default=False)
"""Append an admonition box with the generator code to each page.
show_page_info = c.Type(bool, default=False)
"""Append an admonition box with build-related information.
If True, all pages get added an expandable admonition box at the bottom,
containing the code which generated the page.
In order for this to work, the page needs to be created via decorators, or
the `generated_by` attribute of the MkPage needs to be set manually.
containing information about the created page.
This includes:
- Metadata
- Requirements
- Code which created the page (needs the page to be created via decorators, or
the `generated_by` attribute of the MkPage needs to be set manually)
"""
4 changes: 2 additions & 2 deletions mknodes/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def __init__(
self.build_fn = classhelpers.to_callable(build_fn)
self.build_kwargs = build_kwargs or {}

def build(self, show_code_admonition: bool = False):
def build(self, show_page_info: bool = False):
logger.debug("Building page...")
self.build_fn(project=self, **self.build_kwargs)
logger.debug("Finished building page.")
Expand Down Expand Up @@ -118,7 +118,7 @@ def build(self, show_code_admonition: bool = False):
if root := self._root:
iterator = itertools.chain(iterator, root.iter_nodes())
nodes = [i[1] for i in iterator]
collector = BuildCollector(nodes, show_code_admonition)
collector = BuildCollector(nodes, show_page_info)
ctx.build_files = collector.node_files | collector.extra_files
jinjahelpers.set_markdown_exec_namespace(self.env.globals)
return ctx
Expand Down

0 comments on commit e80afab

Please sign in to comment.