Skip to content

Commit 9901f5e

Browse files
committed
🎉 ✨ Json to Html feature added
1 parent 7e05e97 commit 9901f5e

File tree

4 files changed

+198
-17
lines changed

4 files changed

+198
-17
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
contentstack-*
1+
contentstack_utils-*
22
build_doc.sh
33
test
44
doc
@@ -7,4 +7,4 @@ coverage
77
\.yardoc
88
.DS_Store
99
.bundle/
10-
*/rspec_results.html
10+
**/rspec_results.html

lib/contentstack_utils/model/metadata.rb

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,46 @@ module ContentstackUtils
33
module Model
44
class Metadata
55
def initialize( element )
6-
@itemType = element.attribute('type')
7-
@styleType = element.attribute('sys-style-type')
8-
@itemUid ||= element.attribute('data-sys-entry-uid') || element.attribute('data-sys-asset-uid')
9-
@contentTypeUid = element.attribute('data-sys-content-type-uid')
10-
@text = element.text
11-
@element = element
12-
@attributes = element.attributes
6+
if element.instance_of? Nokogiri::XML::Element
7+
@itemType = element.attribute('type') ? element.attribute('type').value : nil
8+
@styleType = element.attribute('sys-style-type') ? element.attribute('sys-style-type').value : nil
9+
@itemUid ||= (element.attribute('data-sys-entry-uid') ? element.attribute('data-sys-entry-uid').value : nil) || (element.attribute('data-sys-asset-uid') ? element.attribute('data-sys-asset-uid').value : nil)
10+
@contentTypeUid = element.attribute('data-sys-content-type-uid') ? element.attribute('data-sys-content-type-uid').value : nil
11+
@text = element.text
12+
@element = element
13+
@attributes = element
14+
else
15+
@itemType = element["attrs"]['type']
16+
@styleType = element["attrs"]['display-type']
17+
@itemUid ||= element["attrs"]['entry-uid'] || element["attrs"]['asset-uid']
18+
@contentTypeUid = element["attrs"]['content-type-uid']
19+
if element["children"] && element["children"].length() > 0
20+
child = element["children"]
21+
for item in child do
22+
if item["type"] == nil && item["text"]
23+
@text = item["text"]
24+
end
25+
end
26+
end
27+
@element = element
28+
@attributes = element["attrs"]
29+
end
1330
end
1431

1532
def item_type
16-
@itemType ? @itemType.value : nil
33+
@itemType ? @itemType : nil
1734
end
1835

1936
def style_type
20-
@styleType ? @styleType.value : nil
37+
@styleType ? @styleType : nil
2138
end
2239

2340
def item_uid
24-
@itemUid ? @itemUid.value : nil
41+
@itemUid ? @itemUid : nil
2542
end
2643

2744
def content_type_uid
28-
@contentTypeUid ? @contentTypeUid.value : nil
45+
@contentTypeUid ? @contentTypeUid : nil
2946
end
3047

3148
def text
@@ -39,6 +56,14 @@ def element
3956
def attributes
4057
@attributes
4158
end
59+
60+
def get_attribute_value(string)
61+
if @attributes.instance_of? Nokogiri::XML::Element
62+
@attributes.attribute(string) ? @attributes.attribute(string).value : nil
63+
else
64+
@attributes[string]
65+
end
66+
end
4267
end
4368
end
4469
end

lib/contentstack_utils/model/options.rb

Lines changed: 83 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module ContentstackUtils
55
module Model
66
class Options < Interface::Rendarable
77

8-
def initialize(entry)
8+
def initialize(entry = nil)
99
@entry = entry
1010
end
1111

