Skip to main content

Explicit-stack, pure CommonMark parser & renderer.

Project description

puku-markdown

PyPI version Python Versions Test Status License

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 stackpuku-markdown uses an explicit stack, eliminating recursion limits and improving portability (e.g., to C). For example, markdown-it-py relies on recursion and a mutable state.tight flag that becomes inconsistent across nested lists; puku-markdown stores per‑level state on an explicit stack instead.
  • No obscure quirks – Removes confusing state flags like sCount = -1 for 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

puku_markdown-0.2.1.tar.gz (173.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

puku_markdown-0.2.1-py3-none-any.whl (108.1 kB view details)

Uploaded Python 3

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

Hashes for puku_markdown-0.2.1.tar.gz
Algorithm Hash digest
SHA256 c6adff454f77467d6b050d582d7afb5cfbf4b76f0acafed4afa0ad7b508752b3
MD5 3c116c970df0ed9aa2da0757ae0093f8
BLAKE2b-256 28cb65b168211a2f5cbc22ed423d7f35162fcfc31c17e863f5384692c457eedf

See more details on using hashes here.

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

Hashes for puku_markdown-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f76e49b13165606f9693652315c0a7227bd246248eb69114bd2a6ff0d4dde0aa
MD5 4f54a4661dc01ce9e872fbdb446c4975
BLAKE2b-256 d06a35a60f7ffa20624ab3b0a6c02974b61b9c18c39fff17444754a4f74673ab

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page