Skip to content

Commit e07766b

Browse files
authored
Merge pull request #58 from contentstack/feat/DX-597
DX | 24-06-2024 | Release
2 parents 631338a + be4a724 commit e07766b

File tree

7 files changed

+604
-7
lines changed

7 files changed

+604
-7
lines changed

Diff for: CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# CHANGELOG
22

3+
## Version 3.15.1
4+
5+
### Date: 24-June-2024
6+
7+
- added support to convert json to html
8+
9+
---
10+
311
## Version 3.15.0
412

513
### Date: 20-May-2024

Diff for: contentstack/build.gradle

+12-7
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ android.buildFeatures.buildConfig true
1010
mavenPublishing {
1111
publishToMavenCentral(SonatypeHost.DEFAULT)
1212
signAllPublications()
13-
coordinates("com.contentstack.sdk", "android", "3.15.0")
13+
coordinates("com.contentstack.sdk", "android", "3.15.1")
1414

1515
pom {
1616
name = "contentstack-android"
@@ -76,6 +76,11 @@ android {
7676
// }
7777
}
7878
}
79+
// signing {
80+
// // Specify key and other signing details
81+
// useGpgCmd()
82+
// sign configurations.archives
83+
// }
7984
signingConfigs {
8085
debug {
8186
storeFile file("../key.keystore")
@@ -111,12 +116,12 @@ android {
111116
testCoverageEnabled true
112117
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
113118

114-
buildConfigField "String", "host", localProperties['host']
115-
buildConfigField "String", "APIKey", localProperties['APIKey']
116-
buildConfigField "String", "deliveryToken", localProperties['deliveryToken']
117-
buildConfigField "String", "environment", localProperties['environment']
118-
buildConfigField "String", "contentTypeUID", localProperties['contentType']
119-
buildConfigField "String", "assetUID", localProperties['assetUid']
119+
buildConfigField "String", "host", localProperties['host']
120+
buildConfigField "String", "APIKey", localProperties['APIKey']
121+
buildConfigField "String", "deliveryToken", localProperties['deliveryToken']
122+
buildConfigField "String", "environment", localProperties['environment']
123+
buildConfigField "String", "contentTypeUID", localProperties['contentType']
124+
buildConfigField "String", "assetUID", localProperties['assetUid']
120125
}
121126
release {
122127
minifyEnabled false
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
package com.contentstack.sdk;
2+
3+
import android.text.TextUtils;
4+
5+
import org.json.JSONException;
6+
import org.json.JSONObject;
7+
8+
import java.util.ArrayList;
9+
import java.util.Arrays;
10+
import java.util.HashMap;
11+
import java.util.Iterator;
12+
import java.util.Map;
13+
import java.util.Objects;
14+
15+
public class DefaultOption implements Option {
16+
@Override
17+
public String renderOptions(JSONObject embeddedObject, Metadata metadata) {
18+
switch (metadata.getStyleType()) {
19+
case BLOCK:
20+
return "<div><p>" + findTitleOrUid(embeddedObject) + "</p><div><p>Content type: <span>" + embeddedObject.optString("_content_type_uid") + "</span></p></div>";
21+
case INLINE:
22+
return "<span>" + findTitleOrUid(embeddedObject) + "</span>";
23+
case LINK:
24+
return "<a href=\"" + embeddedObject.optString("url") + "\">" + findTitleOrUid(embeddedObject) + "</a>";
25+
case DISPLAY:
26+
return "<img src=\"" + embeddedObject.optString("url") + "\" alt=\"" + findAssetTitle(embeddedObject) + "\" />";
27+
default:
28+
return "";
29+
}
30+
}
31+
32+
@Override
33+
public String renderMark(MarkType markType, String text) {
34+
switch (markType) {
35+
case SUPERSCRIPT:
36+
return "<sup>" + text + "</sup>";
37+
case SUBSCRIPT:
38+
return "<sub>" + text + "</sub>";
39+
case INLINECODE:
40+
return "<span>" + text + "</span>";
41+
case STRIKETHROUGH:
42+
return "<strike>" + text + "</strike>";
43+
case UNDERLINE:
44+
return "<u>" + text + "</u>";
45+
case ITALIC:
46+
return "<em>" + text + "</em>";
47+
case BOLD:
48+
return "<strong>" + text + "</strong>";
49+
case BREAK:
50+
return "<br />" + text.replace("\n", "");
51+
default:
52+
return text;
53+
}
54+
}
55+
56+
private String escapeInjectHtml(JSONObject nodeObj, String nodeType) {
57+
String injectedHtml = getNodeStr(nodeObj, nodeType);
58+
return TextUtils.htmlEncode(injectedHtml);
59+
}
60+
61+
@Override
62+
public String renderNode(String nodeType, JSONObject nodeObject, NodeCallback callback) {
63+
String strAttrs = strAttrs(nodeObject);
64+
String children = callback.renderChildren(nodeObject.optJSONArray("children"));
65+
switch (nodeType) {
66+
case "p":
67+
return "<p" + strAttrs + ">" + children + "</p>";
68+
case "a":
69+
return "<a" + strAttrs + " href=\"" + escapeInjectHtml(nodeObject, "href") + "\">" + children + "</a>";
70+
case "img":
71+
String assetLink = getNodeStr(nodeObject, "asset-link");
72+
if (!assetLink.isEmpty()) {
73+
JSONObject attrs = nodeObject.optJSONObject("attrs");
74+
if (attrs.has("link")) {
75+
return "<a href=\"" + escapeInjectHtml(nodeObject, "link") + "\" >" + "<img" + strAttrs + " src=\"" + escapeInjectHtml(nodeObject, "asset-link") + "\" />" + children + "</a>";
76+
}
77+
return "<img" + strAttrs + " src=\"" + escapeInjectHtml(nodeObject, "asset-link") + "\" />" + children;
78+
}
79+
return "<img" + strAttrs + " src=\"" + escapeInjectHtml(nodeObject, "src") + "\" />" + children;
80+
case "embed":
81+
return "<iframe" + strAttrs + " src=\"" + escapeInjectHtml(nodeObject, "src") + "\"" + children + "</iframe>";
82+
case "h1":
83+
return "<h1" + strAttrs + ">" + children + "</h1>";
84+
case "h2":
85+
return "<h2" + strAttrs + ">" + children + "</h2>";
86+
case "h3":
87+
return "<h3" + strAttrs + ">" + children + "</h3>";
88+
case "h4":
89+
return "<h4" + strAttrs + ">" + children + "</h4>";
90+
case "h5":
91+
return "<h5" + strAttrs + ">" + children + "</h5>";
92+
case "h6":
93+
return "<h6" + strAttrs + ">" + children + "</h6>";
94+
case "ol":
95+
return "<ol" + strAttrs + ">" + children + "</ol>";
96+
case "ul":
97+
return "<ul" + strAttrs + ">" + children + "</ul>";
98+
case "li":
99+
return "<li" + strAttrs + ">" + children + "</li>";
100+
case "hr":
101+
return "<hr" + strAttrs + " />";
102+
case "table":
103+
return "<table " + strAttrs + ">" + children + "</table>";
104+
case "thead":
105+
return "<thead " + strAttrs + ">" + children + "</thead>";
106+
case "tbody":
107+
return "<tbody" + strAttrs + ">" + children + "</tbody>";
108+
case "tfoot":
109+
return "<tfoot" + strAttrs + ">" + children + "</tfoot>";
110+
case "tr":
111+
return "<tr" + strAttrs + ">" + children + "</tr>";
112+
case "th":
113+
return "<th" + strAttrs + ">" + children + "</th>";
114+
case "td":
115+
return "<td" + strAttrs + ">" + children + "</td>";
116+
case "blockquote":
117+
return "<blockquote" + strAttrs + ">" + children + "</blockquote>";
118+
case "code":
119+
return "<code" + strAttrs + ">" + children + "</code>";
120+
case "reference":
121+
return "";
122+
case "fragment":
123+
return "<fragment" + strAttrs + ">" + children + "</fragment>";
124+
default:
125+
return children;
126+
}
127+
}
128+
129+
String strAttrs(JSONObject nodeObject) {
130+
StringBuilder result = new StringBuilder();
131+
if (nodeObject.has("attrs")) {
132+
JSONObject attrsObject = nodeObject.optJSONObject("attrs");
133+
if (attrsObject != null && attrsObject.length() > 0) {
134+
for (Iterator<String> it = attrsObject.keys(); it.hasNext(); ) {
135+
String key = it.next();
136+
Object objValue = attrsObject.opt(key);
137+
String value = objValue.toString();
138+
// If style is available, do styling calculations
139+
if (Objects.equals(key, "style")) {
140+
String resultStyle = stringifyStyles(attrsObject.optJSONObject("style"));
141+
result.append(" ").append(key).append("=\"").append(resultStyle).append("\"");
142+
} else {
143+
String[] ignoreKeys = {"href", "asset-link", "src", "url"};
144+
ArrayList<String> ignoreKeysList = new ArrayList<>(Arrays.asList(ignoreKeys));
145+
if (!ignoreKeysList.contains(key)) {
146+
result.append(" ").append(key).append("=\"").append(value).append("\"");
147+
}
148+
}
149+
}
150+
}
151+
}
152+
return result.toString();
153+
}
154+
155+
private String stringifyStyles(JSONObject style) {
156+
Map<String, String> styleMap = new HashMap<>();
157+
158+
// Convert JSONObject to a Map
159+
Iterator<String> keys = style.keys();
160+
while (keys.hasNext()) {
161+
String key = keys.next();
162+
String value = null;
163+
try {
164+
value = style.getString(key);
165+
} catch (JSONException e) {
166+
throw new RuntimeException(e);
167+
}
168+
styleMap.put(key, value);
169+
}
170+
171+
StringBuilder styleString = new StringBuilder();
172+
173+
for (Map.Entry<String, String> entry : styleMap.entrySet()) {
174+
String property = entry.getKey();
175+
String value = entry.getValue();
176+
177+
styleString.append(property).append(": ").append(value).append("; ");
178+
}
179+
180+
return styleString.toString();
181+
}
182+
183+
private String getNodeStr(JSONObject nodeObject, String key) {
184+
String herf = nodeObject.optJSONObject("attrs").optString(key); // key might be [href/src]
185+
if (herf == null || herf.isEmpty()) {
186+
herf = nodeObject.optJSONObject("attrs").optString("url");
187+
}
188+
return herf;
189+
}
190+
191+
protected String findTitleOrUid(JSONObject embeddedObject) {
192+
String _title = "";
193+
if (embeddedObject != null) {
194+
if (embeddedObject.has("title") && !embeddedObject.optString("title").isEmpty()) {
195+
_title = embeddedObject.optString("title");
196+
} else if (embeddedObject.has("uid")) {
197+
_title = embeddedObject.optString("uid");
198+
}
199+
}
200+
return _title;
201+
}
202+
203+
protected String findAssetTitle(JSONObject embeddedObject) {
204+
String _title = "";
205+
if (embeddedObject != null) {
206+
if (embeddedObject.has("title") && !embeddedObject.optString("title").isEmpty()) {
207+
_title = embeddedObject.optString("title");
208+
} else if (embeddedObject.has("filename")) {
209+
_title = embeddedObject.optString("filename");
210+
} else if (embeddedObject.has("uid")) {
211+
_title = embeddedObject.optString("uid");
212+
}
213+
}
214+
return _title;
215+
}
216+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package com.contentstack.sdk;
2+
3+
import java.util.jar.Attributes;
4+
5+
public class Metadata {
6+
String text;
7+
String itemType;
8+
String itemUid;
9+
String contentTypeUid;
10+
StyleType styleType;
11+
String outerHTML;
12+
Attributes attributes;
13+
14+
public Metadata(String text, String itemType, String itemUid, String contentTypeUid,
15+
String styleType, String outerHTML, Attributes attributes) {
16+
this.text = text;
17+
this.itemType = itemType;
18+
this.itemUid = itemUid;
19+
this.contentTypeUid = contentTypeUid;
20+
this.styleType = StyleType.valueOf(styleType.toUpperCase());
21+
this.outerHTML = outerHTML;
22+
this.attributes = attributes;
23+
}
24+
25+
@Override
26+
public String toString() {
27+
return "EmbeddedObject{" +
28+
"text='" + text + '\'' +
29+
"type='" + itemType + '\'' +
30+
", uid='" + itemUid + '\'' +
31+
", contentTypeUid='" + contentTypeUid + '\'' +
32+
", sysStyleType=" + styleType +
33+
", outerHTML='" + outerHTML + '\'' +
34+
", attributes='" + attributes + '\'' +
35+
'}';
36+
}
37+
38+
/**
39+
* The getText() function returns the value of the text variable.
40+
*
41+
* @return The method is returning a String value.
42+
*/
43+
public String getText() {
44+
return text;
45+
}
46+
47+
/**
48+
* The getItemType() function returns the type of an item.
49+
*
50+
* @return The method is returning the value of the variable "itemType".
51+
*/
52+
public String getItemType() {
53+
return itemType;
54+
}
55+
56+
/**
57+
* The function returns the attributes of an object.
58+
*
59+
* @return The method is returning an object of type Attributes.
60+
*/
61+
public Attributes getAttributes() {
62+
return attributes;
63+
}
64+
65+
/**
66+
* The getItemUid() function returns the itemUid value.
67+
*
68+
* @return The method is returning the value of the variable "itemUid".
69+
*/
70+
public String getItemUid() {
71+
return itemUid;
72+
}
73+
74+
/**
75+
* The function returns the content type UID as a string.
76+
*
77+
* @return The method is returning the value of the variable "contentTypeUid".
78+
*/
79+
public String getContentTypeUid() {
80+
return contentTypeUid;
81+
}
82+
83+
/**
84+
* The function returns the value of the styleType variable.
85+
*
86+
* @return The method is returning the value of the variable "styleType" of type StyleType.
87+
*/
88+
public StyleType getStyleType() {
89+
return styleType;
90+
}
91+
92+
/**
93+
* The getOuterHTML() function returns the outer HTML of an element.
94+
*
95+
* @return The method is returning the value of the variable "outerHTML".
96+
*/
97+
public String getOuterHTML() {
98+
return outerHTML;
99+
}
100+
}
101+
102+
enum StyleType {
103+
BLOCK, INLINE, LINK, DISPLAY, DOWNLOAD,
104+
}

0 commit comments

Comments
 (0)