contentstack_utils 1.1.1 → 1.1.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,163 +1,163 @@
1
- require_relative './model/options.rb'
2
- require_relative './model/metadata.rb'
3
- require_relative './support/helper.rb'
4
- require 'nokogiri'
5
-
6
- module ContentstackUtils
7
- def self.render_content(content, options)
8
- if (content.instance_of? Array)
9
- result = []
10
- content.each do |n|
11
- result.push(render_string(n, options))
12
- end
13
- result
14
- elsif content.instance_of? String
15
- render_string(content, options)
16
- end
17
- end
18
-
19
- def self.json_to_html(content, options)
20
- reference = -> (metadata){
21
- result = ""
22
- if options.entry != nil
23
- object = findObject(metadata, options.entry)
24
- if object!= nil && object.length() > 0
25
- result = options.render_option(object[0], metadata)
26
- end
27
- end
28
- result
29
- }
30
- if (content.instance_of? Array)
31
- result = []
32
- content.each do |n|
33
- result.push(json_doc_to_html(n, options, reference) )
34
- end
35
- result
36
- elsif content.instance_of? Hash
37
- json_doc_to_html(content, options, reference)
38
- end
39
- end
40
-
41
- def self.json_doc_to_html(node, options, callback)
42
- result = ""
43
- if node["children"] && node["children"].length() > 0
44
- result = node_children_to_html(node["children"], options, callback)
45
- end
46
- result
47
- end
48
-
49
- private_class_method def self.node_children_to_html(nodes, options, callback)
50
- nodes.map {|node| node_to_html(node, options, callback)}.join("")
51
- end
52
-
53
- private_class_method def self.node_to_html(node, options, callback)
54
- html_result = ""
55
- if node["type"] == nil && node["text"]
56
- html_result = text_to_htms(node, options)
57
- elsif node["type"]
58
- if node["type"] == "reference"
59
- metadata = Model::Metadata.new(node)
60
- html_result = callback.call(metadata)
61
- else
62
- inner_html = json_doc_to_html(node, options, callback)
63
- html_result = options.render_node(node["type"], node, inner_html)
64
- end
65
- end
66
- html_result
67
- end
68
-
69
- private_class_method def self.text_to_htms(node, options)
70
- text = node["text"]
71
- if node["superscript"]
72
- text = options.render_mark("superscript", text)
73
- end
74
- if node["subscript"]
75
- text = options.render_mark("subscript", text)
76
- end
77
- if node["inlineCode"]
78
- text = options.render_mark("inlineCode", text)
79
- end
80
- if node["strikethrough"]
81
- text = options.render_mark("strikethrough", text)
82
- end
83
- if node["underline"]
84
- text = options.render_mark("underline", text)
85
- end
86
- if node["italic"]
87
- text = options.render_mark("italic", text)
88
- end
89
- if node["bold"]
90
- text = options.render_mark("bold", text)
91
- end
92
- text
93
- end
94
-
95
- private_class_method def self.render_string(string, options)
96
- xml_doc = Nokogiri::HTML(appendFrame(string))
97
- result = xml_doc.xpath('//documentfragmentcontainer').inner_html
98
- findEmbeddedObject(xml_doc).each do |metadata|
99
- object = findObject(metadata, options.entry)
100
- replaceString = ''
101
- if object!= nil && object.length() > 0
102
- replaceString = options.render_option(object[0], metadata)
103
- end
104
- result = result.sub(metadata.element.to_html, replaceString)
105
- end
106
- result
107
- end
108
-
109
- private_class_method def self.findEmbeddedObject(doc)
110
- metadataArray = []
111
- doc.xpath('//*[contains(@class, "embedded-asset") or contains(@class, "embedded-entry")]').each do |n|
112
- metadataArray.push(Model::Metadata.new(n))
113
- end
114
- metadataArray
115
- end
116
-
117
- private_class_method def self.findObject(metadata, entry)
118
- if entry.has_key? '_embedded_items'
119
- embedItems = entry['_embedded_items']
120
- keys = embedItems.keys
121
- keys.each do |key|
122
- object = embedItems[key].select { |embedObject| embedObject['uid'] == metadata.item_uid }
123
- if object != nil && object.length() > 0
124
- return object
125
- end
126
- end
127
- end
128
- return nil
129
- end
130
-
131
- module GQL
132
- include ContentstackUtils
133
- def self.json_to_html(content, options)
134
- embeddedItems = []
135
- if content.has_key? 'embedded_itemsConnection'
136
- embeddedItems = content['embedded_itemsConnection']['edges'] || []
137
- end
138
- reference = -> (metadata){
139
- result = ""
140
- if embeddedItems != nil
141
- object = embeddedItems.select { |embedObject| embedObject['node']["system"]["uid"] == metadata.item_uid }
142
- if object != nil && object.length() > 0
143
- result = options.render_option(object[0]["node"], metadata)
144
- end
145
- end
146
- result
147
- }
148
-
149
- if content.has_key? 'json'
150
- json = content['json']
151
- if (json.instance_of? Array)
152
- result = []
153
- json.each do |n|
154
- result.push(ContentstackUtils.json_doc_to_html(n, options, reference))
155
- end
156
- result
157
- elsif json.instance_of? Hash
158
- ContentstackUtils.json_doc_to_html(json, options, reference)
159
- end
160
- end
161
- end
162
- end
163
- end
1
+ require_relative './model/options.rb'
2
+ require_relative './model/metadata.rb'
3
+ require_relative './support/helper.rb'
4
+ require 'nokogiri'
5
+
6
+ module ContentstackUtils
7
+ def self.render_content(content, options)
8
+ if (content.instance_of? Array)
9
+ result = []
10
+ content.each do |n|
11
+ result.push(render_string(n, options))
12
+ end
13
+ result
14
+ elsif content.instance_of? String
15
+ render_string(content, options)
16
+ end
17
+ end
18
+
19
+ def self.json_to_html(content, options)
20
+ reference = -> (metadata){
21
+ result = ""
22
+ if options.entry != nil
23
+ object = findObject(metadata, options.entry)
24
+ if object!= nil && object.length() > 0
25
+ result = options.render_option(object[0], metadata)
26
+ end
27
+ end
28
+ result
29
+ }
30
+ if (content.instance_of? Array)
31
+ result = []
32
+ content.each do |n|
33
+ result.push(json_doc_to_html(n, options, reference) )
34
+ end
35
+ result
36
+ elsif content.instance_of? Hash
37
+ json_doc_to_html(content, options, reference)
38
+ end
39
+ end
40
+
41
+ def self.json_doc_to_html(node, options, callback)
42
+ result = ""
43
+ if node["children"] && node["children"].length() > 0
44
+ result = node_children_to_html(node["children"], options, callback)
45
+ end
46
+ result
47
+ end
48
+
49
+ private_class_method def self.node_children_to_html(nodes, options, callback)
50
+ nodes.map {|node| node_to_html(node, options, callback)}.join("")
51
+ end
52
+
53
+ private_class_method def self.node_to_html(node, options, callback)
54
+ html_result = ""
55
+ if node["type"] == nil && node["text"]
56
+ html_result = text_to_htms(node, options)
57
+ elsif node["type"]
58
+ if node["type"] == "reference"
59
+ metadata = Model::Metadata.new(node)
60
+ html_result = callback.call(metadata)
61
+ else
62
+ inner_html = json_doc_to_html(node, options, callback)
63
+ html_result = options.render_node(node["type"], node, inner_html)
64
+ end
65
+ end
66
+ html_result
67
+ end
68
+
69
+ private_class_method def self.text_to_htms(node, options)
70
+ text = node["text"]
71
+ if node["superscript"]
72
+ text = options.render_mark("superscript", text)
73
+ end
74
+ if node["subscript"]
75
+ text = options.render_mark("subscript", text)
76
+ end
77
+ if node["inlineCode"]
78
+ text = options.render_mark("inlineCode", text)
79
+ end
80
+ if node["strikethrough"]
81
+ text = options.render_mark("strikethrough", text)
82
+ end
83
+ if node["underline"]
84
+ text = options.render_mark("underline", text)
85
+ end
86
+ if node["italic"]
87
+ text = options.render_mark("italic", text)
88
+ end
89
+ if node["bold"]
90
+ text = options.render_mark("bold", text)
91
+ end
92
+ text
93
+ end
94
+
95
+ private_class_method def self.render_string(string, options)
96
+ xml_doc = Nokogiri::HTML(appendFrame(string))
97
+ result = xml_doc.xpath('//documentfragmentcontainer').inner_html
98
+ findEmbeddedObject(xml_doc).each do |metadata|
99
+ object = findObject(metadata, options.entry)
100
+ replaceString = ''
101
+ if object!= nil && object.length() > 0
102
+ replaceString = options.render_option(object[0], metadata)
103
+ end
104
+ result = result.sub(metadata.element.to_html, replaceString)
105
+ end
106
+ result
107
+ end
108
+
109
+ private_class_method def self.findEmbeddedObject(doc)
110
+ metadataArray = []
111
+ doc.xpath('//*[contains(@class, "embedded-asset") or contains(@class, "embedded-entry")]').each do |n|
112
+ metadataArray.push(Model::Metadata.new(n))
113
+ end
114
+ metadataArray
115
+ end
116
+
117
+ private_class_method def self.findObject(metadata, entry)
118
+ if entry.has_key? '_embedded_items'
119
+ embedItems = entry['_embedded_items']
120
+ keys = embedItems.keys
121
+ keys.each do |key|
122
+ object = embedItems[key].select { |embedObject| embedObject['uid'] == metadata.item_uid }
123
+ if object != nil && object.length() > 0
124
+ return object
125
+ end
126
+ end
127
+ end
128
+ return nil
129
+ end
130
+
131
+ module GQL
132
+ include ContentstackUtils
133
+ def self.json_to_html(content, options)
134
+ embeddedItems = []
135
+ if content.has_key? 'embedded_itemsConnection'
136
+ embeddedItems = content['embedded_itemsConnection']['edges'] || []
137
+ end
138
+ reference = -> (metadata){
139
+ result = ""
140
+ if embeddedItems != nil
141
+ object = embeddedItems.select { |embedObject| embedObject['node']["system"]["uid"] == metadata.item_uid }
142
+ if object != nil && object.length() > 0
143
+ result = options.render_option(object[0]["node"], metadata)
144
+ end
145
+ end
146
+ result
147
+ }
148
+
149
+ if content.has_key? 'json'
150
+ json = content['json']
151
+ if (json.instance_of? Array)
152
+ result = []
153
+ json.each do |n|
154
+ result.push(ContentstackUtils.json_doc_to_html(n, options, reference))
155
+ end
156
+ result
157
+ elsif json.instance_of? Hash
158
+ ContentstackUtils.json_doc_to_html(json, options, reference)
159
+ end
160
+ end
161
+ end
162
+ end
163
+ end
@@ -1,3 +1,3 @@
1
- module ContentstackUtils
2
- VERSION = "1.1.1"
1
+ module ContentstackUtils
2
+ VERSION = "1.1.3.1"
3
3
  end
