Dead simple custom python functions with mkdocs
Project description
Dead simple custom python functions with mkdocs
Usage 📖
Install the plugin in your project ...
pip install TODO
... and add it to your mkdocs.yaml configuration ...
# mkdocs.yaml
plugins:
- fun
... create a docs/fun.py file ...
# docs/fun.py
def hello() -> str:
return "world"
... and start using your functions in your docs ...
<!-- docs/docs.md -->
This #!hello() comes from my function!
... becomes ...
This world comes from my function!
Configuration 🎛
You can customize the plugin behaviour with configuration:
# mkdocs.yaml
plugins:
- fun:
pattern: "#!(?P<func>[^\(]+)\((?P<params>[^\)]*)\)" # Regex to match functions
module: fun.py # Python file that defines your functions
Examples 💡
References and links
# docs/fun.py
def ref(key: str) -> str:
"""
Bookkeeping and standardized format of references in the docs
"""
r = {
"mcguffin": ("McGuffin", "/refs/mcguffin.md"),
}.get(key)
assert r, f"No ref for '{key}' found"
return f"[`{r[0]}`]({r[1]})"
def link(key: str) -> str:
"""
Bookkeeping and standardized format of external links in the docs
"""
l = {
"github": (":fontawesome-brands-github: Github", "https://www.github.com"),
}.get(key)
assert l, f"No link for '{key}' found"
return f'[{l[0]}]({l[1]}){{:target="_blank"}}'
<!-- docs/docs.md -->
Look at our internal #!ref(mcguffin) docs for more info. Also open up #!link(github).
... becomes ...
Look at our internal [`McGuffin`](/refs/mcguffin.md) docs for more info. Also open up [:fontawesome-brands-github: Github](https://www.github.com){:target="_blank"}.
Shell
# docs/fun.py
@functools.cache
def shell(cmd: str) -> str:
"""
Run arbitrary commands and return stdout
"""
return (
subprocess.run(
cmd,
shell=True,
check=True,
capture_output=True,
)
.stdout.decode()
.strip()
)
<!-- docs/docs.md -->
#!shell("echo hello | cowsay")
... becomes ...
_______
< hello >
-------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
File contents
# docs/fun.py
def func_def(file: str, name: str) -> str:
"""
Reads 'file' and returns the definition of function 'name'
"""
with (pathlib.Path(__file__).parent / file).open() as f:
content = f.read()
# Find function definition including decorators
pattern = rf"(@.*\n)*def {name}\s*\([^)]*\)[^:]*:"
match = re.search(pattern, content, re.MULTILINE)
assert match, f"Function '{name}' not found in '{file}'"
# Find where function starts and ends
start_pos = match.start()
lines = content.splitlines()
start_line = content[:start_pos].count("\n")
# Find function body by tracking indentation
function_lines = [lines[start_line]]
indent = (
re.match(r"(\s*)", lines[start_line + 1]).group(1)
if start_line + 1 < len(lines)
else ""
)
# Collect and return
for i in range(start_line + 1, len(lines)):
line = lines[i]
if line.strip() and not line.startswith(indent):
break
function_lines.append(line)
return "\n".join(function_lines).strip()
<!-- docs/docs.md -->
#!func_def(fun.py, hello)
... becomes ...
def hello() -> str:
return "world"
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mkdocs_fun_plugin-0.1.0.tar.gz.
File metadata
- Download URL: mkdocs_fun_plugin-0.1.0.tar.gz
- Upload date:
- Size: 43.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3723e65fc13da782e6b9481d53d2622229b4daab5d30acf9e1424770e3bb6d4c
|
|
| MD5 |
63d5a603cf61ed2b632c907341464f87
|
|
| BLAKE2b-256 |
f806e85d5562f7d8921e11c04cfb5a94b47c5295f4676c3b2fd4f71c9be8e956
|
File details
Details for the file mkdocs_fun_plugin-0.1.0-py3-none-any.whl.
File metadata
- Download URL: mkdocs_fun_plugin-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a3fbb53bb50239806d2a15e5e82a0ac5c05c86f098c944bbe93ad7d5283ba029
|
|
| MD5 |
3e1c906613d74dfde0df558d21731dc3
|
|
| BLAKE2b-256 |
86dee20503b9901eb79a83100d62a3f121e59b503e293a3dd67755774e9c7ca4
|