Skip to content

Commit

Permalink
Merge pull request #2 from contentstack/supercharge-rte
Browse files Browse the repository at this point in the history
Supercharge rte
  • Loading branch information
ishaileshmishra authored Jul 22, 2021
2 parents ca9b8fe + b7b9b38 commit 28807be
Show file tree
Hide file tree
Showing 67 changed files with 4,548 additions and 3,234 deletions.
1 change: 1 addition & 0 deletions .talismanrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
threshold: medium
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright 2020 Contentstack
Copyright 2021 Contentstack

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
53 changes: 36 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ Contentstack Utils SDK lets you interact with the Content Delivery APIs and retr
To get an embedded item of a single entry, you need to provide the stack API key, environment name, content type’s UID, and entry’s UID. Then, use the `entry.fetch` function as shown below:

```python
import contentstack
import contentstack

stack = contentstack.Stack('api_key','delivery_token','environment')
content_type = stack.content_type("content_type_uid")
entry = content_type.entry("entry_uid")
result = entry.fetch()
if result is not None:
entry = result['entries']
Utils.render(entry, ['rich_text_editor', 'some_other_text'], Option())
stack = contentstack.Stack('api_key','delivery_token','environment')
content_type = stack.content_type("content_type_uid")
entry = content_type.entry("entry_uid")
result = entry.fetch()
if result is not None:
entry = result['entries']
Utils.render(entry, ['rich_text_editor', 'some_other_text'], Option())

```

Expand All @@ -81,13 +81,32 @@ To get an embedded item of a single entry, you need to provide the stack API key
To get embedded items from multiple entries, you need to provide the stack API key, delivery token, environment name, and content type’s UID.

```python
import contentstack

stack = contentstack.Stack('api_key','delivery_token','environment')
query = stack.content_type("content_type_uid").query()
result = query.find()
if result is not None and 'entries' in result:
entry = result['entries']
for item in range:
Utils.render(item, ['rich_text_editor', 'some_other_text'], Option())
import contentstack

stack = contentstack.Stack('api_key','delivery_token','environment')
query = stack.content_type("content_type_uid").query()
result = query.find()
if result is not None and 'entries' in result:
entry = result['entries']
for item in range:
option = Option()
Utils.render(item, ['rich_text_editor', 'some_other_text'], option)
```


## Supercharged

To get supercharged items from multiple entries, you need to provide the stack API key, delivery token, environment name, and content type’s UID.

```python
import contentstack

stack = contentstack.Stack('api_key','delivery_token','environment')
query = stack.content_type("content_type_uid").query()
result = query.find()
if result is not None and 'entries' in result:
entry = result['entries']
for item in range:
option = Option()
Utils.json_to_html(item, ['paragraph_text'], option)
```
12 changes: 12 additions & 0 deletions changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@
**CHANGELOG**
================

NEW FEATURE: Supercharged RTE

- jsonToHtml function support added

*v0.2.0*
============

**Date: 02-Sept-2021**

- Initial release of contentstack utility package


ENHANCEMENT, NEW FEATURE, BUG RESOLVE