@@ -1,4 +1,4 @@
1
- $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
2
-
3
- require 'contentstack_utils/version'
1
+ $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
2
+
3
+ require 'contentstack_utils/version'
4
4
  require 'contentstack_utils/utils'
@@ -1,95 +1,95 @@
1
- require 'spec_helper'
2
-
3
- RSpec.describe ContentstackUtils::Model::Metadata do
4
- describe ' Metadata test' do
5
- it 'Blank attributes' do
6
- characters = getElement('<h1>TEST</h1>', "//h1")
7
- characters.each do |n|
8
- metadata = ContentstackUtils::Model::Metadata.new(n)
9
- expect(metadata.item_type).to eq nil
10
- expect(metadata.style_type).to eq nil
11
- expect(metadata.item_uid).to eq nil
12
- expect(metadata.content_type_uid).to eq nil
13
- expect(metadata.text).to eq 'TEST'
14
- end
15
- end
16
-
17
- it 'Wrong attributes' do
18
- characters = getElement('<h1 type="" sys-style-type="" data-sys-entry-uid="" data-sys-content-type-uid="">TEST</h1>', "//h1")
19
- characters.each do |n|
20
- metadata = ContentstackUtils::Model::Metadata.new(n)
21
- expect(metadata.item_type).to eq ""
22
- expect(metadata.style_type).to eq ""
23
- expect(metadata.item_uid).to eq ""
24
- expect(metadata.content_type_uid).to eq ""
25
- expect(metadata.text).to eq 'TEST'
26
- end
27
- end
28
-
29
- it 'Attributes' do
30
- characters = getElement('<h1 type="asset" sys-style-type="inline" data-sys-entry-uid="uid" data-sys-content-type-uid="contentType">
31
- TEST</h1>', "//h1")
32
- characters.each do |n|
33
- metadata = ContentstackUtils::Model::Metadata.new(n)
34
- expect(metadata.item_type).to eq "asset"
35
- expect(metadata.style_type).to eq "inline"
36
- expect(metadata.item_uid).to eq "uid"
37
- expect(metadata.content_type_uid).to eq "contentType"
38
- expect(metadata.text).to eq '
39
- TEST'
40
- end
41
- end
42
-
43
- it 'Asset Uid Attributes' do
44
- characters = getElement('<h1 type="asset" sys-style-type="inline" data-sys-asset-uid="assetuid">TEST</h1>', "//h1")
45
- characters.each do |n|
46
- metadata = ContentstackUtils::Model::Metadata.new(n)
47
- expect(metadata.item_type).to eq "asset"
48
- expect(metadata.style_type).to eq "inline"
49
- expect(metadata.item_uid).to eq "assetuid"
50
- expect(metadata.content_type_uid).to eq nil
51
- expect(metadata.text).to eq 'TEST'
52
- end
53
- end
54
-
55
- it 'Asset Json To metadata' do
56
- doc = getJson(AssetReferenceJson)
57
- metadata = ContentstackUtils::Model::Metadata.new(doc["children"][0])
58
- expect(metadata.item_type).to eq "asset"
59
- expect(metadata.style_type).to eq "display"
60
- expect(metadata.item_uid).to eq "asset_uid_1"
61
- expect(metadata.content_type_uid).to eq 'sys_assets'
62
- expect(metadata.text).to eq ''
63
- end
64
-
65
- it 'Entry Block Json To metadata' do
66
- doc = getJson(EntryReferenceBlockJson)
67
- metadata = ContentstackUtils::Model::Metadata.new(doc["children"][0])
68
- expect(metadata.item_type).to eq "entry"
69
- expect(metadata.style_type).to eq "block"
70
- expect(metadata.item_uid).to eq "entry_uid_1"
71
- expect(metadata.content_type_uid).to eq 'content_block'
72
- expect(metadata.text).to eq ''
73
- end
74
-
75
- it 'Entry Link Json To metadata' do
76
- doc = getJson(EntryReferenceLinkJson)
77
- metadata = ContentstackUtils::Model::Metadata.new(doc["children"][0])
78
- expect(metadata.item_type).to eq "entry"
79
- expect(metadata.style_type).to eq "link"
80
- expect(metadata.item_uid).to eq "entry_uid_2"
81
- expect(metadata.content_type_uid).to eq 'embeddedrte'
82
- expect(metadata.text).to eq "/copy-of-entry-final-02"
83
- end
84
-
85
- it 'Entry Inline Json To metadata' do
86
- doc = getJson(EntryReferenceInlineJson)
87
- metadata = ContentstackUtils::Model::Metadata.new(doc["children"][0])
88
- expect(metadata.item_type).to eq "entry"
89
- expect(metadata.style_type).to eq "inline"
90
- expect(metadata.item_uid).to eq "entry_uid_3"
91
- expect(metadata.content_type_uid).to eq 'embeddedrte'
92
- expect(metadata.text).to eq ''
93
- end
94
- end
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe ContentstackUtils::Model::Metadata do
4
+ describe ' Metadata test' do
5
+ it 'Blank attributes' do
6
+ characters = getElement('<h1>TEST</h1>', "//h1")
7
+ characters.each do |n|
8
+ metadata = ContentstackUtils::Model::Metadata.new(n)
9
+ expect(metadata.item_type).to eq nil
10
+ expect(metadata.style_type).to eq nil
11
+ expect(metadata.item_uid).to eq nil
12
+ expect(metadata.content_type_uid).to eq nil
13
+ expect(metadata.text).to eq 'TEST'
14
+ end
15
+ end
16
+
17
+ it 'Wrong attributes' do
18
+ characters = getElement('<h1 type="" sys-style-type="" data-sys-entry-uid="" data-sys-content-type-uid="">TEST</h1>', "//h1")
19
+ characters.each do |n|
20
+ metadata = ContentstackUtils::Model::Metadata.new(n)
21
+ expect(metadata.item_type).to eq ""
22
+ expect(metadata.style_type).to eq ""
23
+ expect(metadata.item_uid).to eq ""
24
+ expect(metadata.content_type_uid).to eq ""
25
+ expect(metadata.text).to eq 'TEST'
26
+ end
27
+ end
28
+
29
+ it 'Attributes' do
30
+ characters = getElement('<h1 type="asset" sys-style-type="inline" data-sys-entry-uid="uid" data-sys-content-type-uid="contentType">
31
+ TEST</h1>', "//h1")
32
+ characters.each do |n|
33
+ metadata = ContentstackUtils::Model::Metadata.new(n)
34
+ expect(metadata.item_type).to eq "asset"
35
+ expect(metadata.style_type).to eq "inline"
36
+ expect(metadata.item_uid).to eq "uid"
37
+ expect(metadata.content_type_uid).to eq "contentType"
38
+ expect(metadata.text).to eq '
39
+ TEST'
40
+ end
41
+ end
42
+
43
+ it 'Asset Uid Attributes' do
44
+ characters = getElement('<h1 type="asset" sys-style-type="inline" data-sys-asset-uid="assetuid">TEST</h1>', "//h1")
45
+ characters.each do |n|
46
+ metadata = ContentstackUtils::Model::Metadata.new(n)
47
+ expect(metadata.item_type).to eq "asset"
48
+ expect(metadata.style_type).to eq "inline"
49
+ expect(metadata.item_uid).to eq "assetuid"
50
+ expect(metadata.content_type_uid).to eq nil
51
+ expect(metadata.text).to eq 'TEST'
52
+ end
53
+ end
54
+
55
+ it 'Asset Json To metadata' do
56
+ doc = getJson(AssetReferenceJson)
57
+ metadata = ContentstackUtils::Model::Metadata.new(doc["children"][0])
58
+ expect(metadata.item_type).to eq "asset"
59
+ expect(metadata.style_type).to eq "display"
60
+ expect(metadata.item_uid).to eq "asset_uid_1"
61
+ expect(metadata.content_type_uid).to eq 'sys_assets'
62
+ expect(metadata.text).to eq ''
63
+ end
64
+
65
+ it 'Entry Block Json To metadata' do
66
+ doc = getJson(EntryReferenceBlockJson)
67
+ metadata = ContentstackUtils::Model::Metadata.new(doc["children"][0])
68
+ expect(metadata.item_type).to eq "entry"
69
+ expect(metadata.style_type).to eq "block"
70
+ expect(metadata.item_uid).to eq "entry_uid_1"
71
+ expect(metadata.content_type_uid).to eq 'content_block'
72
+ expect(metadata.text).to eq ''
73
+ end
74
+
75
+ it 'Entry Link Json To metadata' do
76
+ doc = getJson(EntryReferenceLinkJson)
77
+ metadata = ContentstackUtils::Model::Metadata.new(doc["children"][0])
78
+ expect(metadata.item_type).to eq "entry"
79
+ expect(metadata.style_type).to eq "link"
80
+ expect(metadata.item_uid).to eq "entry_uid_2"
81
+ expect(metadata.content_type_uid).to eq 'embeddedrte'
82
+ expect(metadata.text).to eq "/copy-of-entry-final-02"
83
+ end
84
+
85
+ it 'Entry Inline Json To metadata' do
86
+ doc = getJson(EntryReferenceInlineJson)
87
+ metadata = ContentstackUtils::Model::Metadata.new(doc["children"][0])
88
+ expect(metadata.item_type).to eq "entry"
89
+ expect(metadata.style_type).to eq "inline"
90
+ expect(metadata.item_uid).to eq "entry_uid_3"
91
+ expect(metadata.content_type_uid).to eq 'embeddedrte'
92
+ expect(metadata.text).to eq ''
93
+ end
94
+ end
95
95
  end