Contentful Rich Text Renderer
===================================
`Contentful <https://www.contentful.com>`_ 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 <./LICENSE>`_ for further details.
Contributing
------------
Feel free to improve this tool by submitting a Pull Request.
Raw data
{
"_id": null,
"home_page": "https://github.com/contentful/rich-text-renderer.py",
"name": "rich-text-renderer",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "contentful delivery cda cms content",
"author": "Contentful GmbH (David Litvak Bruno), Bhushan Lodha (rubydog)",
"author_email": "bhushan.lodha@external.contentful.com",
"download_url": "https://files.pythonhosted.org/packages/aa/44/dab2f9eba87bd2cecfa6e05b9e35658309655178208cc533684d0f518b85/rich_text_renderer-0.2.8.tar.gz",
"platform": null,
"description": "Contentful Rich Text Renderer\n===================================\n\n`Contentful <https://www.contentful.com>`_ 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.\n\nThis 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>`.\nBy default this library will serialize ``RichText`` fields into it's corresponding HTML representation. All behaviour can be overridden to serialize to different formats.\n\nInstallation\n------------\n\nInstall Contentful Rich Text Renderer from the Python Package Index::\n\n pip install rich_text_renderer\n\nUsage\n-----\n\nCreate a renderer::\n\n from rich_text_renderer import RichTextRenderer\n\n renderer = RichTextRenderer()\n\nRender your document::\n\n renderer.render(document)\n\nUsing different renderers\n-------------------------\n\nThere are many cases in which HTML serialization is not what you want.\nTherefore, all renderers are overridable when creating a `rich_text_renderer.RichTextRenderer <rich_text_renderer.RichTextRenderer>`.\n\nAlso, if you're planning to embed entries within your rich text, overriding the ``'embedded-entry-block'`` option is a must,\nas by default it only does ``<div>str(entry)</div>``.\n\nYou can override the configuration like follows::\n\n renderer = RichTextRenderer({\n 'embedded-entry-block': MyEntryBlockRenderer\n })\n\nWhere ``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.\n\nAn example entry renderer, assuming our entry has 2 fields called ``name`` and ``description`` could be::\n\n from rich_text_renderer.base_node_renderer import BaseNodeRenderer\n\n # BaseNodeRenderer implements the `__init__` method required.\n class MyEntryBlockRenderer(BaseNodeRenderer):\n def render(self, node):\n entry = node['data']['target']\n\n return \"<div class='my-entry'><h3>{0}</h3><p><small>{1}</p></small></div>\".format(\n entry.name,\n entry.description\n )\n\nDealing with unknown node types\n-------------------------------\n\nBy 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.\nIf 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.\n\nAn example would be like follows::\n\n class SilentNullRenderer(BaseNodeRenderer):\n def render(node):\n return \"\"\n\n renderer = RichTextRenderer({\n None: SilentNullRenderer\n })\n\nLicense\n-------\n\nCopyright (c) 2018 Contentful GmbH. See `LICENSE <./LICENSE>`_ for further details.\n\nContributing\n------------\n\nFeel free to improve this tool by submitting a Pull Request.\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Contentful Rich Text Renderer",
"version": "0.2.8",
"project_urls": {
"Homepage": "https://github.com/contentful/rich-text-renderer.py"
},
"split_keywords": [
"contentful",
"delivery",
"cda",
"cms",
"content"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "fe5f9500fa72be6e5e7fec39c7ce8493117e22f4b74a746d5f83ddefc22213b4",
"md5": "4bfc654e9a7805bff2f7a549873873af",
"sha256": "74e28bde639f737e2bc50f3dfb147bdbad4b2d7ab3f8f76a9db2d444edb8d13b"
},
"downloads": -1,
"filename": "rich_text_renderer-0.2.8-py3-none-any.whl",
"has_sig": false,
"md5_digest": "4bfc654e9a7805bff2f7a549873873af",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 7868,
"upload_time": "2023-10-26T17:59:17",
"upload_time_iso_8601": "2023-10-26T17:59:17.346913Z",
"url": "https://files.pythonhosted.org/packages/fe/5f/9500fa72be6e5e7fec39c7ce8493117e22f4b74a746d5f83ddefc22213b4/rich_text_renderer-0.2.8-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "aa44dab2f9eba87bd2cecfa6e05b9e35658309655178208cc533684d0f518b85",
"md5": "3c7ad894ac54f5988e0039df8c69d8fa",
"sha256": "e4680109372b55611250ebe0745f59cbf3f8932afa6f5cb5d1f3cc282a4bf95c"
},
"downloads": -1,
"filename": "rich_text_renderer-0.2.8.tar.gz",
"has_sig": false,
"md5_digest": "3c7ad894ac54f5988e0039df8c69d8fa",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 6530,
"upload_time": "2023-10-26T17:59:19",
"upload_time_iso_8601": "2023-10-26T17:59:19.157561Z",
"url": "https://files.pythonhosted.org/packages/aa/44/dab2f9eba87bd2cecfa6e05b9e35658309655178208cc533684d0f518b85/rich_text_renderer-0.2.8.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-10-26 17:59:19",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "contentful",
"github_project": "rich-text-renderer.py",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"circle": true,
"requirements": [],
"tox": true,
"lcname": "rich-text-renderer"
}