Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
contentstack-*
contentstack_utils-*
build_doc.sh
test
doc
Expand All @@ -7,4 +7,4 @@ coverage
\.yardoc
.DS_Store
.bundle/
*/rspec_results.html
**/rspec_results.html
47 changes: 36 additions & 11 deletions lib/contentstack_utils/model/metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,46 @@ module ContentstackUtils
module Model
class Metadata
def initialize( element )
@itemType = element.attribute('type')
@styleType = element.attribute('sys-style-type')
@itemUid ||= element.attribute('data-sys-entry-uid') || element.attribute('data-sys-asset-uid')
@contentTypeUid = element.attribute('data-sys-content-type-uid')
@text = element.text
@element = element
@attributes = element.attributes
if element.instance_of? Nokogiri::XML::Element
@itemType = element.attribute('type') ? element.attribute('type').value : nil
@styleType = element.attribute('sys-style-type') ? element.attribute('sys-style-type').value : nil
@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)
@contentTypeUid = element.attribute('data-sys-content-type-uid') ? element.attribute('data-sys-content-type-uid').value : nil
@text = element.text
@element = element
@attributes = element
else
@itemType = element["attrs"]['type']
@styleType = element["attrs"]['display-type']
@itemUid ||= element["attrs"]['entry-uid'] || element["attrs"]['asset-uid']
@contentTypeUid = element["attrs"]['content-type-uid']
if element["children"] && element["children"].length() > 0
child = element["children"]
for item in child do
if item["type"] == nil && item["text"]
@text = item["text"]
end
end
end
@element = element
@attributes = element["attrs"]
end
end

def item_type
@itemType ? @itemType.value : nil
@itemType ? @itemType : nil
end

def style_type
@styleType ? @styleType.value : nil
@styleType ? @styleType : nil
end

def item_uid
@itemUid ? @itemUid.value : nil
@itemUid ? @itemUid : nil
end

def content_type_uid
@contentTypeUid ? @contentTypeUid.value : nil
@contentTypeUid ? @contentTypeUid : nil
end

def text
Expand All @@ -39,6 +56,14 @@ def element
def attributes
@attributes
end

def get_attribute_value(string)
if @attributes.instance_of? Nokogiri::XML::Element
@attributes.attribute(string) ? @attributes.attribute(string).value : nil
else
@attributes[string]
end
end
end
end
end
87 changes: 83 additions & 4 deletions lib/contentstack_utils/model/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module ContentstackUtils
module Model
class Options < Interface::Rendarable

def initialize(entry)
def initialize(entry = nil)
@entry = entry
end

Expand All @@ -21,11 +21,90 @@ def render_option(embeddedObject, metadata)
when 'inline'
renderString = "<span>#{embeddedObject["title"] || embeddedObject["uid"]}</span>";
when 'link'
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>";
metadata.get_attribute_value("href")
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>";
when 'display'
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"]))}' />";
renderString = "<img src='#{metadata.get_attribute_value("src")|| embeddedObject["url"]}' alt='#{(metadata.attributes["alt"] ? metadata.attributes["alt"].value : (embeddedObject["title"] || embeddedObject["filename"] || embeddedObject["uid"]))}' />";
when 'download'
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>";
renderString = "<a href='#{metadata.get_attribute_value("href") || embeddedObject["url"]}'>#{(metadata.text && metadata.text != '' ? metadata.text : (embeddedObject["filename"] || embeddedObject["title"] || embeddedObject["uid"]))}</a>";
end
renderString
end

def render_mark(mark_type, text)
renderString = text
case mark_type
when 'bold'
renderString = "<strong>#{text}</strong>"
when 'italic'
renderString = "<em>#{text}</em>"
when 'underline'
renderString = "<u>#{text}</u>"
when 'strikethrough'
renderString = "<strike>#{text}</strike>"
when 'inlineCode'
renderString = "<span>#{text}</span>"
when 'subscript'
renderString = "<sub>#{text}</sub>"
when 'superscript'
renderString = "<sup>#{text}</sup>"
end
renderString
end