@@ -21,11 +21,90 @@ def render_option(embeddedObject, metadata)
2121
when 'inline'
2222
renderString = "<span>#{embeddedObject["title"] || embeddedObject["uid"]}</span>";
2323
when 'link'
24-
renderString = "<a href='#{(metadata.attributes["href"] ? metadata.attributes["href"].value : nil) || embeddedObject["url"] || embeddedObject["title"] || embeddedObject["uid"]}'>#{(metadata.text && metadata.text != '' ? metadata.text : (embeddedObject["title"] || embeddedObject["uid"]))}</a>";
24+
metadata.get_attribute_value("href")
25+
renderString = "<a href='#{metadata.get_attribute_value("href") || embeddedObject["url"] || embeddedObject["title"] || embeddedObject["uid"]}'>#{(metadata.text && metadata.text != '' ? metadata.text : (embeddedObject["title"] || embeddedObject["uid"]))}</a>";
2526
when 'display'
26-
renderString = "<img src='#{(metadata.attributes["href"] ? metadata.attributes["href"].value : nil)|| embeddedObject["url"]}' alt='#{(metadata.attributes["alt"] ? metadata.attributes["alt"].value : (embeddedObject["title"] || embeddedObject["filename"] || embeddedObject["uid"]))}' />";
27+
renderString = "<img src='#{metadata.get_attribute_value("src")|| embeddedObject["url"]}' alt='#{(metadata.attributes["alt"] ? metadata.attributes["alt"].value : (embeddedObject["title"] || embeddedObject["filename"] || embeddedObject["uid"]))}' />";
2728
when 'download'
28-
renderString = "<a href='#{(metadata.attributes["href"] ? metadata.attributes["href"].value : nil) || embeddedObject["url"]}'>#{(metadata.text && metadata.text != '' ? metadata.text : (embeddedObject["filename"] || embeddedObject["title"] || embeddedObject["uid"]))}</a>";
29+
renderString = "<a href='#{metadata.get_attribute_value("href") || embeddedObject["url"]}'>#{(metadata.text && metadata.text != '' ? metadata.text : (embeddedObject["filename"] || embeddedObject["title"] || embeddedObject["uid"]))}</a>";
30+
end
31+
renderString
32+
end
33+
34+
def render_mark(mark_type, text)
35+
renderString = text
36+
case mark_type
37+
when 'bold'
38+
renderString = "<strong>#{text}</strong>"
39+
when 'italic'
40+
renderString = "<em>#{text}</em>"
41+
when 'underline'
42+
renderString = "<u>#{text}</u>"
43+
when 'strikethrough'
44+
renderString = "<strike>#{text}</strike>"
45+
when 'inlineCode'
46+
renderString = "<span>#{text}</span>"
47+
when 'subscript'
48+
renderString = "<sub>#{text}</sub>"
49+
when 'superscript'
50+
renderString = "<sup>#{text}</sup>"
51+
end
52+
renderString
53+
end
54+
55+
def render_node(node_type, node, inner_html)
56+
renderString = ""
57+
case node_type
58+
when 'doc'
59+
renderString = ""
60+
when 'p'
61+
renderString = "<p>#{inner_html}</p>"
62+
when 'a'
63+
renderString = "<a href='#{node["attrs"]["href"] || ""}'>#{inner_html}</a>"
64+
when 'img'
65+
renderString = "<img src='#{node["attrs"]["src"] || ""}' />#{inner_html}"
66+
when 'embed'
67+
renderString = "<iframe src='#{node["attrs"]["src"] || ""}'></iframe>"
68+
when 'h1'
69+
renderString = "<h1>#{inner_html}</h1>"
70+
when 'h2'
71+
renderString = "<h2>#{inner_html}</h2>"
72+
when 'h3'
73+
renderString = "<h3>#{inner_html}</h3>"
74+
when 'h4'
75+
renderString = "<h4>#{inner_html}</h4>"
76+
when 'h5'
77+
renderString = "<h5>#{inner_html}</h5>"
78+
when 'h6'
79+
renderString = "<h6>#{inner_html}</h6>"
80+
when 'ol'
81+
renderString = "<ol>#{inner_html}</ol>"
82+
when 'ul'
83+
renderString = "<ul>#{inner_html}</ul>"
84+
when 'li'
85+
renderString = "<li>#{inner_html}</li>"
86+
when 'hr'
87+
renderString = "<hr />"
88+
when 'table'
89+
renderString = "<table>#{inner_html}</table>"
90+
when 'thead'
91+
renderString = "<thead>#{inner_html}</thead>"
92+
when 'tbody'
93+
renderString = "<tbody>#{inner_html}</tbody>"
94+
when 'tfoot'
95+
renderString = "<tfoot>#{inner_html}</tfoot>"
96+
when 'tr'
97+
renderString = "<tr>#{inner_html}</tr>"
98+
when 'th'
99+
renderString = "<th>#{inner_html}</th>"
100+
when 'td'
101+
renderString = "<td>#{inner_html}</td>"
102+
when 'blockquote'
103+
renderString = "<blockquote>#{inner_html}</blockquote>"
104+
when 'code'
105+
renderString = "<code>#{inner_html}</code>"
106+
when 'reference'
107+
renderString = ""
29108
end
30109
renderString
31110
end

lib/contentstack_utils/utils.rb

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,83 @@ def self.render_content(content, options)
1616
end
1717
end
1818

19+
def self.json_to_html(content, options)
20+
if (content.instance_of? Array)
21+
result = []
22+
content.each do |n|
23+
result.push(json_doc_to_html(n, options))
24+
end
25+
result
26+
elsif content.instance_of? Hash
27+
json_doc_to_html(content, options)
28+
end
29+
end
30+
31+
private_class_method def self.json_doc_to_html(node, options)
32+
result = ""
33+
if node["children"] && node["children"].length() > 0
34+
result = node_children_to_html(node["children"], options)
35+
end
36+
result
37+
end
38+
39+
private_class_method def self.node_children_to_html(nodes, options)
40+
nodes.map {|node| node_to_html(node, options)}.join("")
41+
end
42+
43+
private_class_method def self.node_to_html(node, options)
44+
html_result = ""
45+
if node["type"] == nil && node["text"]
46+
html_result = text_to_htms(node, options)
47+
elsif node["type"]
48+
if node["type"] == "reference"
49+
html_result = reference_to_html(node, options)
50+
else
51+
inner_html = json_doc_to_html(node, options)
52+
html_result = options.render_node(node["type"], node, inner_html)
53+
end
54+
end
55+
html_result
56+
end
57+
58+
private_class_method def self.text_to_htms(node, options)
59+
text = node["text"]
60+
if node["superscript"]
61+
text = options.render_mark("superscript", text)
62+
end
63+
if node["subscript"]
64+
text = options.render_mark("subscript", text)
65+
end
66+
if node["inlineCode"]
67+
text = options.render_mark("inlineCode", text)
68+
end
69+
if node["strikethrough"]
70+
text = options.render_mark("strikethrough", text)
71+
end
72+
if node["underline"]
73+
text = options.render_mark("underline", text)
74+
end
75+
if node["italic"]
76+
text = options.render_mark("italic", text)
77+
end
78+
if node["bold"]
79+
text = options.render_mark("bold", text)
80+
end
81+
text
82+
end
83+
84+
private_class_method def self.reference_to_html(node, options)
85+
result = ""
86+
if options.entry != nil
87+
metadata = Model::Metadata.new(node)
88+
object = findObject(metadata, options.entry)
89+
if object!= nil && object.length() > 0
90+
result = options.render_option(object[0], metadata)
91+
end
92+
end
93+
result
94+
end
95+
1996
private_class_method def self.render_string(string, options)
2097
xml_doc = Nokogiri::HTML(appendFrame(string))
2198
result = xml_doc.xpath('//documentfragmentcontainer').inner_html

0 commit comments

Comments
 (0)