50 lines
1.4 KiB
Python
50 lines
1.4 KiB
Python
import html
|
|
import os
|
|
|
|
|
|
def declare_variables(variables, macro):
|
|
@macro
|
|
def includecode(fn: str, flavor: str = ""):
|
|
"""
|
|
Load code from a file and save as a preformatted code block.
|
|
If a flavor is specified, it's passed in as a hint for syntax highlighters.
|
|
|
|
Example usage in markdown:
|
|
|
|
{{includecode("code/myfile.py", "python")}}
|
|
|
|
"""
|
|
docs_dir = variables.get("docs_dir", "docs")
|
|
fn = os.path.abspath(os.path.join(docs_dir, fn))
|
|
if not os.path.exists(fn):
|
|
return f"""<b>File not found: {fn}</b>"""
|
|
with open(fn, "r") as f:
|
|
return (
|
|
f"""```{flavor}\n{f.read()}\n```"""
|
|
)
|
|
|
|
@macro
|
|
def includemd(fn: str):
|
|
"""
|
|
Load markdown from files external to the mkdocs root path.
|
|
Example usage in markdown:
|
|
|
|
{{includemd("../../README.md")}}
|
|
|
|
"""
|
|
docs_dir = variables.get("docs_dir", "docs")
|
|
fn = os.path.abspath(os.path.join(docs_dir, fn))
|
|
if not os.path.exists(fn):
|
|
return f"""<b>File not found: {fn}</b>"""
|
|
with open(fn, "r") as f:
|
|
return f.read()
|
|
|
|
@macro
|
|
def downloadlink(contain: str, url: str):
|
|
"""
|
|
Create a download link example :
|
|
|
|
{{downloadlink("Download readme", "../../README.md")}}
|
|
|
|
"""
|
|
return f"""<a href={url} download>{contain}</a>""" |