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
muslpy 3.10–3.920 runs
installs and imports cleanly · install 0.0s · import 0.010s · 17.8MB
glibcpy 3.10–3.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)
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`.
fixDefine 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.
fixProvide 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.
fixEnsure 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.