Skip to main content

Contentful Rich Text Renderer

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rich_text_renderer-0.2.7.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

rich_text_renderer-0.2.7-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file rich_text_renderer-0.2.7.tar.gz.

File metadata

  • Download URL: rich_text_renderer-0.2.7.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.6

File hashes

Hashes for rich_text_renderer-0.2.7.tar.gz
Algorithm Hash digest
SHA256 d34f1bfc1a2903e9b087d7550a01089b06867f0be21fa494fdee73b656c80bc7
MD5 d7518f82e8e64b6d377612e2c182f6db
BLAKE2b-256 7bec91a1997669fb1bfe4656f0dd2112ddadd17a5f3963871147e0d28f3c8455

See more details on using hashes here.

File details

Details for the file rich_text_renderer-0.2.7-py3-none-any.whl.

File metadata

File hashes

Hashes for rich_text_renderer-0.2.7-py3-none-any.whl
Algorithm Hash digest
SHA256 e6fab8d1243dddd29bb65717ade63ca57f10561a3944fc49dac770c4d95ab71c
MD5 a9fbddc2072606e4dffd8e5dca313261
BLAKE2b-256 922a7cd1dd0db8972cde42c54d4ff6db6d2ea6ff058daecef0b5b3bf50312ade

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page