Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added support for json to html conversion #56

Closed
wants to merge 49 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
a5e841d
v3.14.0: function support added
ishaileshmishra Feb 6, 2024
080cc05
v3.14.0: function support added
ishaileshmishra Feb 6, 2024
b11ea95
v3.14.0: function support added
ishaileshmishra Feb 8, 2024
9131a2b
v3.14.0: function support added
ishaileshmishra Feb 8, 2024
eb1432d
Merge pull request #47 from contentstack/CS-43851/sequential-sync
ishaileshmishra Feb 8, 2024
d2eded9
Create publish-snapshot.yaml
aravindbuilt Feb 12, 2024
3acb124
Rename publish-snapshot.yaml to publish-snapshot.yml
aravindbuilt Feb 12, 2024
7c0b706
Create publish-release.yml
aravindbuilt Feb 12, 2024
681b78b
Update publish-snapshot.yml
aravindbuilt Feb 12, 2024
e2d4d7b
Update publish-release.yml
aravindbuilt Feb 12, 2024
a3d55ff
Merge branch 'master' into next
ishaileshmishra Feb 12, 2024
323ff44
v3.14.0: function support added
ishaileshmishra Feb 12, 2024
b312ccb
Update publish-snapshot.yml
aravindbuilt Feb 12, 2024
e8d445c
Update publish-snapshot.yml
aravindbuilt Feb 12, 2024
185a2c7
Update sca-scan.yml
aravindbuilt Feb 12, 2024
665bf64
Update publish-snapshot.yml
aravindbuilt Feb 12, 2024
7938f7c
v3.14.0: function support added
ishaileshmishra Feb 12, 2024
a5d76b4
v3.14.0: function support added
ishaileshmishra Feb 12, 2024
fb6e92d
Update publish-snapshot.yml
aravindbuilt Feb 12, 2024
8d43e49
v3.14.0: function support added
ishaileshmishra Feb 12, 2024
30e9ae5
v3.14.0: function support added
ishaileshmishra Feb 12, 2024
018ec83
v3.14.0: function support added
ishaileshmishra Feb 12, 2024
1faadce
Update publish-snapshot.yml
aravindbuilt Feb 12, 2024
c23575e
Update publish-snapshot.yml
aravindbuilt Feb 12, 2024
b4198af
Update publish-snapshot.yml
aravindbuilt Feb 12, 2024
a709fb4
Update publish-snapshot.yml
aravindbuilt Feb 12, 2024
157f5ff
Update publish-snapshot.yml
aravindbuilt Feb 12, 2024
61ae6ea
v3.14.0: function support added
ishaileshmishra Feb 12, 2024
683ffe7
v3.14.0: function support added
ishaileshmishra Feb 12, 2024
3b8e6ef
Update sca-scan.yml
aravindbuilt Feb 13, 2024
ceb22db
Update publish-snapshot.yml
aravindbuilt Feb 13, 2024
ec3c9ae
v3.14.0: function support added
ishaileshmishra Feb 13, 2024
8d1217d
v3.14.0: function support added
ishaileshmishra Feb 13, 2024
0483a0b
Update codeql-analysis.yml
aravindbuilt Feb 13, 2024
23e0420
Update publish-release.yml
aravindbuilt Feb 13, 2024
9dfe45a
v3.14.0: function support added
ishaileshmishra Feb 13, 2024
e2a1ad7
v3.14.0: function support added
ishaileshmishra Feb 13, 2024
39762c3
v3.14.0: function support added
ishaileshmishra Feb 14, 2024
daaf8a1
feat: gcp support implementation
harshithad0703 May 6, 2024
df0f6f2
updated path for keystore
harshithad0703 May 6, 2024
f1dab8f
chore: version bump to 3.14.0
harshithad0703 May 7, 2024
0158902
changeLog updated
harshithad0703 May 7, 2024
379f9de
Merge branch 'master' into feat/DX-67-gcp-support
harshithad0703 May 7, 2024
8f9b157
feat: specified host name in stack.java
harshithad0703 May 7, 2024
39b22ac
Merge pull request #51 from contentstack/feat/DX-67-gcp-support
harshithad0703 May 7, 2024
e1c067f
feat: added support to convert json to html
May 8, 2024
ed9a522
fix: resolved conflicts
May 15, 2024
07aabb6
fix: resolved conflicts
May 15, 2024
1a12e00
Merge branch 'master' into feat/DX-517
vkalta Jun 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ buildscript {
}