def render_node(node_type, node, inner_html)
renderString = ""
case node_type
when 'doc'
renderString = ""
when 'p'
renderString = "<p>#{inner_html}</p>"
when 'a'
renderString = "<a href='#{node["attrs"]["href"] || ""}'>#{inner_html}</a>"
when 'img'
renderString = "<img src='#{node["attrs"]["src"] || ""}' />#{inner_html}"
when 'embed'
renderString = "<iframe src='#{node["attrs"]["src"] || ""}'></iframe>"
when 'h1'
renderString = "<h1>#{inner_html}</h1>"
when 'h2'
renderString = "<h2>#{inner_html}</h2>"
when 'h3'
renderString = "<h3>#{inner_html}</h3>"
when 'h4'
renderString = "<h4>#{inner_html}</h4>"
when 'h5'
renderString = "<h5>#{inner_html}</h5>"
when 'h6'
renderString = "<h6>#{inner_html}</h6>"
when 'ol'
renderString = "<ol>#{inner_html}</ol>"
when 'ul'
renderString = "<ul>#{inner_html}</ul>"
when 'li'
renderString = "<li>#{inner_html}</li>"
when 'hr'
renderString = "<hr />"
when 'table'
renderString = "<table>#{inner_html}</table>"
when 'thead'
renderString = "<thead>#{inner_html}</thead>"
when 'tbody'
renderString = "<tbody>#{inner_html}</tbody>"
when 'tfoot'
renderString = "<tfoot>#{inner_html}</tfoot>"
when 'tr'
renderString = "<tr>#{inner_html}</tr>"
when 'th'
renderString = "<th>#{inner_html}</th>"
when 'td'
renderString = "<td>#{inner_html}</td>"
when 'blockquote'
renderString = "<blockquote>#{inner_html}</blockquote>"
when 'code'
renderString = "<code>#{inner_html}</code>"
when 'reference'
renderString = ""
end
renderString
end
Expand Down
77 changes: 77 additions & 0 deletions lib/contentstack_utils/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,83 @@ def self.render_content(content, options)
end
end

def self.json_to_html(content, options)
if (content.instance_of? Array)
result = []
content.each do |n|
result.push(json_doc_to_html(n, options))
end
result
elsif content.instance_of? Hash
json_doc_to_html(content, options)
end
end

private_class_method def self.json_doc_to_html(node, options)
result = ""
if node["children"] && node["children"].length() > 0
result = node_children_to_html(node["children"], options)
end
result
end

private_class_method def self.node_children_to_html(nodes, options)
nodes.map {|node| node_to_html(node, options)}.join("")
end

private_class_method def self.node_to_html(node, options)
html_result = ""
if node["type"] == nil && node["text"]
html_result = text_to_htms(node, options)
elsif node["type"]
if node["type"] == "reference"
html_result = reference_to_html(node, options)
else
inner_html = json_doc_to_html(node, options)
html_result = options.render_node(node["type"], node, inner_html)
end
end
html_result
end

private_class_method def self.text_to_htms(node, options)
text = node["text"]
if node["superscript"]
text = options.render_mark("superscript", text)
end
if node["subscript"]
text = options.render_mark("subscript", text)
end
if node["inlineCode"]
text = options.render_mark("inlineCode", text)
end
if node["strikethrough"]
text = options.render_mark("strikethrough", text)
end
if node["underline"]
text = options.render_mark("underline", text)
end
if node["italic"]
text = options.render_mark("italic", text)
end
if node["bold"]
text = options.render_mark("bold", text)
end
text
end

private_class_method def self.reference_to_html(node, options)
result = ""
if options.entry != nil
metadata = Model::Metadata.new(node)
object = findObject(metadata, options.entry)
if object!= nil && object.length() > 0
result = options.render_option(object[0], metadata)
end
end
result
end

private_class_method def self.render_string(string, options)
xml_doc = Nokogiri::HTML(appendFrame(string))
result = xml_doc.xpath('//documentfragmentcontainer').inner_html
Expand Down
40 changes: 40 additions & 0 deletions spec/lib/model/metadata_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,45 @@
expect(metadata.text).to eq 'TEST'
end
end

it 'Asset Json To metadata' do
doc = getJson(AssetReferenceJson)
metadata = ContentstackUtils::Model::Metadata.new(doc["children"][0])
expect(metadata.item_type).to eq "asset"
expect(metadata.style_type).to eq "display"
expect(metadata.item_uid).to eq "blt44asset"
expect(metadata.content_type_uid).to eq 'sys_assets'
expect(metadata.text).to eq ''
end

it 'Entry Block Json To metadata' do
doc = getJson(EntryReferenceBlockJson)
metadata = ContentstackUtils::Model::Metadata.new(doc["children"][0])
expect(metadata.item_type).to eq "entry"
expect(metadata.style_type).to eq "block"
expect(metadata.item_uid).to eq "blttitleuid"
expect(metadata.content_type_uid).to eq 'content_block'
expect(metadata.text).to eq ''
end

it 'Entry Link Json To metadata' do
doc = getJson(EntryReferenceLinkJson)
metadata = ContentstackUtils::Model::Metadata.new(doc["children"][0])
expect(metadata.item_type).to eq "entry"
expect(metadata.style_type).to eq "link"
expect(metadata.item_uid).to eq "bltemmbedEntryuid"
expect(metadata.content_type_uid).to eq 'embeddedrte'
expect(metadata.text).to eq "/copy-of-entry-final-02"
end

it 'Entry Inline Json To metadata' do
doc = getJson(EntryReferenceInlineJson)
metadata = ContentstackUtils::Model::Metadata.new(doc["children"][0])
expect(metadata.item_type).to eq "entry"
expect(metadata.style_type).to eq "inline"
expect(metadata.item_uid).to eq "blttitleUpdateuid"
expect(metadata.content_type_uid).to eq 'embeddedrte'
expect(metadata.text).to eq ''
end
end
end
Loading