readme-renderer
readme-renderer is a Python library designed to safely convert README file content (Markdown, reStructuredText, or plain text) into HTML. It is primarily used by PyPI's Warehouse to render `long_description` for Python packages, ensuring they display correctly and securely on the PyPI website. The library is currently at version 44.0 and is actively maintained by the Python Packaging Authority (PyPA).
Warnings
- breaking The library was originally named `readme` and was renamed to `readme_renderer` to resolve naming conflicts with system files. Users migrating from the old `readme` package must update their imports and dependencies.
- breaking The `cmarkgfm` dependency, essential for Markdown rendering, was moved into an optional extra. If you use `readme-renderer` to process Markdown and do not install it with `[md]` extra, rendering may fail or emit a `UserWarning`.
- breaking Support for Python 3.7 was dropped. Users on older Python versions will need to upgrade their Python interpreter or use an older `readme-renderer` version.
- gotcha reStructuredText content must adhere strictly to the Docutils specification without Sphinx extensions (e.g., directives and roles like `:py:func:`). Invalid markup or Sphinx-specific syntax will cause PyPI to display the raw source instead of a rendered version.
- gotcha The library performs HTML sanitization on the rendered output to prevent XSS attacks. This means only a whitelist of HTML tags and attributes are permitted. Custom or unrecognized HTML within your README may be stripped out.
Install
-
pip install readme-renderer -
pip install readme-renderer[md]
Imports
- render
from readme_renderer.markdown import render as render_markdown
- render
from readme_renderer.rst import render as render_rst
Quickstart
from readme_renderer.markdown import render as render_markdown
from readme_renderer.rst import render as render_rst
# Example Markdown content
markdown_content = """
# My Project
This is a *great* project with `python` code.
```python
def hello_world():
print("Hello, PyPI!")
```
"""
# Example reStructuredText content
rst_content = """
My Project
==========
This is a *great* project with ``python`` code.
.. code-block:: python
def hello_world():
print("Hello, PyPI!")
"""
# Render Markdown
html_from_markdown = render_markdown(markdown_content)
print("--- Rendered Markdown ---")
print(html_from_markdown[:200] + '...' if len(html_from_markdown) > 200 else html_from_markdown)
# Render reStructuredText
# Note: ensure 'docutils' is installed for rST rendering.
html_from_rst = render_rst(rst_content)
print("\n--- Rendered reStructuredText ---")
print(html_from_rst[:200] + '...' if len(html_from_rst) > 200 else html_from_rst)