Skip to main content

Contentful provides a content infrastructure for digital teams to power content in websites, apps, and devices. Unlike a CMS, Contentful was built to integrate with the modern software stack. It offers a central hub for structured content, powerful management and delivery APIs, and a customizable web app that enable developers and content creators to ship digital products faster.

This library provides rendering capabilities for the RichText field type. It is recommended to be used alongside the Contentful Delivery SDK <https://www.github.com/contentful/contentful.py>. By default this library will serialize RichText fields into it’s corresponding HTML representation. All behaviour can be overridden to serialize to different formats.

Installation

Install Contentful Rich Text Renderer from the Python Package Index:

pip install rich_text_renderer

Usage

Create a renderer:

from rich_text_renderer import RichTextRenderer

renderer = RichTextRenderer()

Render your document:

renderer.render(document)

Using different renderers

There are many cases in which HTML serialization is not what you want. Therefore, all renderers are overridable when creating a rich_text_renderer.RichTextRenderer <rich_text_renderer.RichTextRenderer>.

Also, if you’re planning to embed entries within your rich text, overriding the 'embedded-entry-block' option is a must, as by default it only does <div>str(entry)</div>.

You can override the configuration like follows:

renderer = RichTextRenderer({
    'embedded-entry-block': MyEntryBlockRenderer
})

Where MyEntryBlockRenderer requires to have a render(self, node) method and needs to return a string, also it requires to be initialized with a dict containing mappings for all renderers.

An example entry renderer, assuming our entry has 2 fields called name and description could be:

from rich_text_renderer.base_node_renderer import BaseNodeRenderer

# BaseNodeRenderer implements the `__init__` method required.
class MyEntryBlockRenderer(BaseNodeRenderer):
    def render(self, node):
        entry = node['data']['target']

        return "<div class='my-entry'><h3>{0}</h3><p><small>{1}</p></small></div>".format(
            entry.name,
            entry.description
        )

Dealing with unknown node types

By default, this library will treat all unknown node types as errors and will raise an exception letting the user know which node mapping is missing. If you wish to remove this behaviour then replace the None key of the mapping with a NullRenderer that returns an empty string, or something similar.

An example would be like follows:

class SilentNullRenderer(BaseNodeRenderer):
    def render(node):
        return ""

renderer = RichTextRenderer({
    None: SilentNullRenderer
})

License

Copyright (c) 2018 Contentful GmbH. See LICENSE for further details.

Contributing

Feel free to improve this tool by submitting a Pull Request.

Metadata

Release files for rich-text-renderer 0.2.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for rich-text-renderer 0.2.8
File Size Uploaded Provenance
rich_text_renderer-0.2.8.tar.gz 6.5 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for rich-text-renderer 0.2.8
File Interpreter ABI Platform
rich_text_renderer-0.2.8-py3-none-any.whl Python 3 none any Details

Total release size: 14.4 kB

Release files / rich_text_renderer-0.2.8.tar.gz

Download URL rich_text_renderer-0.2.8.tar.gz
Size 6.5 kB
Tags Source
SHA-256 checksum
How to use checksums
e4680109372b55611250ebe0745f59cbf3f8932afa6f5cb5d1f3cc282a4bf95c
BLAKE2b-256 checksum
How to use checksums
aa44dab2f9eba87bd2cecfa6e05b9e35658309655178208cc533684d0f518b85
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.5

Release files / rich_text_renderer-0.2.8-py3-none-any.whl

Download URL rich_text_renderer-0.2.8-py3-none-any.whl
Size 7.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
74e28bde639f737e2bc50f3dfb147bdbad4b2d7ab3f8f76a9db2d444edb8d13b
BLAKE2b-256 checksum
How to use checksums
fe5f9500fa72be6e5e7fec39c7ce8493117e22f4b74a746d5f83ddefc22213b4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.5

Security for rich-text-renderer 0.2.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page