Skip to main content

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-tests suite)
  • 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) -> dict
    • html can be a string/bytes, a justhtml.JustHTML instance, or a JustHTML root node.
    • base_url controls resolution of relative URLs (preferred). url is a deprecated alias.
  • mf2dom.parse_async(...) is parse(...) via asyncio.to_thread(...).
  • mf2dom.render(doc) -> str renders a deterministic HTML representation of an mf2 document.

Why mf2dom vs mf2py?

Both libraries parse microformats, but they optimize for different use cases:

  • Choose mf2dom if you need deterministic rendering, stable round-trips, and a smaller/no-network runtime surface (useful for normalization, caching, and “canonical mf2 HTML” fixtures).
  • Choose mf2py if you need URL fetching, microformats1 compatibility, metaformats support, or wider Python version support.

Testing & correctness

  • Official parsing fixtures: tests/test_official_microformats_suite.py runs the upstream microformats-tests JSON fixtures.
  • Coverage gate: pyproject.toml enforces 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


Download files

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

Source Distribution

mf2dom-0.1.1.tar.gz (85.6 kB view details)

Uploaded Source

Built Distribution

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

mf2dom-0.1.1-py3-none-any.whl (32.0 kB view details)

Uploaded Python 3

File details

Details for the file mf2dom-0.1.1.tar.gz.

File metadata

  • Download URL: mf2dom-0.1.1.tar.gz
  • Upload date:
  • Size: 85.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for mf2dom-0.1.1.tar.gz
Algorithm Hash digest
SHA256 8e59ae5a118906b1c4017c978e113117eafc34191f49b60229e64443ade03351
MD5 9ebe1c33a83c8be904f740f7aff90875
BLAKE2b-256 3aa132ef5f748c2a2b6e4b0fe76777c3ec0c2df2cdad7070b4c42010a3737deb

See more details on using hashes here.

File details

Details for the file mf2dom-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: mf2dom-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 32.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for mf2dom-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e4ff91264fc9c57a2c44d349767e5c37cea9f878072ace1390a56d9d36385df2
MD5 e259e36fb9109c7bddfde78166ed9e5e
BLAKE2b-256 3a13a8837d5869542c68d1f44799c7e0dd53e3c8192df7d4f34b48df9eb1272c

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