*v0.1.0*
Expand Down
6 changes: 0 additions & 6 deletions contentstack_utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@
# coverage report -m
# coverage html -d coveragereport
"""
The __init__.py files are required to make Python treat the directories as containing
packages; this is done to prevent directories with a common name, such as string,
from unintentionally hiding valid modules that occur later on the module search path
Used: Safety checks your installed dependencies for known security vulnerabilities
file __init__.py contains package information like
__author__, __status__, __version__, __endpoint__ and __email__
`Your code has been rated at 10.00/10`
Expand Down
7 changes: 1 addition & 6 deletions contentstack_utils/embedded/item_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@


class ItemType(enum.Enum):
"""
Contains Two option for ItemsType
ENTRY
ASSET
"""
"""Contains Two option for ItemsType => ENTRY and ASSET """
ENTRY = 'entry'
ASSET = 'asset'
7 changes: 1 addition & 6 deletions contentstack_utils/embedded/styletype.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@ class StyleType(enum.Enum):

"""
This StyleType contains four options like below.
BLOCK
INLINE
LINK
DISPLAY
DOWNLOAD
BLOCK ,INLINE ,LINK,DISPLAY,DOWNLOAD
"""

BLOCK = 'block'
Expand Down
1 change: 0 additions & 1 deletion contentstack_utils/helper/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,3 @@ def convert_style(style) -> StyleType:
return StyleType.DISPLAY
elif style == 'download':
return StyleType.DOWNLOAD

35 changes: 0 additions & 35 deletions contentstack_utils/helper/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,63 +38,28 @@ def __init__(self, text: str, item_type: str, item_uid: str,

@property
def get_text(self):
"""text for embedded objects
Returns:
str : text for embedded objects
"""
return self.text

@property
def get_item_type(self):
"""item_type for embedded objects
Returns:
str : item_type for embedded objects
"""
return self.item_type

@property
def get_item_uid(self):
"""item_uid for embedded objects
Returns:
str : item_uid for embedded objects
"""
return self.item_uid

@property
def get_content_type_uid(self):
"""content_type_uid for embedded objects
Returns:
str : content_type_uid for embedded objects
"""
return self.content_type_uid

@property
def get_style_type(self) -> StyleType:
"""style_type for embedded objects
Returns:
StyleType : style_type for embedded objects
"""
return self.style_type

@property
def get_outer_html(self):
"""outer_html for embedded objects
Returns:
str : outer_html for embedded objects
"""
return self.outer_html

@property
def get_attributes(self):
"""attributes for embedded objects
Returns:
str : attributes for embedded objects
"""
return self.attributes
28 changes: 28 additions & 0 deletions contentstack_utils/helper/node_to_html.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from contentstack_utils.render.options import Options


class NodeToHtml:

@staticmethod
def text_node_to_html(node, option: Options):
"""
accepts node type,
on the basis of the node type, generates string
:rtype: str
"""
node_text = node['text']
if 'superscript' in node:
node_text = option.render_mark('superscript', node_text)
if 'subscript' in node:
node_text = option.render_mark('subscript', node_text)
if 'inlineCode' in node:
node_text = option.render_mark('inlineCode', node_text)
if 'strikethrough' in node:
node_text = option.render_mark('strikethrough', node_text)
if 'underline' in node:
node_text = option.render_mark('underline', node_text)
if 'italic' in node:
node_text = option.render_mark('italic', node_text)
if 'bold' in node:
node_text = option.render_mark('bold', node_text)
return node_text
121 changes: 96 additions & 25 deletions contentstack_utils/render/options.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,117 @@
# pylint: disable=missing-function-docstring
# pylint: disable=missing-docstring
# pylint: disable=too-few-public-methods
from contentstack_utils.helper.metadata import Metadata


def _title_or_uid(embedded_obj: dict) -> str:
def _title_or_uid(_obj: dict) -> str:
_title = ""
if embedded_obj is not None:
if 'title' in embedded_obj and len(embedded_obj['title']) != 0:
_title = embedded_obj['title']
elif 'uid' in embedded_obj:
_title = embedded_obj['uid']
if _obj is not None:
if 'title' in _obj and len(_obj['title']) != 0:
_title = _obj['title']
elif 'uid' in _obj:
_title = _obj['uid']
return _title


def _asset_title_or_uid(embedded_obj: dict) -> str:
def _asset_title_or_uid(_obj: dict) -> str:
_title = ""
if embedded_obj is not None:
if 'title' in embedded_obj and len(embedded_obj['title']) != 0:
_title = embedded_obj['title']
elif 'filename' in embedded_obj:
_title = embedded_obj['filename']
elif 'uid' in embedded_obj:
_title = embedded_obj['uid']
if _obj is not None:
if 'title' in _obj and len(_obj['title']) != 0:
_title = _obj['title']
elif 'filename' in _obj:
_title = _obj['filename']
elif 'uid' in _obj:
_title = _obj['uid']
return _title


class Options:

@staticmethod
def render_options(embedded_obj: dict, metadata: Metadata):
def render_options(_obj: dict, metadata: Metadata):
if metadata.style_type.value == 'block':
return '<div><p>' + _title_or_uid(embedded_obj) \
+ '</p><div><p>Content type: <span>' + embedded_obj['_content_type_uid'] \
return '<div><p>' + _title_or_uid(_obj) \
+ '</p><div><p>Content type: <span>' + _obj['_content_type_uid'] \
+ '</span></p></div>'
if metadata.style_type.value == 'inline':
return '<span>' + _title_or_uid(embedded_obj) + '</span>'
return '<span>' + _title_or_uid(_obj) + '</span>'
if metadata.style_type.value == 'link':
return '<a href=' + embedded_obj['url'] + '>' + _title_or_uid(embedded_obj) + '</a>'
return '<a href=' + _obj['url'] + '>' + _title_or_uid(_obj) + '</a>'
if metadata.style_type.value == 'display':
return '<img src=' + embedded_obj['url'] + ' alt=' \
+ _asset_title_or_uid(embedded_obj) + '/>'
return '<img src=' + _obj['url'] + ' alt=' \
+ _asset_title_or_uid(_obj) + '/>'
if metadata.style_type.value == 'download':
return '<a href=' + embedded_obj['url'] + '>' + _asset_title_or_uid(embedded_obj) + '</a>'
return '<a href=' + _obj['url'] + '>' + _asset_title_or_uid(_obj) + '</a>'

@staticmethod
def render_mark(mark_type: str, render_text: str):
if mark_type == 'superscript':
return "<sup>" + render_text + "</sup>"
if mark_type == 'subscript':
return "<sub>" + render_text + "</sub>"
if mark_type == 'inlineCode':
return "<span>" + render_text + "</span>"
if mark_type == 'strikethrough':
return "<strike>" + render_text + "</strike>"
if mark_type == 'underline':
return "<u>" + render_text + "</u>"
if mark_type == 'italic':
return "<em>" + render_text + "</em>"
if mark_type == 'bold':
return "<strong>" + render_text + "</strong>"
else:
return render_text
pass

@staticmethod
def render_node(node_type, node_obj: dict, callback):
inner_html = callback(node_obj['children'])
if node_type == 'p':
return "<p>" + inner_html + "</p>"
if node_type == 'a':
return "<a href=" + node_obj["attrs"]["href"] + ">" + inner_html + "</a>"
if node_type == 'img':
return "<img src=" + node_obj["attrs"]["src"] + " />" + inner_html + ""
if node_type == 'embed':
return "<iframe src=" + node_obj["attrs"]["src"] + ">" + inner_html + "</iframe>"
if node_type == 'h1':
return "<h1>" + inner_html + "</h1>"
if node_type == 'h2':
return "<h2>" + inner_html + "</h2>"
if node_type == 'h3':
return "<h3>" + inner_html + "</h3>"
if node_type == 'h4':
return "<h4>" + inner_html + "</h4>"
if node_type == 'h5':
return "<h5>" + inner_html + "</h5>"
if node_type == 'h6':
return "<h6>" + inner_html + "</h6>"
if node_type == 'ol':
return "<ol>" + inner_html + "</ol>"
if node_type == 'ul':
return "<ul>" + inner_html + "</ul>"
if node_type == 'li':
return "<li>" + inner_html + "</li>"
if node_type == 'hr':
return "<hr />"
if node_type == 'table':
return "<table>" + inner_html + "</table>"
if node_type == 'thead':
return "<thead>" + inner_html + "</thead>"
if node_type == 'tbody':
return "<tbody>" + inner_html + "</tbody>"
if node_type == 'tfoot':
return "<tfoot>" + inner_html + "</tfoot>"
if node_type == 'tr':
return "<tr>" + inner_html + "</tr>"

if node_type == 'th':
return "<th>" + inner_html + "</th>"
if node_type == 'td':
return "<td>" + inner_html + "</td>"
if node_type == 'blockquote':
return "<blockquote>" + inner_html + "</blockquote>"
if node_type == 'code':
return "<code>" + inner_html + "</code>"
if node_type in ['doc', 'reference']:
return inner_html
else:
return inner_html
Loading

0 comments on commit 28807be

Please sign in to comment.