From 38c0064787da57eb380d6b3ae43c1eead4771713 Mon Sep 17 00:00:00 2001 From: Uttam Krishna Ukkoji Date: Wed, 23 Jun 2021 13:33:11 +0530 Subject: [PATCH] :tada: :sparkles: Json to Html feature added --- .gitignore | 4 +- lib/contentstack_utils/model/metadata.rb | 47 +++- lib/contentstack_utils/model/options.rb | 87 +++++- lib/contentstack_utils/utils.rb | 77 +++++ spec/lib/model/metadata_spec.rb | 40 +++ spec/lib/model/option_spec.rb | 216 ++++++++++++++ spec/lib/utils_spec.rb | 344 +++++++++++++++++++++++ spec/mock/json_to_html_mock.rb | 145 ++++++++++ spec/support/xml_parse.rb | 4 + 9 files changed, 947 insertions(+), 17 deletions(-) create mode 100644 spec/mock/json_to_html_mock.rb diff --git a/.gitignore b/.gitignore index 1cbd975..14f42c2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -contentstack-* +contentstack_utils-* build_doc.sh test doc @@ -7,4 +7,4 @@ coverage \.yardoc .DS_Store .bundle/ -*/rspec_results.html \ No newline at end of file +**/rspec_results.html \ No newline at end of file diff --git a/lib/contentstack_utils/model/metadata.rb b/lib/contentstack_utils/model/metadata.rb index 79882b9..2412082 100644 --- a/lib/contentstack_utils/model/metadata.rb +++ b/lib/contentstack_utils/model/metadata.rb @@ -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 @@ -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 \ No newline at end of file diff --git a/lib/contentstack_utils/model/options.rb b/lib/contentstack_utils/model/options.rb index 857057c..0c6f818 100644 --- a/lib/contentstack_utils/model/options.rb +++ b/lib/contentstack_utils/model/options.rb @@ -5,7 +5,7 @@ module ContentstackUtils module Model class Options < Interface::Rendarable - def initialize(entry) + def initialize(entry = nil) @entry = entry end @@ -21,11 +21,90 @@ def render_option(embeddedObject, metadata) when 'inline' renderString = "#{embeddedObject["title"] || embeddedObject["uid"]}"; when 'link' - renderString = "#{(metadata.text && metadata.text != '' ? metadata.text : (embeddedObject["title"] || embeddedObject["uid"]))}"; + metadata.get_attribute_value("href") + renderString = "#{(metadata.text && metadata.text != '' ? metadata.text : (embeddedObject["title"] || embeddedObject["uid"]))}"; when 'display' - renderString = "#{(metadata.attributes["alt"] ? metadata.attributes["alt"].value : (embeddedObject["title"] || embeddedObject["filename"] || embeddedObject["uid"]))}"; + renderString = "#{(metadata.attributes["alt"] ? metadata.attributes["alt"].value : (embeddedObject["title"] || embeddedObject["filename"] || embeddedObject["uid"]))}"; when 'download' - renderString = "#{(metadata.text && metadata.text != '' ? metadata.text : (embeddedObject["filename"] || embeddedObject["title"] || embeddedObject["uid"]))}"; + renderString = "#{(metadata.text && metadata.text != '' ? metadata.text : (embeddedObject["filename"] || embeddedObject["title"] || embeddedObject["uid"]))}"; + end + renderString + end + + def render_mark(mark_type, text) + renderString = text + case mark_type + when 'bold' + renderString = "#{text}" + when 'italic' + renderString = "#{text}" + when 'underline' + renderString = "#{text}" + when 'strikethrough' + renderString = "#{text}" + when 'inlineCode' + renderString = "#{text}" + when 'subscript' + renderString = "#{text}" + when 'superscript' + renderString = "#{text}" + end + renderString + end + + def render_node(node_type, node, inner_html) + renderString = "" + case node_type + when 'doc' + renderString = "" + when 'p' + renderString = "

#{inner_html}

" + when 'a' + renderString = "#{inner_html}" + when 'img' + renderString = "#{inner_html}" + when 'embed' + renderString = "" + when 'h1' + renderString = "

#{inner_html}

" + when 'h2' + renderString = "

#{inner_html}

" + when 'h3' + renderString = "

#{inner_html}

" + when 'h4' + renderString = "

#{inner_html}

" + when 'h5' + renderString = "
#{inner_html}
" + when 'h6' + renderString = "
#{inner_html}
" + when 'ol' + renderString = "
    #{inner_html}