dependencies {
//classpath "com.android.tools.build:gradle:8.2.1" //8.2.1
classpath 'com.android.tools.build:gradle:7.4.2'
classpath 'io.github.gradle-nexus:publish-plugin:2.0.0-rc-1'
classpath "org.jacoco:org.jacoco.core:$jacoco_version"
Expand Down
17 changes: 11 additions & 6 deletions contentstack/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ android {
// }
}
}
// signing {
// // Specify key and other signing details
// useGpgCmd()
// sign configurations.archives
// }
signingConfigs {
debug {
storeFile file("../key.keystore")
Expand Down Expand Up @@ -111,12 +116,12 @@ android {
testCoverageEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

buildConfigField "String", "host", localProperties['host']
buildConfigField "String", "APIKey", localProperties['APIKey']
buildConfigField "String", "deliveryToken", localProperties['deliveryToken']
buildConfigField "String", "environment", localProperties['environment']
buildConfigField "String", "contentTypeUID", localProperties['contentType']
buildConfigField "String", "assetUID", localProperties['assetUid']
buildConfigField "String", "host", localProperties['host']
buildConfigField "String", "APIKey", localProperties['APIKey']
buildConfigField "String", "deliveryToken", localProperties['deliveryToken']
buildConfigField "String", "environment", localProperties['environment']
buildConfigField "String", "contentTypeUID", localProperties['contentType']
buildConfigField "String", "assetUID", localProperties['assetUid']
}
release {
minifyEnabled false
Expand Down
216 changes: 216 additions & 0 deletions contentstack/src/main/java/com/contentstack/sdk/DefaultOption.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
package com.contentstack.sdk;

import android.text.TextUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;

public class DefaultOption implements Option {
@Override
public String renderOptions(JSONObject embeddedObject, Metadata metadata) {
switch (metadata.getStyleType()) {
case BLOCK:
return "<div><p>" + findTitleOrUid(embeddedObject) + "</p><div><p>Content type: <span>" + embeddedObject.optString("_content_type_uid") + "</span></p></div>";
case INLINE:
return "<span>" + findTitleOrUid(embeddedObject) + "</span>";
case LINK:
return "<a href=\"" + embeddedObject.optString("url") + "\">" + findTitleOrUid(embeddedObject) + "</a>";
case DISPLAY:
return "<img src=\"" + embeddedObject.optString("url") + "\" alt=\"" + findAssetTitle(embeddedObject) + "\" />";
default:
return "";
}
}

@Override
public String renderMark(MarkType markType, String text) {
switch (markType) {
case SUPERSCRIPT:
return "<sup>" + text + "</sup>";
case SUBSCRIPT:
return "<sub>" + text + "</sub>";
case INLINECODE:
return "<span>" + text + "</span>";
case STRIKETHROUGH:
return "<strike>" + text + "</strike>";
case UNDERLINE:
return "<u>" + text + "</u>";
case ITALIC:
return "<em>" + text + "</em>";
case BOLD:
return "<strong>" + text + "</strong>";
case BREAK:
return "<br />" + text.replace("\n", "");
default:
return text;
}
}

private String escapeInjectHtml(JSONObject nodeObj, String nodeType) {
String injectedHtml = getNodeStr(nodeObj, nodeType);
return TextUtils.htmlEncode(injectedHtml);
}

@Override
public String renderNode(String nodeType, JSONObject nodeObject, NodeCallback callback) {
String strAttrs = strAttrs(nodeObject);
String children = callback.renderChildren(nodeObject.optJSONArray("children"));
switch (nodeType) {
case "p":
return "<p" + strAttrs + ">" + children + "</p>";
case "a":
return "<a" + strAttrs + " href=\"" + escapeInjectHtml(nodeObject, "href") + "\">" + children + "</a>";
case "img":
String assetLink = getNodeStr(nodeObject, "asset-link");
if (!assetLink.isEmpty()) {
JSONObject attrs = nodeObject.optJSONObject("attrs");
if (attrs.has("link")) {
return "<a href=\"" + escapeInjectHtml(nodeObject, "link") + "\" >" + "<img" + strAttrs + " src=\"" + escapeInjectHtml(nodeObject, "asset-link") + "\" />" + children + "</a>";
}
return "<img" + strAttrs + " src=\"" + escapeInjectHtml(nodeObject, "asset-link") + "\" />" + children;
}
return "<img" + strAttrs + " src=\"" + escapeInjectHtml(nodeObject, "src") + "\" />" + children;
case "embed":
return "<iframe" + strAttrs + " src=\"" + escapeInjectHtml(nodeObject, "src") + "\"" + children + "</iframe>";
case "h1":
return "<h1" + strAttrs + ">" + children + "</h1>";
case "h2":
return "<h2" + strAttrs + ">" + children + "</h2>";
case "h3":
return "<h3" + strAttrs + ">" + children + "</h3>";
case "h4":
return "<h4" + strAttrs + ">" + children + "</h4>";
case "h5":
return "<h5" + strAttrs + ">" + children + "</h5>";
case "h6":
return "<h6" + strAttrs + ">" + children + "</h6>";
case "ol":
return "<ol" + strAttrs + ">" + children + "</ol>";
case "ul":
return "<ul" + strAttrs + ">" + children + "</ul>";
case "li":
return "<li" + strAttrs + ">" + children + "</li>";
case "hr":
return "<hr" + strAttrs + " />";
case "table":
return "<table " + strAttrs + ">" + children + "</table>";
case "thead":
return "<thead " + strAttrs + ">" + children + "</thead>";
case "tbody":
return "<tbody" + strAttrs + ">" + children + "</tbody>";
case "tfoot":
return "<tfoot" + strAttrs + ">" + children + "</tfoot>";
case "tr":
return "<tr" + strAttrs + ">" + children + "</tr>";
case "th":
return "<th" + strAttrs + ">" + children + "</th>";
case "td":
return "<td" + strAttrs + ">" + children + "</td>";
case "blockquote":
return "<blockquote" + strAttrs + ">" + children + "</blockquote>";
case "code":
return "<code" + strAttrs + ">" + children + "</code>";
case "reference":
return "";
case "fragment":
return "<fragment" + strAttrs + ">" + children + "</fragment>";
default:
return children;
}
}

String strAttrs(JSONObject nodeObject) {
StringBuilder result = new StringBuilder();
if (nodeObject.has("attrs")) {
JSONObject attrsObject = nodeObject.optJSONObject("attrs");
if (attrsObject != null && attrsObject.length() > 0) {
for (Iterator<String> it = attrsObject.keys(); it.hasNext(); ) {
String key = it.next();
Object objValue = attrsObject.opt(key);
String value = objValue.toString();
// If style is available, do styling calculations
if (Objects.equals(key, "style")) {
String resultStyle = stringifyStyles(attrsObject.optJSONObject("style"));
result.append(" ").append(key).append("=\"").append(resultStyle).append("\"");
} else {
String[] ignoreKeys = {"href", "asset-link", "src", "url"};
ArrayList<String> ignoreKeysList = new ArrayList<>(Arrays.asList(ignoreKeys));
if (!ignoreKeysList.contains(key)) {
result.append(" ").append(key).append("=\"").append(value).append("\"");
}
}
}
}
}
return result.toString();
}

private String stringifyStyles(JSONObject style) {
Map<String, String> styleMap = new HashMap<>();

// Convert JSONObject to a Map
Iterator<String> keys = style.keys();
while (keys.hasNext()) {
String key = keys.next();
String value = null;
try {
value = style.getString(key);
} catch (JSONException e) {
throw new RuntimeException(e);
}
styleMap.put(key, value);
}

StringBuilder styleString = new StringBuilder();

for (Map.Entry<String, String> entry : styleMap.entrySet()) {
String property = entry.getKey();
String value = entry.getValue();

styleString.append(property).append(": ").append(value).append("; ");
}

return styleString.toString();
}

private String getNodeStr(JSONObject nodeObject, String key) {
String herf = nodeObject.optJSONObject("attrs").optString(key); // key might be [href/src]
if (herf == null || herf.isEmpty()) {
herf = nodeObject.optJSONObject("attrs").optString("url");
}
return herf;
}

protected String findTitleOrUid(JSONObject embeddedObject) {
String _title = "";
if (embeddedObject != null) {
if (embeddedObject.has("title") && !embeddedObject.optString("title").isEmpty()) {
_title = embeddedObject.optString("title");
} else if (embeddedObject.has("uid")) {
_title = embeddedObject.optString("uid");
}
}
return _title;
}

protected String findAssetTitle(JSONObject embeddedObject) {
String _title = "";
if (embeddedObject != null) {
if (embeddedObject.has("title") && !embeddedObject.optString("title").isEmpty()) {
_title = embeddedObject.optString("title");
} else if (embeddedObject.has("filename")) {
_title = embeddedObject.optString("filename");
} else if (embeddedObject.has("uid")) {
_title = embeddedObject.optString("uid");
}
}
return _title;
}
}
104 changes: 104 additions & 0 deletions contentstack/src/main/java/com/contentstack/sdk/Metadata.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package com.contentstack.sdk;

import java.util.jar.Attributes;

public class Metadata {
String text;
String itemType;
String itemUid;
String contentTypeUid;
StyleType styleType;
String outerHTML;
Attributes attributes;

public Metadata(String text, String itemType, String itemUid, String contentTypeUid,
String styleType, String outerHTML, Attributes attributes) {
this.text = text;
this.itemType = itemType;
this.itemUid = itemUid;
this.contentTypeUid = contentTypeUid;
this.styleType = StyleType.valueOf(styleType.toUpperCase());
this.outerHTML = outerHTML;
this.attributes = attributes;
}

@Override
public String toString() {
return "EmbeddedObject{" +
"text='" + text + '\'' +
"type='" + itemType + '\'' +
", uid='" + itemUid + '\'' +
", contentTypeUid='" + contentTypeUid + '\'' +
", sysStyleType=" + styleType +
", outerHTML='" + outerHTML + '\'' +
", attributes='" + attributes + '\'' +
'}';
}

/**
* The getText() function returns the value of the text variable.
*
* @return The method is returning a String value.
*/
public String getText() {
return text;
}

/**
* The getItemType() function returns the type of an item.
*
* @return The method is returning the value of the variable "itemType".
*/
public String getItemType() {
return itemType;
}

/**
* The function returns the attributes of an object.
*
* @return The method is returning an object of type Attributes.
*/
public Attributes getAttributes() {
return attributes;
}

/**
* The getItemUid() function returns the itemUid value.
*
* @return The method is returning the value of the variable "itemUid".
*/
public String getItemUid() {
return itemUid;
}

/**
* The function returns the content type UID as a string.
*
* @return The method is returning the value of the variable "contentTypeUid".
*/
public String getContentTypeUid() {
return contentTypeUid;
}

/**
* The function returns the value of the styleType variable.
*
* @return The method is returning the value of the variable "styleType" of type StyleType.
*/
public StyleType getStyleType() {
return styleType;
}

/**
* The getOuterHTML() function returns the outer HTML of an element.
*
* @return The method is returning the value of the variable "outerHTML".
*/
public String getOuterHTML() {
return outerHTML;
}
}

enum StyleType {
BLOCK, INLINE, LINK, DISPLAY, DOWNLOAD,
}
Loading
Loading