readme-renderer

44.0 · active · verified Sun Mar 29

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

Install

Imports

Quickstart

This quickstart demonstrates how to render both Markdown and reStructuredText content into HTML using the `readme_renderer` library. It imports the specific rendering functions from their respective submodules and then calls them with sample content. The output is a string of HTML. Note that for Markdown rendering, the optional `cmarkgfm` dependency is highly recommended, installed via `pip install readme-renderer[md]`.

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)

view raw JSON →