Skip to content

Commit

Permalink
chore: rework examples
Browse files Browse the repository at this point in the history
  • Loading branch information
phil65 committed Nov 14, 2023
1 parent 2c57d2c commit 8aa5728
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 34 deletions.
15 changes: 0 additions & 15 deletions mknodes/basenodes/mknode/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,21 +350,6 @@ def get_resources(self) -> resources.Resources:
req.merge(node_req)
return req

@classmethod
def create_example_page(cls, page):
import mknodes as mk

# We dont instanciate MkNode directly, so we take a subclass
# to show some base class functionality

node = mk.MkText("Intro\n# A header\nOutro")
node.shift_header_levels = 2
page += mk.MkReprRawRendered(node, header="### Shift header levels")

node = mk.MkText("Every node can also append annotations (1)")
node.annotations[1] = "Nice!"
page += mk.MkReprRawRendered(node, header="### Append annotations")

@classmethod
def for_project(cls, project=None, **kwargs):
if project:
Expand Down
39 changes: 20 additions & 19 deletions mknodes/manual/nodes_section.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,28 +93,29 @@ def create_section_for_nodes(
) -> mk.MkTable:
table = mk.MkTable(columns=["Node", "Docstrings", "Markdown extensions"])
for kls in klasses:
if "create_example_page" in kls.__dict__:
page = nav.add_page(kls.__name__, icon=kls.ICON)
create_class_page(kls, page)
link = mk.MkLink(page, kls.__name__, icon=kls.ICON)
extensions = ", ".join(f"`{i}`" for i in kls.REQUIRED_EXTENSIONS)
table.add_row((link, kls.__doc__, extensions))
if not kls.nodefile:
continue
page = nav.add_page(kls.__name__, icon=kls.ICON)
page += "## Examples"
examples = kls.nodefile.get_examples(page)
for k, v in examples.items():
page += mk.MkHeader(f"Example: **{k}**")
page += mk.MkAdmonition(v.pop("Jinja"), title="Jinja")
page += mk.MkAdmonition(v.pop("Repr"), title="Python")
page += mk.MkTabbed(v)
if kls.STATUS:
page.metadata.status = kls.STATUS
elif kls.JS_FILES:
page.metadata.status = "js"
elif kls.CSS:
page.metadata.status = "css"
page.created_by = create_section_for_nodes
link = mk.MkLink(page, kls.__name__, icon=kls.ICON)
extensions = ", ".join(f"`{i}`" for i in kls.REQUIRED_EXTENSIONS)
table.add_row((link, kls.__doc__, extensions))
return table


def create_class_page(kls: type[mk.MkNode], page: mk.MkPage):
page += mk.MkCode.for_object(kls.create_example_page, extract_body=True)
page += "## Examples"
if kls.STATUS:
page.metadata.status = kls.STATUS
elif kls.JS_FILES:
page.metadata.status = "js"
elif kls.CSS:
page.metadata.status = "css"
kls.create_example_page(page)
page.created_by = create_class_page


nav = mk.MkNav("The nodes")


Expand Down

0 comments on commit 8aa5728

Please sign in to comment.