Explicit-stack, pure CommonMark parser & renderer.
Project description
puku-markdown
A CommonMark‑compliant Markdown parser and renderer for block elements, written in pure Python with an explicit stack – no recursion, no hidden state, and a strong focus on readability and maintainability. Requires Python 3.12 or higher.
Motivation
While markdown-it-py is a solid parser, its codebase has several pain points that this project addresses:
- Recursive descent → explicit stack –
puku-markdownuses an explicit stack, eliminating recursion limits and improving portability (e.g., to C). For example,markdown-it-pyrelies on recursion and a mutablestate.tightflag that becomes inconsistent across nested lists;puku-markdownstores per‑level state on an explicit stack instead. - No obscure quirks – Removes confusing state flags like
sCount = -1for continuation. - Readability first – Clean variable names, thorough comments, and a well‑structured codebase.
- Designed for long‑term maintenance – Every internal detail is documented and intentional.
This library was born from a deep refactoring attempt of
markdown-it-py. Instead of fighting the original code, we built a fresh, cleaner implementation.
Features
- ✅ CommonMark block elements – headings, code blocks, lists, blockquotes, HTML blocks, thematic breaks, and more.
- ✅ Explicit stack – no recursion, easy to reason about and port.
- ✅ Pure Python – no external runtime dependencies.
- ✅ Renderer – currently supports rendering back to plain Markdown (text).
- ✅ Tested against
markdown-it-py– we reuse their extensive test suite for compliance.
Current status: Block elements are fully supported for both parsing and rendering. Inline elements (emphasis, links, images, etc.) are planned.
Installation
pip install puku-markdown
Or with uv (recommended):
uv add puku-markdown
Quick Usage
from puku_markdown import parse, render_to_text
# Parse Markdown to an AST.
document = parse("""
# Heading
- List item 1
- List item 2
> A blockquote.
""")
# Render the AST back to Markdown text.
rendered = render_to_text(document)
print(rendered)
Development
Clone the repository and set up the environment:
git clone https://github.com/curlinkw/puku-markdown.git
cd puku-markdown
uv sync --group test
Run tests:
uv run pytest
Contributing
Issues and pull requests are welcome. Please ensure that changes pass the CommonMark test suite and maintain 100% compatibility for the supported block elements.
License
MIT License. See LICENSE for details.
Project details
Release history Release notifications | RSS feed
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 puku_markdown-0.2.1.tar.gz.
File metadata
- Download URL: puku_markdown-0.2.1.tar.gz
- Upload date:
- Size: 173.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c6adff454f77467d6b050d582d7afb5cfbf4b76f0acafed4afa0ad7b508752b3
|
|
| MD5 |
3c116c970df0ed9aa2da0757ae0093f8
|
|
| BLAKE2b-256 |
28cb65b168211a2f5cbc22ed423d7f35162fcfc31c17e863f5384692c457eedf
|
File details
Details for the file puku_markdown-0.2.1-py3-none-any.whl.
File metadata
- Download URL: puku_markdown-0.2.1-py3-none-any.whl
- Upload date:
- Size: 108.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f76e49b13165606f9693652315c0a7227bd246248eb69114bd2a6ff0d4dde0aa
|
|
| MD5 |
4f54a4661dc01ce9e872fbdb446c4975
|
|
| BLAKE2b-256 |
d06a35a60f7ffa20624ab3b0a6c02974b61b9c18c39fff17444754a4f74673ab
|