Skip to content

Commit

Permalink
Add support for wiki-style links
Browse files Browse the repository at this point in the history
  • Loading branch information
arjun-menon committed Nov 26, 2024
1 parent 87004ea commit 6011ca4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 24 deletions.
2 changes: 2 additions & 0 deletions alteza/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ def path(name: str) -> str:

# Perform initial Markdown processing:
if isinstance(pyPageNode, Md):
PyPageNode.temporal_link = link
mdResult = Md.processMarkdown(pyPageOutput)
PyPageNode.temporal_link = None
env.update(mdResult.metadata)
pyPageOutput = mdResult.html

Expand Down
61 changes: 37 additions & 24 deletions alteza/fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import markdown
import yaml
from colored import Style, Fore # type: ignore
from markdown.extensions.wikilinks import WikiLinkExtension

colored_logs = True

Expand Down Expand Up @@ -302,6 +303,8 @@ def __getattr__(self, attr: str) -> None:


class PyPageNode(PageNode):
temporal_link: Optional[Callable[[str], str]] = None

def __init__(self, parent: Optional[DirNode], dirPath: str, fileName: str) -> None:
super().__init__(parent, dirPath, fileName)
self._pyPageOutput: Optional[str] = None # to be generated (by pypage)
Expand All @@ -318,7 +321,39 @@ def output(self, htmlOutput: str) -> None:
self._pyPageOutput = htmlOutput


def buildWikiUrl(label: str, base: str, end: str) -> str:
# pylint: disable=unused-argument
if PyPageNode.temporal_link is None:
raise AltezaException("PyPageNode.temporal_link is not set.")
# pylint: disable=not-callable
return PyPageNode.temporal_link(label)


class Md(PyPageNode):
md = markdown.Markdown(
# See: https://python-markdown.github.io/extensions/
extensions=[
# Extra extensions:
"abbr",
"attr_list",
"def_list",
"fenced_code",
"footnotes",
"md_in_html",
"tables",
# Standard extensions:
"admonition",
"codehilite",
"meta",
"mdx_breakless_lists",
# "sane_lists",
"mdx_truly_sane_lists",
"smarty", # not sure
"toc",
WikiLinkExtension(html_class="", build_url=buildWikiUrl),
]
)

def __init__(self, parent: Optional[DirNode], dirPath: str, fileName: str) -> None:
super().__init__(parent, dirPath, fileName)

Expand All @@ -339,32 +374,10 @@ class Result(NamedTuple):

@staticmethod
def processMarkdown(text: str) -> Result:
md = markdown.Markdown(
# See: https://python-markdown.github.io/extensions/
extensions=[
# Extra extensions:
"abbr",
"attr_list",
"def_list",
"fenced_code",
"footnotes",
"md_in_html",
"tables",
# Standard extensions:
"admonition",
"codehilite",
"meta",
"mdx_breakless_lists",
# "sane_lists",
"mdx_truly_sane_lists",
"smarty", # not sure
"toc",
]
)
html: str = md.convert(text)
html: str = Md.md.convert(text)
yamlFrontMatter: str = ""

for name, lines in md.Meta.items(): # type: ignore # pylint: disable=no-member
for name, lines in Md.md.Meta.items(): # type: ignore # pylint: disable=no-member
yamlFrontMatter += f"{name} : {lines[0]} \n"
for line in lines[1:]:
yamlFrontMatter += " " * (len(name) + 3) + line + "\n"
Expand Down
2 changes: 2 additions & 0 deletions test_content/sectionK/sectionM/joyful-squirrel.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Here's a joyful squirrel.

Want to learn about a [magic turtle]({{link("magic-turtle")}})?

What about a [[curious-cat]]?

This is [what a "megabyte" is]({{link("just_a_test")}}).

This page was started on {{getIdeaDate("%B %-d, %Y")}}.
Expand Down

0 comments on commit 6011ca4

Please sign in to comment.