" + when 'ul' + renderString = "" + when 'li' + renderString = "
  • #{inner_html}
  • " + when 'hr' + renderString = "
    " + when 'table' + renderString = "#{inner_html}
    " + when 'thead' + renderString = "#{inner_html}" + when 'tbody' + renderString = "#{inner_html}" + when 'tfoot' + renderString = "#{inner_html}" + when 'tr' + renderString = "#{inner_html}" + when 'th' + renderString = "#{inner_html}" + when 'td' + renderString = "#{inner_html}" + when 'blockquote' + renderString = "
    #{inner_html}
    " + when 'code' + renderString = "#{inner_html}" + when 'reference' + renderString = "" end renderString end diff --git a/lib/contentstack_utils/utils.rb b/lib/contentstack_utils/utils.rb index 5e745bb..5b103fc 100644 --- a/lib/contentstack_utils/utils.rb +++ b/lib/contentstack_utils/utils.rb @@ -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 diff --git a/spec/lib/model/metadata_spec.rb b/spec/lib/model/metadata_spec.rb index ac9698a..6c4aa65 100644 --- a/spec/lib/model/metadata_spec.rb +++ b/spec/lib/model/metadata_spec.rb @@ -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 \ No newline at end of file diff --git a/spec/lib/model/option_spec.rb b/spec/lib/model/option_spec.rb index db6e828..20b2807 100644 --- a/spec/lib/model/option_spec.rb +++ b/spec/lib/model/option_spec.rb @@ -82,5 +82,221 @@ def getMetadata(itemType=nil, styleType=nil, linkText=nil) expect(subject.render_option(ASSET_CONTENT_URL, getMetadata('entry', 'download', linkText))). to eq "#{linkText}" end + + it 'Should return Mark text html' do + expect(subject.render_mark('bold', linkText)). + to eq "#{linkText}" + expect(subject.render_mark('italic', linkText)). + to eq "#{linkText}" + expect(subject.render_mark('underline', linkText)). + to eq "#{linkText}" + expect(subject.render_mark('strikethrough', linkText)). + to eq "#{linkText}" + expect(subject.render_mark('inlineCode', linkText)). + to eq "#{linkText}" + expect(subject.render_mark('subscript', linkText)). + to eq "#{linkText}" + expect(subject.render_mark('superscript', linkText)). + to eq "#{linkText}" + expect(subject.render_mark('', linkText)). + to eq linkText + end + + it 'Should return blank string for doc node type' do + doc = getJson(BlankDocument) + + result = subject.render_node('doc', doc, linkText) + expect(result).to eq "" + end + + it 'Should return paragraph string for paragrpah node type' do + doc = getJson(BlankDocument) + + result = subject.render_node('p', doc, linkText) + expect(result).to eq "

    #{linkText}

    " + end + + it 'Should return link string with blank href for link node type' do + doc = getJson(BlankDocument) + + result = subject.render_node('a', doc, linkText) + expect(result).to eq "#{linkText}" + end + + it 'Should return link string for link node type' do + doc = getJson(LinkInPJson)["children"][0] + + result = subject.render_node('a', doc, linkText) + expect(result).to eq "#{linkText}" + end + + it 'Should return image string with blank src for image node type' do + doc = getJson(BlankDocument) + + result = subject.render_node('img', doc, linkText) + expect(result).to eq "#{linkText}" + end + + it 'Should return link string for link node type' do + doc = getJson(ImgJson)["children"][0] + + result = subject.render_node('img', doc, linkText) + expect(result).to eq "#{linkText}" + end + + it 'Should return embed string with blank src for embed node type' do + doc = getJson(BlankDocument) + + result = subject.render_node('embed', doc, linkText) + expect(result).to eq "" + end + + it 'Should return embed string for embed node type' do + doc = getJson(EmbedJson)["children"][0] + + result = subject.render_node('embed', doc, linkText) + expect(result).to eq "" + end + + it 'Should return Heading 1 string for Heading 1 node type' do + doc = getJson(BlankDocument) + + result = subject.render_node('h1', doc, linkText) + expect(result).to eq "

    #{linkText}

    " + end + + it 'Should return Heading 2 string for Heading 2 node type' do + doc = getJson(BlankDocument) + + result = subject.render_node('h2', doc, linkText) + expect(result).to eq "

    #{linkText}

    " + end + + it 'Should return Heading 3 string for Heading 3 node type' do + doc = getJson(BlankDocument) + + result = subject.render_node('h3', doc, linkText) + expect(result).to eq "

    #{linkText}

    " + end + + it 'Should return Heading 4 string for Heading 4 node type' do + doc = getJson(BlankDocument) + + result = subject.render_node('h4', doc, linkText) + expect(result).to eq "

    #{linkText}

    " + end + + it 'Should return Heading 5 string for Heading 5 node type' do + doc = getJson(BlankDocument) + + result = subject.render_node('h5', doc, linkText) + expect(result).to eq "
    #{linkText}
    " + end + + it 'Should return Heading 6 string for Heading 6 node type' do + doc = getJson(BlankDocument) + + result = subject.render_node('h6', doc, linkText) + expect(result).to eq "
    #{linkText}
    " + end + + it 'Should return Hr string for Hr node type' do + doc = getJson(BlankDocument) + + result = subject.render_node('hr', doc, linkText) + expect(result).to eq "
    " + end + + it 'Should return order list string for order list node type' do + doc = getJson(BlankDocument) + + result = subject.render_node('ol', doc, linkText) + expect(result).to eq "
      #{linkText}
    " + end + + it 'Should return Unorder list string for Unorder list node type' do + doc = getJson(BlankDocument) + + result = subject.render_node('ul', doc, linkText) + expect(result).to eq "" + end + + it 'Should return list item string for list item node type' do + doc = getJson(BlankDocument) + + result = subject.render_node('li', doc, linkText) + expect(result).to eq "
  • #{linkText}
  • " + end + + it 'Should return table string for table node type' do + doc = getJson(BlankDocument) + + result = subject.render_node('table', doc, linkText) + expect(result).to eq "#{linkText}
    " + end + + it 'Should return thead string for thead node type' do + doc = getJson(BlankDocument) + + result = subject.render_node('thead', doc, linkText) + expect(result).to eq "#{linkText}" + end + + it 'Should return tfoot string for tfoot node type' do + doc = getJson(BlankDocument) + + result = subject.render_node('tfoot', doc, linkText) + expect(result).to eq "#{linkText}" + end + + it 'Should return tbody string fortbody node type' do + doc = getJson(BlankDocument) + + result = subject.render_node('tbody', doc, linkText) + expect(result).to eq "#{linkText}" + end + + it 'Should return table row string for table row node type' do + doc = getJson(BlankDocument) + + result = subject.render_node('tr', doc, linkText) + expect(result).to eq "#{linkText}" + end + + it 'Should return table head string for table head node type' do + doc = getJson(BlankDocument) + + result = subject.render_node('th', doc, linkText) + expect(result).to eq "#{linkText}" + end + + it 'Should return table data string for table data node type' do + doc = getJson(BlankDocument) + + result = subject.render_node('td', doc, linkText) + expect(result).to eq "#{linkText}" + end + + it 'Should return blockquote string for blockquote node type' do + doc = getJson(BlankDocument) + + result = subject.render_node('blockquote', doc, linkText) + expect(result).to eq "
    #{linkText}
    " + end + + it 'Should return code string for code node type' do + doc = getJson(BlankDocument) + + result = subject.render_node('code', doc, linkText) + expect(result).to eq "#{linkText}" + end + + it 'Should return blank string for reference node type' do + doc = getJson(BlankDocument) + + result = subject.render_node('reference', doc, linkText) + expect(result).to eq "" + end + end end \ No newline at end of file diff --git a/spec/lib/utils_spec.rb b/spec/lib/utils_spec.rb index 2966e9a..2fcb3b1 100644 --- a/spec/lib/utils_spec.rb +++ b/spec/lib/utils_spec.rb @@ -59,6 +59,350 @@ end end + + describe '#JsonToHtml Array' do + it 'Should return blank string for blank doc' do + doc = getJson(BlankDocument) + + result = ContentstackUtils.json_to_html([doc], ContentstackUtils::Model::Options.new()) + + expect(result).to eq [""] + end + + it 'Should return mark text string for PlainTextJson doc' do + doc = getJson(PlainTextJson) + + result = ContentstackUtils.json_to_html([doc], ContentstackUtils::Model::Options.new()) + + expect(result).to eq [PlainTextHtml] + end + + it 'Should return paragraph string for ParagraphJson doc' do + doc = getJson(ParagraphJson) + + result = ContentstackUtils.json_to_html([doc], ContentstackUtils::Model::Options.new()) + + expect(result).to eq [ParagraphHtml] + end + + it 'Should return H1 string for H1Json doc' do + doc = getJson(H1Json) + + result = ContentstackUtils.json_to_html([doc], ContentstackUtils::Model::Options.new()) + + expect(result).to eq [H1Html] + end + + it 'Should return H2 string for H2Json doc' do + doc = getJson(H2Json) + + result = ContentstackUtils.json_to_html([doc], ContentstackUtils::Model::Options.new()) + + expect(result).to eq [H2Html] + end + + it 'Should return H3 string for H3Json doc' do + doc = getJson(H3Json) + + result = ContentstackUtils.json_to_html([doc], ContentstackUtils::Model::Options.new()) + + expect(result).to eq [H3Html] + end + + it 'Should return H4 string for H4Json doc' do + doc = getJson(H4Json) + + result = ContentstackUtils.json_to_html([doc], ContentstackUtils::Model::Options.new()) + + expect(result).to eq [H4Html] + end + + it 'Should return H5 string for H5Json doc' do + doc = getJson(H5Json) + + result = ContentstackUtils.json_to_html([doc], ContentstackUtils::Model::Options.new()) + + expect(result).to eq [H5Html] + end + + it 'Should return H6 string for H6Json doc' do + doc = getJson(H6Json) + + result = ContentstackUtils.json_to_html([doc], ContentstackUtils::Model::Options.new()) + + expect(result).to eq [H6Html] + end + + it 'Should return Order List string for OrderListJson doc' do + doc = getJson(OrderListJson) + + result = ContentstackUtils.json_to_html([doc], ContentstackUtils::Model::Options.new()) + + expect(result).to eq [OrderListHtml] + end + + it 'Should return Unorder List string for UnorderListJson doc' do + doc = getJson(UnorderListJson) + + result = ContentstackUtils.json_to_html([doc], ContentstackUtils::Model::Options.new()) + + expect(result).to eq [UnorderListHtml] + end + + it 'Should return image string for ImgJson doc' do + doc = getJson(ImgJson) + + result = ContentstackUtils.json_to_html([doc], ContentstackUtils::Model::Options.new()) + + expect(result).to eq [ImgHtml] + end + + it 'Should return Blockquote string for BlockquoteJson doc' do + doc = getJson(BlockquoteJson) + + result = ContentstackUtils.json_to_html([doc], ContentstackUtils::Model::Options.new()) + + expect(result).to eq [BlockquoteHtml] + end + + it 'Should return Code string for CodeJson doc' do + doc = getJson(CodeJson) + + result = ContentstackUtils.json_to_html([doc], ContentstackUtils::Model::Options.new()) + + expect(result).to eq [CodeHtml] + end + + it 'Should return Table string for TableJson doc' do + doc = getJson(TableJson) + + result = ContentstackUtils.json_to_html([doc], ContentstackUtils::Model::Options.new()) + + expect(result).to eq [TableHtml] + end + + it 'Should return Link string for LinkInPJson doc' do + doc = getJson(LinkInPJson) + + result = ContentstackUtils.json_to_html([doc], ContentstackUtils::Model::Options.new()) + + expect(result).to eq [LinkInPHtml] + end + + it 'Should return Embed string for EmbedJson doc' do + doc = getJson(EmbedJson) + + result = ContentstackUtils.json_to_html([doc], ContentstackUtils::Model::Options.new()) + + expect(result).to eq [EmbedHtml] + end + + it 'Should return horizontal line string for horizontal line doc' do + doc = getJson(HRJson) + + result = ContentstackUtils.json_to_html([doc], ContentstackUtils::Model::Options.new()) + + expect(result).to eq ['
    '] + end + end + + describe '#JsonToHtml' do + it 'Should return blank string for blank doc' do + doc = getJson(BlankDocument) + + result = ContentstackUtils.json_to_html(doc, ContentstackUtils::Model::Options.new()) + + expect(result).to eq "" + end + + it 'Should return mark text string for PlainTextJson doc' do + doc = getJson(PlainTextJson) + + result = ContentstackUtils.json_to_html(doc, ContentstackUtils::Model::Options.new()) + + expect(result).to eq PlainTextHtml + end + + it 'Should return paragraph string for ParagraphJson doc' do + doc = getJson(ParagraphJson) + + result = ContentstackUtils.json_to_html(doc, ContentstackUtils::Model::Options.new()) + + expect(result).to eq ParagraphHtml + end + + it 'Should return H1 string for H1Json doc' do + doc = getJson(H1Json) + + result = ContentstackUtils.json_to_html(doc, ContentstackUtils::Model::Options.new()) + + expect(result).to eq H1Html + end + + it 'Should return H2 string for H2Json doc' do + doc = getJson(H2Json) + + result = ContentstackUtils.json_to_html(doc, ContentstackUtils::Model::Options.new()) + + expect(result).to eq H2Html + end + + it 'Should return H3 string for H3Json doc' do + doc = getJson(H3Json) + + result = ContentstackUtils.json_to_html(doc, ContentstackUtils::Model::Options.new()) + + expect(result).to eq H3Html + end + + it 'Should return H4 string for H4Json doc' do + doc = getJson(H4Json) + + result = ContentstackUtils.json_to_html(doc, ContentstackUtils::Model::Options.new()) + + expect(result).to eq H4Html + end + + it 'Should return H5 string for H5Json doc' do + doc = getJson(H5Json) + + result = ContentstackUtils.json_to_html(doc, ContentstackUtils::Model::Options.new()) + + expect(result).to eq H5Html + end + + it 'Should return H6 string for H6Json doc' do + doc = getJson(H6Json) + + result = ContentstackUtils.json_to_html(doc, ContentstackUtils::Model::Options.new()) + + expect(result).to eq H6Html + end + + it 'Should return Order List string for OrderListJson doc' do + doc = getJson(OrderListJson) + + result = ContentstackUtils.json_to_html(doc, ContentstackUtils::Model::Options.new()) + + expect(result).to eq OrderListHtml + end + + it 'Should return Unorder List string for UnorderListJson doc' do + doc = getJson(UnorderListJson) + + result = ContentstackUtils.json_to_html(doc, ContentstackUtils::Model::Options.new()) + + expect(result).to eq UnorderListHtml + end + + it 'Should return image string for ImgJson doc' do + doc = getJson(ImgJson) + + result = ContentstackUtils.json_to_html(doc, ContentstackUtils::Model::Options.new()) + + expect(result).to eq ImgHtml + end + + it 'Should return Blockquote string for BlockquoteJson doc' do + doc = getJson(BlockquoteJson) + + result = ContentstackUtils.json_to_html(doc, ContentstackUtils::Model::Options.new()) + + expect(result).to eq BlockquoteHtml + end + + it 'Should return Code string for CodeJson doc' do + doc = getJson(CodeJson) + + result = ContentstackUtils.json_to_html(doc, ContentstackUtils::Model::Options.new()) + + expect(result).to eq CodeHtml + end + + it 'Should return Table string for TableJson doc' do + doc = getJson(TableJson) + + result = ContentstackUtils.json_to_html(doc, ContentstackUtils::Model::Options.new()) + + expect(result).to eq TableHtml + end + + it 'Should return Link string for LinkInPJson doc' do + doc = getJson(LinkInPJson) + + result = ContentstackUtils.json_to_html(doc, ContentstackUtils::Model::Options.new()) + + expect(result).to eq LinkInPHtml + end + + it 'Should return Embed string for EmbedJson doc' do + doc = getJson(EmbedJson) + + result = ContentstackUtils.json_to_html(doc, ContentstackUtils::Model::Options.new()) + + expect(result).to eq EmbedHtml + end + + it 'Should return horizontal line string for horizontal line doc' do + doc = getJson(HRJson) + + result = ContentstackUtils.json_to_html(doc, ContentstackUtils::Model::Options.new()) + + expect(result).to eq '
    ' + end + + it 'Should return horizontal line string for horizontal line doc' do + doc = getJson(H1NonChildJson) + + result = ContentstackUtils.json_to_html(doc, ContentstackUtils::Model::Options.new()) + + expect(result).to eq '

    ' + end + end + + describe '#JsonToHtml reference' do + it 'Should return blank string for non entry option' do + doc = getJson(AssetReferenceJson) + + result = ContentstackUtils.json_to_html(doc, ContentstackUtils::Model::Options.new()) + + expect(result).to eq '' + end + + it 'Should return asset embedded items' do + doc = getJson(AssetReferenceJson) + + result = ContentstackUtils.json_to_html(doc, ContentstackUtils::Model::Options.new(JSON_EMBEDDED_ITEMS_ENTRY)) + + expect(result).to eq AssetReferenceHtml + end + + it 'Should return entry block embedded items' do + doc = getJson(EntryReferenceBlockJson) + + result = ContentstackUtils.json_to_html(doc, ContentstackUtils::Model::Options.new(JSON_EMBEDDED_ITEMS_ENTRY)) + + expect(result).to eq EntryReferenceBlockHtml + end + + it 'Should return entry link embedded items' do + doc = getJson(EntryReferenceLinkJson) + + result = ContentstackUtils.json_to_html(doc, ContentstackUtils::Model::Options.new(JSON_EMBEDDED_ITEMS_ENTRY)) + + expect(result).to eq EntryReferenceLinkHtml + end + + it 'Should return entry inline embedded items' do + doc = getJson(EntryReferenceInlineJson) + + result = ContentstackUtils.json_to_html(doc, ContentstackUtils::Model::Options.new(JSON_EMBEDDED_ITEMS_ENTRY)) + + expect(result).to eq EntryReferenceInlineHtml + end + + end + def makeRenderFunction(content, option = ContentstackUtils::Model::Options.new(ENTRY_EMBEDDED_ASSET)) ContentstackUtils.render_content(content, option) end diff --git a/spec/mock/json_to_html_mock.rb b/spec/mock/json_to_html_mock.rb new file mode 100644 index 0000000..d3a63a8 --- /dev/null +++ b/spec/mock/json_to_html_mock.rb @@ -0,0 +1,145 @@ + +PlainTextHtml = "Aliquam sit amet libero dapibus, eleifend ligula at, varius justoLorem ipsumdolor sit ametconsectetur adipiscing elit.Sed condimentum iaculis magna in vehicula. Vestibulum vitae convallis lacus. " +ParagraphHtml = "

    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed condimentum iaculis magna in vehicula. Vestibulum vitae convallis lacus. Praesent a diam iaculis turpis rhoncus faucibus. Aliquam sed pulvinar sem.

    " +H1Html = "

    Lorem ipsum dolor sit amet.

    " +H2Html = "

    Vestibulum a ligula eget massa sagittis aliquam sit amet quis tortor.

    " +H3Html = "

    Mauris venenatis dui id massa sollicitudin, non bibendum nunc dictum.

    " +H4Html = "

    MaNullam feugiat turpis quis elit interdum, vitae laoreet quam viverra

    " +H5Html = "
    Mauris venenatis dui id massa sollicitudin, non bibendum nunc dictum.
    " +H6Html = "
    Nunc porta diam vitae purus semper, ut consequat lorem vehicula.
    " +OrderListHtml = "
    1. Morbi in quam molestie, fermentum diam vitae, bibendum ipsum.
    2. Pellentesque mattis lacus in quam aliquam congue
    3. Integer feugiat leo dignissim, lobortis enim vitae, mollis lectus.
    4. Sed in ante lacinia, molestie metus eu, fringilla sapien.
    " +UnorderListHtml = "" +ImgHtml = "" +TableHtml = "

    Header 1

    Header 2

    Body row 1 data 1

    Body row 1 data 2

    Body row 2 data 1

    Body row 2 data 2

    " +BlockquoteHtml = "
    Praesent eu ex sed nibh venenatis pretium.
    " +CodeHtml = "Code template." +LinkInPHtml = "

    LINK

    " +EmbedHtml = "" +AssetReferenceHtml = "dummy.pdf" +EntryReferenceBlockHtml = "

    Update this title

    Content type: content_block

    " +EntryReferenceLinkHtml = "/copy-of-entry-final-02" +EntryReferenceInlineHtml = "updated title" + +BlankDocument = '{ "uid":"06e34a7a4e5d7fc2acd", "_version":13, "attrs":{ }, "children":[],"type":"doc"}' +PlainTextJson = '{ "uid":"06e34a7a4e5d7fc2acd", "_version":13, "attrs":{ }, "children":[{"text":"Aliquam sit amet libero dapibus, eleifend ligula at, varius justo","bold":true},{ "text":"Lorem ipsum","bold":true,"italic":true},{ "text":"dolor sit amet","bold":true,"italic":true,"underline":true},{ "text":"consectetur adipiscing elit.","bold":true,"italic":true,"underline":true,"strikethrough":true},{ "text":"Sed condimentum iaculis magna in vehicula. ","bold":true,"italic":true,"underline":true,"inlineCode":true},{ "text":"Vestibulum vitae convallis ","bold":true,"italic":true,"underline":true,"superscript":true},{ "text":" lacus. ","bold":true,"italic":true,"underline":true,"subscript":true}],"type":"doc"}' +H1Json = '{ "uid":"06e34a7a449d7fc2acd","_version":13,"attrs":{ },"children":[{ "type":"h1","attrs":{ },"uid":"c2dfed70 4d7030c65e2e1","children":[{ "text":"Lorem ipsum dolor sit amet.","bold":true,"italic":true,"underline":true,"subscript":true}]}],"type":"doc"}' +H2Json = '{ "uid":"06e34a7a4e2acd","_version":13,"attrs":{ },"children":[{ "type":"h2","attrs":{ },"uid":"c2dfed9a7030c65e2e1","children":[{ "text":"Vestibulum a ligula eget massa sagittis aliquam sit amet quis tortor. ","bold":true,"italic":true,"underline":true,"subscript":true}]}],"type":"doc"}' +H3Json = '{ "uid":"06e34ad7fc2acd","_version":13,"attrs":{ },"children":[{ "type":"h3","attrs":{ },"uid":"c2df42cfb70 4d7030c65e2e1","children":[{ "text":"Mauris venenatis dui id massa sollicitudin, non bibendum nunc dictum.","bold":true,"italic":true,"underline":true,"subscript":true}]}],"type":"doc"}' +H4Json = '{ "uid":"06e34a7a4e54cd", "_version":13, "attrs":{ "style":{ "text-align":"center" }, "redactor-attributes":{ } }, "children":[{"type":"h4","attrs":{},"uid":"c2dfed4d7030c65e2e1","children":[{"text":"MaNullam feugiat turpis quis elit interdum, vitae laoreet quam viverra","bold":true,"italic":true,"underline":true,"subscript":true}]}],"type":"doc"}' +H5Json = '{ "uid": "06e381190849dacd", "_version": 13, "attrs": { }, "children": [ { "type": "h5", "attrs": {}, "uid": "c2d672242cfb7045e2e1", "children": [ { "text": "Mauris venenatis dui id massa sollicitudin, non bibendum nunc dictum." } ] } ], "type": "doc" }' +H6Json = '{ "uid": "06e34a71190849d7fcd", "_version": 13, "attrs": { }, "children": [ { "type": "h6", "attrs": {}, "uid": "c2dfa672242cfb7e2e1", "children": [ { "text": "Nunc porta diam vitae purus semper, ut consequat lorem vehicula." } ] } ], "type": "doc" }' +OrderListJson = '{ "uid":"06e35 48119084 9d7fc2acd", "_version":13, "attrs":{ }, "children":[{"uid":"2b5b4acbb3cfce02d3e","type":"ol","children":[{"type":"li","attrs":{"style":{"text-align":"justify"},"redactor-attributes":{ }},"uid":"160bbd7430b98bd3d996","children":[{"text":"Morbi in quam molestie, fermentum diam vitae, bibendum ipsum."}]},{ "type":"li","attrs":{ "style":{ "text-align":"justify"},"redactor-attributes":{ } },"uid":"a15f4d749bc2903d","children":[{ "text":"Pellentesque mattis lacus in quam aliquam congue"}]},{ "type":"li","attrs":{ "style":{ "text-align":"justify"},"redactor-attributes":{ } },"uid":"e54224bbcb6f9e8f1e43","children":[{ "text":"Integer feugiat leo dignissim, lobortis enim vitae, mollis lectus."}]},{ "type":"li","attrs":{ "style":{ "text-align":"justify"},"redactor-attributes":{ } },"uid":"c0148bab9af758784","children":[{ "text":"Sed in ante lacinia, molestie metus eu, fringilla sapien."}]}],"id":"7f413d448a","attrs":{ }}],"type":"doc"}' +UnorderListJson = '{ "uid":"0e5481190849a", "_version":13, "attrs":{ }, "children":[{"uid":"a3a2b443ebffc867b","type":"ul","children":[{"uid":"f67354d4eed64451922","type":"li","children":[{"text":"Sed quis metus sed mi hendrerit mollis vel et odio."}],"attrs":{ }},{ "uid":"5 50cba5 bea92f23e36fd1","type":"li","children":[{ "text":"Integer vitae sem dignissim, elementum libero vel, fringilla massa."}],"attrs":{ } },{ "uid":"0e5c9b4cd983e8fd543","type":"li","children":[{ "text":"Integer imperdiet arcu sit amet tortor faucibus aliquet."}],"attrs":{ } },{ "uid":"6d9a43a3816bd83a9a","type":"li","children":[{ "text":"Aenean scelerisque velit vitae dui vehicula, at congue massa sagittis."}],"attrs":{ } }],"id":"b083fa46ef899420ab19","attrs":{ }}],"type":"doc"}' +ImgJson = '{ "uid":"06e34a7a4849d7fc2acd", "_version":13, "attrs":{ }, "children":[{"uid":"f3be74be3b64646e626","type":"img","attrs":{"src":"https://images.contentstack.com/v3/assets/blt7726e6b/bltb42cd1/5fa3be959bedb6b/Donald.jog.png","width":33.69418132611637,"height":"auto","redactor-attributes":{"asset_uid":"47f1aa5ae422cd1"}},"children":[{"text":""}]}],"type":"doc"}' +ParagraphJson = '{ "uid":"0d7fd", "_version":13, "attrs":{ }, "children":[{"type":"p","attrs":{},"uid":"0a1b5676aa510e5a","children":[{"text":"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed condimentum iaculis magna in vehicula. Vestibulum vitae convallis lacus. Praesent a diam iaculis turpis rhoncus faucibus. Aliquam sed pulvinar sem."}]}],"type":"doc"}' +BlockquoteJson = '{ "uid":"06084d7fd", "_version":13, "attrs":{ }, "children":[{"uid":"503f9cc97534db55","type":"blockquote","id":"431f78e567161460","children":[{"text":"Praesent eu ex sed nibh venenatis pretium."}],"attrs":{ }}],"type":"doc"}' +CodeJson = '{ "uid":"06ea490849d7fc2acd", "_version":13, "attrs":{ }, "children":[{"uid":"83fba92c91b30002b","type":"code","attrs":{},"children":[{"text":"Code template."}]}],"type":"doc"}' +TableJson = '{ "uid": "06e481190849d7fcd", "_version": 13, "attrs": { }, "children": [ { "uid": "6dd64343bf634bfadd4", "type": "table", "attrs": { "rows": 4, "cols": 2, "colWidths": [ 250, 250 ] }, "children": [ { "uid": "b9082", "type": "thead", "attrs": {}, "children": [ { "type": "tr", "attrs": {}, "children": [ { "type": "th", "attrs": {}, "children": [ { "type": "p", "attrs": {}, "children": [ { "text": "Header 1" } ], "uid": "daa3ef" } ], "uid": "4b3124414a3" }, { "type": "th", "attrs": { }, "children": [ { "type": "p", "attrs": { }, "children": [ { "text": "Header 2" } ], "uid": "eae83c5797d" } ], "uid": "bca9b6f037a04fb485" } ], "uid": "b91ae7a48ef2e9da1" } ] }, { "type": "tbody", "attrs": { }, "children": [ { "type": "tr", "attrs": { }, "children": [ { "type": "td", "attrs": { }, "children": [ { "type": "p", "attrs": { }, "children": [ { "text": "Body row 1 data 1" } ], "uid": "ec674ccc5ce70b7cab" } ], "uid": "2a70bdeeb99a" }, { "type": "td", "attrs": { }, "children": [ { "type": "p", "attrs": { }, "children": [ { "text": "Body row 1 data 2" } ], "uid": "769a 3f9db34 ce8ec 10486d50" } ], "uid": "d6407 34a5c6 1ab1e5f7d1" } ], "uid": "77f6 b951c68 7f9eb397c5" }, { "type": "tr", "attrs": { }, "children": [ { "type": "td", "attrs": { }, "children": [ { "type": "p", "attrs": { }, "children": [ { "text": "Body row 2 data 1" } ], "uid": "a6bf 11bb48 630e87d721c0" } ], "uid": "3da39838b0feaf" }, { "type": "td", "attrs": { }, "children": [ { "type": "p", "attrs": { }, "children": [ { "text": "Body row 2 data 2" } ], "uid": "3b7d12 1f694202 49029e86313" } ], "uid": "95b38b04abcbc25e94f" } ], "uid": "b 227fea 8d247013 4f1e1e8" } ], "uid": "fd5ab86aa642798451b" } ] } ], "type": "doc" }' +LinkInPJson = '{ "uid":"06e34a7190849d7f2acd", "_version":13, "attrs":{ }, "children":[{"type":"p","attrs":{"style":{"text-align":"left"},"redactor-attributes":{ }},"uid":"d88dcdf4590dff2d","children":[{"text":"","bold":true,"italic":true,"underline":true,"subscript":true},{ "uid":"0d06598201aa8b47","type":"a","attrs":{ "href":"LINK.com","target":"_self"},"children":[{ "text":"LINK"}]},{ "text":""}]}],"type":"doc"}' +EmbedJson = '{ "uid":"06e34a7190849d7f2acd", "_version":13, "attrs":{ }, "children":[{"uid":"251017315905c35d42c9","type":"embed","attrs":{"src":"https://www.youtube.com/watch?v=AOP0yARiW8U"},"children":[{"text":""}]}],"type":"doc"}' +AssetReferenceJson = '{"uid":"06e34a7 5e4 e549d ", "_version":1, "attrs":{}, "children":[{ "uid": "4f7e33 3390a955 de10c1 c836", "type":"reference","attrs":{"display-type":"display","asset-uid":"blt44asset","content-type-uid":"sys_assets","asset-link":"https://images.contentstack.com/v3/assets/blt77263d3e6b/blt73403ee7281/51807f919e0e4/11.jpg","asset-name":"11.jpg","asset-type":"image/jpeg","type":"asset","class-name":"embedded-asset","width":25.16914749661705,"className":"dsd","id":"sdf"},"children":[{"text":""}]}],"type":"doc"}' +EntryReferenceBlockJson = '{ "uid":"06e34a7 5e4 e549d ", "_version":1, "attrs":{ }, "children":[{"uid":"70f9b 325075d43 128c0d0 aa3eb7f291f","type":"reference","attrs":{"display-type":"block","entry-uid":"blttitleuid","content-type-uid":"content_block","locale":"en-us","type":"entry","class-name":"embedded-entry"},"children":[{"text":""}]}],"type":"doc"}' +EntryReferenceLinkJson = '{ "uid":"06e34a7 5e4 e549d", "_version":1, "attrs":{ }, "children":[{"uid":"7626ea98e0e95d602210","type":"reference","attrs":{"target":"_self","href":"/copy-of-entry-final-02","display-type":"link","entry-uid":"bltemmbedEntryuid","content-type-uid":"embeddedrte","locale":"en-us","type":"entry","class-name":"embedded-entry"},"children":[{"text":"/copy-of-entry-final-02"}]}],"type":"doc"}' +EntryReferenceInlineJson = '{ "uid":"06e34a7 5e4 e549d", "_version":1, "attrs":{ }, "children":[{"uid":"506 4878f3f46 s21f0cbc aff","type":"reference","attrs":{"display-type":"inline","entry-uid":"blttitleUpdateuid","content-type-uid":"embeddedrte","locale":"en-us","type":"entry","class-name":"embedded-entry"},"children":[{"text":""}]}],"type":"doc"}' +HRJson = '{ "uid":"06e34a7 5e4 e549d", "_version":1, "attrs":{ }, "children":[{"uid":"f5a7b57 40a8a5c3 576828276b","type":"hr","children":[{"text":""}],"attrs":{ }}],"type":"doc"}' +H1NonChildJson = '{ "uid":"06e34a7a449d7fc2acd","_version":13,"attrs":{ },"children":[{ "type":"h1","attrs":{ },"uid":"c2dfed70 4d7030c65e2e1"}],"type":"doc"}' + + +JSON_EMBEDDED_ITEMS_ENTRY= { + "title"=> 'one', + "url"=> '/one', + "locale"=> 'en-us', + "uid"=> 'blt88jn', + "created_by"=> 'bltcreate', + "updated_by"=> 'bltcreate', + "created_at"=> '2020-08-13T06:18:18.989Z', + "updated_at"=> '2020-08-31T06:06:31.258Z', + "markdown"=> '', + "_embedded_items"=> { + "rich_text_editor"=> [ + { + "_content_type_uid"=> 'sys_assets', + "uid"=> 'blt44asset', + "created_at"=> '2020-08-19T09:13:32.785Z', + "updated_at"=> '2020-08-19T09:13:32.785Z', + "created_by"=> 'bltcreate', + "updated_by"=> 'bltcreate', + "content_type"=> 'application/pdf', + "file_size"=> '13264', + "filename"=> 'dummy.pdf', + "url"=> '/v3/assets/blt333/blt44asset/dummy.pdf', + "_version"=> 1, + "title"=> 'dummy.pdf' + }, + { + "title"=> 'Update this title', + "url"=> '', + "locale"=> 'en-us', + "uid"=> 'blttitleuid', + "_content_type_uid"=> 'content_block', + "_version"=> 5, + "_in_progress"=> false, + "multi_line"=> '', + "_embedded_items"=> { + "rich_text_editor"=> [ + { + "uid"=> 'blttitleuid', + "_content_type_uid"=> 'content_block' + } + ] + }, + "rich_text_editor"=> '
    ', + }, + { + "title"=> 'updated title', + "rich_text_editor"=> [ + '
    ' + ], + "locale"=> 'en-us', + "uid"=> 'blttitleUpdateuid', + "_content_type_uid"=> 'embeddedrte', + "_in_progress"=> false, + "_embedded_items"=> { + "rich_text_editor"=>[ + { + "_content_type_uid"=> 'sys_assets', + "uid"=> 'bltassetUID', + "content_type"=> 'image/png', + "file_size"=> '36743', + "filename"=> 'svg-logo-text.png', + "url"=> '/v3/assets/blturl/bltassetEmbuid/svg-logo-text.png', + "title"=> 'svg-logo-text.png', + "description"=> '' + } + ] + } + }, + { + "title"=> 'Entry with embedded entry', + "rich_text_editor"=> [ + '
    \n
    \n



    \n
    ' + ], + "locale"=> 'en-us', + "uid"=> 'bltemmbedEntryuid', + "_content_type_uid"=> 'embeddedrte', + "_in_progress"=> false, + "_embedded_items"=> { + "rich_text_editor"=>[ + { + "uid"=> 'blt1234CtUID', + "_content_type_uid"=> '1234' + }, + { + "uid"=> 'blt1234CtUID', + "_content_type_uid"=> '1234' + }, + { + "uid"=> 'blt1234AssetUID', + "_content_type_uid"=> 'sys_assets' + } + ] + }, + } + ] + } + } \ No newline at end of file diff --git a/spec/support/xml_parse.rb b/spec/support/xml_parse.rb index 45c03e1..1ae68fa 100644 --- a/spec/support/xml_parse.rb +++ b/spec/support/xml_parse.rb @@ -10,4 +10,8 @@ def appendFrame(string) def getHTML(xml) xml_doc = Nokogiri::XML(appendFrame(xml)) xml_doc.xpath('//documentfragmentcontainer').inner_html +end + +def getJson(text) + JSON.parse(text) end \ No newline at end of file