Registry / communication / rich-text-renderer

rich-text-renderer

JSON →
library0.2.8pypypi✓ verified 87d ago

The Contentful Rich Text Renderer is a Python library designed to serialize the RichText field type from Contentful into its corresponding HTML representation by default. It provides flexible rendering capabilities, allowing for full customization of node and mark rendering. It is commonly used alongside the Contentful Delivery SDK. The current version is 0.2.8, and releases are typically tied to updates in the Contentful ecosystem as needed.

pip install rich-text-renderer
INSTALL
IMPORT
SIG · RICH-TEXT-RENDERER
R
rich-text-renderer
communicationpythonv0.2.8
Install
1.6s avg
Import
9ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.2.8 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.010s · 17.8MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 1.6s · import 0.007s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

RichTextRenderer
from rich_text_renderer import RichTextRenderer
BLOCKS
from rich_text_renderer.base_renderer import BLOCKS
Required when creating custom renderers for block-level nodes.
MARKS
from rich_text_renderer.base_renderer import MARKS
Required when creating custom renderers for text marks.
BaseNodeRenderer
from rich_text_renderer.base_node_renderer import BaseNodeRenderer
Used as a base class for custom node renderers.

Initializes `RichTextRenderer` and renders a sample Rich Text JSON document to HTML. It also demonstrates how to provide a custom renderer for specific block types like 'embedded-entry-block' to control how linked entries are displayed.

from rich_text_renderer import RichTextRenderer # Example Rich Text document (typically obtained from Contentful API) document = { 'nodeType': 'document', 'data': {}, 'content': [ { 'nodeType': 'paragraph', 'data': {}, 'content': [ {'nodeType': 'text', 'value': 'Hello, ', 'marks': []}, {'nodeType': 'text', 'value': 'Contentful Rich Text!', 'marks': [{'type': 'bold'}]} ] } ] } renderer = RichTextRenderer() html_output = renderer.render(document) print(html_output) # Example with a custom renderer for a specific block type (e.g., 'embedded-entry-block') from rich_text_renderer.base_node_renderer import BaseNodeRenderer from rich_text_renderer.base_renderer import BLOCKS class MyEntryBlockRenderer(BaseNodeRenderer): def render(self, node): entry_id = node['data']['target']['sys']['id'] # In a real application, you would fetch entry data using the Contentful SDK # For this example, we'll just return a placeholder HTML string. return f'<div class="embedded-entry" data-entry-id="{entry_id}">Custom Embedded Entry: {entry_id}</div>' custom_renderer = RichTextRenderer({ BLOCKS.EMBEDDED_ENTRY: MyEntryBlockRenderer() }) document_with_entry = { 'nodeType': 'document', 'data': {}, 'content': [ { 'nodeType': 'paragraph', 'data': {}, 'content': [ {'nodeType': 'text', 'value': 'Here is an ', 'marks': []}, {'nodeType': 'text', 'value': 'embedded entry:', 'marks': [{'type': 'italic'}]} ] }, { 'nodeType': 'embedded-entry-block', 'data': { 'target': { 'sys': {'id': 'my-custom-entry', 'type': 'Link', 'linkType': 'Entry'} } }, 'content': [] } ] } html_output_custom = custom_renderer.render(document_with_entry) print('\n--- With Custom Renderer ---') print(html_output_custom)
Debug
Known issues
gotchaBy default, the library only renders a generic `<div>str(entry)</div>` for embedded entries (`embedded-entry-block`). This is usually not sufficient for meaningful content.
fix
Always provide a custom renderer for `BLOCKS.EMBEDDED_ENTRY` when working with embedded entries to display them correctly. This requires defining a custom class inheriting from `BaseNodeRenderer` and passing an instance to the `RichTextRenderer` constructor.
affects: All versions
gotchaThe renderer will raise an exception if it encounters an unknown node type in the Rich Text document (e.g., if your Contentful model defines a custom block that the renderer doesn't have a mapping for).
fix
To prevent exceptions from unknown node types, you can provide a `NullRenderer` (or a custom fallback renderer) for the `None` key in the renderer mapping, which will return an empty string or a default representation. E.g., `RichTextRenderer({None: NullRenderer()})`.
affects: All versions
gotchaThe Contentful Rich Text field returns content as a JSON object, not raw HTML. Directly attempting to process this JSON as a simple string or using a generic HTML parser will fail.
fix
Always use the `rich-text-renderer` library to properly interpret and render the JSON structure of Contentful's Rich Text field into a desired output format (e.g., HTML).
affects: All versions
Errors
Common errors & fixes
KeyError: 'embedded-entry-block'
Attempting to render a Rich Text document that contains an embedded entry block without providing a custom renderer for `BLOCKS.EMBEDDED_ENTRY`.
fix
Define and register a custom renderer for `BLOCKS.EMBEDDED_ENTRY` within the `RichTextRenderer` constructor to handle how embedded entries should be displayed.
rich_text_renderer.base_renderer.InvalidNodeException: Unknown node type: 'unknown-custom-block'
The Rich Text document contains a node type that the `RichTextRenderer` does not have a default or custom mapping for.
fix
Provide a custom renderer for the specific unknown node type, or register a fallback renderer for `None` to gracefully handle all unmapped node types.
TypeError: render() missing 1 required positional argument: 'document'
Attempting to call the `render` method of `RichTextRenderer` without passing the Rich Text JSON `document` as an argument.
fix
Ensure the Rich Text JSON object is passed as the `document` argument to the `renderer.render()` method.
Upgrade
Version history
0.2.8latest on PyPI · released Oct 26, 2023
Audit
Dependencies
contentful.pyoptionalRecommended for fetching Rich Text field data from Contentful.
Agent activity
15 hits · last 30 days
node
12
OpenAI (training)
2
Resources
rich-text-renderer — pip install rich-text-renderer · libregistry