Microformats2 (mf2) parser and renderer powered by JustHTML.
Project description
mf2dom
Microformats2 (mf2) parser and deterministic renderer powered by JustHTML.
mf2dom focuses on:
- Correct mf2 parsing (validated against the official
microformats-testssuite) - Deterministic HTML rendering and stable round-trips (
HTML -> mf2 -> HTML) - A small runtime surface area (no network I/O, no BeautifulSoup)
Installation
pip install mf2dom
Requires Python 3.11+.
Quickstart
Parse mf2 JSON from HTML:
import mf2dom
html = '<a class="h-card u-url p-name" href="/me">Alice</a>'
doc = mf2dom.parse(html, base_url="https://example.com/")
print(doc["items"])
The parsed document is a dict with items, rels, and rel-urls keys (mf2 JSON shape).
Render mf2 JSON back into canonical HTML:
html2 = mf2dom.render(doc)
Async parsing (offloads to a thread):
doc = await mf2dom.parse_async(html, base_url="https://example.com/")
API
mf2dom.parse(html, *, base_url=None, url=None) -> dicthtmlcan be a string/bytes, ajusthtml.JustHTMLinstance, or a JustHTML root node.base_urlcontrols resolution of relative URLs (preferred).urlis a deprecated alias.
mf2dom.parse_async(...)isparse(...)viaasyncio.to_thread(...).mf2dom.render(doc) -> strrenders a deterministic HTML representation of an mf2 document.
Why mf2dom vs mf2py?
Both libraries parse microformats, but they optimize for different use cases:
- Choose
mf2domif you need deterministic rendering, stable round-trips, and a smaller/no-network runtime surface (useful for normalization, caching, and “canonical mf2 HTML” fixtures). - Choose
mf2pyif you need URL fetching, microformats1 compatibility, metaformats support, or wider Python version support.
Testing & correctness
- Official parsing fixtures:
tests/test_official_microformats_suite.pyruns the upstreammicroformats-testsJSON fixtures. - Coverage gate:
pyproject.tomlenforces 100% branch coverage.
To run the official fixture suite locally, check out microformats-tests as a sibling directory:
git clone https://github.com/microformats/microformats-tests ../microformats-tests
Development (uv)
uv sync --group dev
uv run pytest
uv run coverage run -m pytest && uv run coverage report
uv run pre-commit install
License
AGPL 3
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 mf2dom-0.1.2.tar.gz.
File metadata
- Download URL: mf2dom-0.1.2.tar.gz
- Upload date:
- Size: 86.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8f17a678c6300c1344e91a1883f36a218a1af7a6d47663cb5818ad018c4d8941
|
|
| MD5 |
68cf44cb042599c39b896903d43af7e1
|
|
| BLAKE2b-256 |
dbbc6244106885da54dda55ffce82dfa2f21cf33d8435218d0a50764732c316c
|
File details
Details for the file mf2dom-0.1.2-py3-none-any.whl.
File metadata
- Download URL: mf2dom-0.1.2-py3-none-any.whl
- Upload date:
- Size: 32.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
adddbf725ac29d8f842b4a1fa86c4168d1d223f0ea708e3d1e2ea3530f6d0a1f
|
|
| MD5 |
411dccbd1b646914b355e524e1229305
|
|
| BLAKE2b-256 |
360f5b04ab0a0e24fc968cd39728b7ae3e54a2db59f27d6fe1d500cc063f1f82
|