From 9b0decdcbea1e7368fe55f40e8e00a10be2d118c Mon Sep 17 00:00:00 2001 From: k-kumar-01 Date: Thu, 10 Jun 2021 21:04:06 +0530 Subject: [PATCH 1/9] feat: add text and emphasis transformers - #397 Rules and helpers for the conversion Complete roundtripping for text and emphasis(CiceroMark->OOXML->CiceroMark) Test for the above Signed-off-by: k-kumar-01 --- .../src/CiceroMarkToOOXMLTransformer.js | 82 ++++++++++++++--- .../src/CiceroMarkToOOXMLTransformer.test.js | 37 ++++++++ .../markdown-docx/src/OoxmlTransformer.js | 11 +++ packages/markdown-docx/src/helpers.js | 88 +++++++++++++++++++ packages/markdown-docx/src/rules.js | 33 +++++++ .../test/data/ciceroMark/ciceroMark.js | 73 +++++++++++++++ 6 files changed, 310 insertions(+), 14 deletions(-) create mode 100644 packages/markdown-docx/src/CiceroMarkToOOXMLTransformer.test.js create mode 100644 packages/markdown-docx/src/helpers.js create mode 100644 packages/markdown-docx/src/rules.js create mode 100644 packages/markdown-docx/test/data/ciceroMark/ciceroMark.js diff --git a/packages/markdown-docx/src/CiceroMarkToOOXMLTransformer.js b/packages/markdown-docx/src/CiceroMarkToOOXMLTransformer.js index c6335689..5e7a0cf3 100644 --- a/packages/markdown-docx/src/CiceroMarkToOOXMLTransformer.js +++ b/packages/markdown-docx/src/CiceroMarkToOOXMLTransformer.js @@ -14,23 +14,73 @@ 'use strict'; +const { TEXT_RULE } = require('./rules'); +const {wrapOOXML} = require('./helpers'); + +const definedNodes = { + computedVariable: 'org.accordproject.ciceromark.ComputedVariable', + heading: 'org.accordproject.commonmark.Heading', + item: 'org.accordproject.commonmark.Item', + list: 'org.accordproject.commonmark.List', + listBlock: 'org.accordproject.ciceromark.ListBlock', + paragraph: 'org.accordproject.commonmark.Paragraph', + softbreak: 'org.accordproject.commonmark.Softbreak', + text: 'org.accordproject.commonmark.Text', + variable: 'org.accordproject.ciceromark.Variable', + emphasize: 'org.accordproject.commonmark.Emph', +}; + +let globalOOXML=''; + /** * Transforms the ciceromark to OOXML */ class CiceroMarkToOOXMLTransfomer { - definedNodes = { - computedVariable: 'org.accordproject.ciceromark.ComputedVariable', - heading: 'org.accordproject.commonmark.Heading', - item: 'org.accordproject.commonmark.Item', - list: 'org.accordproject.commonmark.List', - listBlock: 'org.accordproject.ciceromark.ListBlock', - paragraph: 'org.accordproject.commonmark.Paragraph', - softbreak: 'org.accordproject.commonmark.Softbreak', - text: 'org.accordproject.commonmark.Text', - variable: 'org.accordproject.ciceromark.Variable', - emphasize: 'org.accordproject.commonmark.Emph', - }; + /** + * Gets the class of a given CiceroMark Node. + * + * @param {object} node CiceroMark node entity + * @returns {string} Class of given node + */ + getClass(node) { + return node.$class; + } + + /** + * Gets the OOXML for the given node. + * + * @param {object} node Description of node type + * @param {object} counter Counter for different variables based on node name + * @param {object} parent Parent object for a node + * @returns {string} OOXML for the given node + */ + getNodes(node, counter, parent = null) { + if (this.getClass(node) === definedNodes.text) { + if (parent !== null && parent.class === definedNodes.emphasize) { + return TEXT_RULE(node.text, true); + } else { + return TEXT_RULE(node.text); + } + } + + if (this.getClass(node) === definedNodes.emphasize) { + let ooxml = ''; + node.nodes.forEach(subNode => { + ooxml += this.getNodes(subNode, counter, { class: node.$class }); + }); + return ooxml; + } + + if (this.getClass(node) === definedNodes.paragraph) { + let ooxml = ''; + node.nodes.forEach(subNode => { + ooxml += this.getNodes(subNode, counter, ); + }); + globalOOXML = `${globalOOXML}${ooxml}`; + } + return ''; + } /** * Transforms the given CiceroMark JSON to OOXML @@ -40,8 +90,12 @@ class CiceroMarkToOOXMLTransfomer { * @param {string} ooxml Initial OOXML string * @returns {string} Converted OOXML string i.e. CicecoMark->OOXML */ - toOOXML(ciceromark, counter, ooxml) { - let globalOOXML = ooxml; + toOOXML(ciceromark, counter, ooxml = '') { + globalOOXML = ooxml; + ciceromark.nodes.forEach(node => { + this.getNodes(node, counter); + }); + globalOOXML = wrapOOXML(globalOOXML); return globalOOXML; } } diff --git a/packages/markdown-docx/src/CiceroMarkToOOXMLTransformer.test.js b/packages/markdown-docx/src/CiceroMarkToOOXMLTransformer.test.js new file mode 100644 index 00000000..406f64b3 --- /dev/null +++ b/packages/markdown-docx/src/CiceroMarkToOOXMLTransformer.test.js @@ -0,0 +1,37 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// @ts-nocheck +/* eslint-disable no-undef */ +'use strict'; + +const chai = require('chai'); + +const expect = chai.expect; + +const OoxmlTransformer = require('./OoxmlTransformer'); +const CiceroMarkToOOXMLTransfomer = require('./CiceroMarkToOOXMLTransformer'); +const { paraAndEmphasisJSON } = require('../test/data/ciceroMark/ciceroMark'); + +describe('Perform roundtripping between CiceroMark and OOXML', () => { + + it('should parse paragraphs and emphasis nodes.', async () => { + const ciceroMarkTransformer = new CiceroMarkToOOXMLTransfomer(); + const ooxml = ciceroMarkTransformer.toOOXML(paraAndEmphasisJSON); + + const ooxmlTransformer = new OoxmlTransformer(); + const convertedObject = ooxmlTransformer.toCiceroMark(ooxml); + expect(convertedObject).to.deep.equal(paraAndEmphasisJSON); + }); +}); \ No newline at end of file diff --git a/packages/markdown-docx/src/OoxmlTransformer.js b/packages/markdown-docx/src/OoxmlTransformer.js index 2fcde302..4fa2230d 100644 --- a/packages/markdown-docx/src/OoxmlTransformer.js +++ b/packages/markdown-docx/src/OoxmlTransformer.js @@ -151,6 +151,17 @@ class OoxmlTransformer { $class: `${NS_PREFIX_CommonMarkModel}Softbreak`, }; case 'w:r': + if(element.elements[0].name==='w:rPr'){ + let emphFound = element.elements[0].elements.some(subElement=>{ + return subElement.name==='w:i' && subElement.attributes['w:val'] === 'true'; + }); + if(emphFound){ + return { + $class: `${NS_PREFIX_CommonMarkModel}Emph`, + nodes:[...this.deserializeElements(element.elements)] + }; + } + } return [...this.deserializeElements(element.elements)]; case 'w:color': return element.attributes['w:color']; diff --git a/packages/markdown-docx/src/helpers.js b/packages/markdown-docx/src/helpers.js new file mode 100644 index 00000000..cb2b3aaf --- /dev/null +++ b/packages/markdown-docx/src/helpers.js @@ -0,0 +1,88 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; + +/** + * Replaces the angular brackets with the respective codes. + * + * @param {string} node String to be replaced + * @returns {string} String with replaced angular brackets + */ +function sanitizeHtmlChars(node) { + return node.replace(/>/g, '>').replace(/ + + + + + + + + + + + + ${ooxml} + + + + + + `; + + return ooxml; +} + +module.exports = { sanitizeHtmlChars, wrapOOXML }; \ No newline at end of file diff --git a/packages/markdown-docx/src/rules.js b/packages/markdown-docx/src/rules.js new file mode 100644 index 00000000..6a8d2736 --- /dev/null +++ b/packages/markdown-docx/src/rules.js @@ -0,0 +1,33 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; + +const { sanitizeHtmlChars } = require('./helpers'); + +/** + * Inserts text. + * + * @param {string} value Text to be rendered + * @param {boolean} emphasize true=emphasized text, false=normal text + * @returns {string} OOXML for the text + */ +const TEXT_RULE = (value, emphasize = false) => { + if (emphasize) { + return `${sanitizeHtmlChars(value)}`; + } + return `${sanitizeHtmlChars(value)}`; +}; + +module.exports = { TEXT_RULE }; \ No newline at end of file diff --git a/packages/markdown-docx/test/data/ciceroMark/ciceroMark.js b/packages/markdown-docx/test/data/ciceroMark/ciceroMark.js new file mode 100644 index 00000000..5b4af8e7 --- /dev/null +++ b/packages/markdown-docx/test/data/ciceroMark/ciceroMark.js @@ -0,0 +1,73 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; + +const paraAndEmphasisJSON = { + '$class':'org.accordproject.commonmark.Document', + 'xmlns':'http://commonmark.org/xml/1.0', + 'nodes':[ + { + '$class':'org.accordproject.commonmark.Paragraph', + 'nodes':[ + { + '$class':'org.accordproject.commonmark.Text', + 'text':'Hello and Welcome to the testing round. Today we try ' + }, + { + '$class':'org.accordproject.commonmark.Emph', + 'nodes':[ + { + '$class':'org.accordproject.commonmark.Text', + 'text':'testing' + } + ] + }, + { + '$class':'org.accordproject.commonmark.Text', + 'text':' of the converters.' + } + ] + }, + { + '$class':'org.accordproject.commonmark.Paragraph', + 'nodes':[ + { + '$class':'org.accordproject.commonmark.Text', + 'text':'Let\'s start with the basic testing of the converters.' + } + ] + }, + { + '$class':'org.accordproject.commonmark.Paragraph', + 'nodes':[ + { + '$class':'org.accordproject.commonmark.Text', + 'text':'The Accord Project is a non-profit, collaborative, initiative developing an ecosystem and open source tools specifically for smart legal contracts. Open source means that anyone can freely use and contribute to development.' + } + ] + }, + { + '$class':'org.accordproject.commonmark.Paragraph', + 'nodes':[ + { + '$class':'org.accordproject.commonmark.Text', + 'text':'We hope to see many more successful tests.' + } + ] + } + ] +}; + +module.exports = { paraAndEmphasisJSON }; \ No newline at end of file From e43b15ab1feae539d1136ed2375a77736fc4483c Mon Sep 17 00:00:00 2001 From: k-kumar-01 Date: Fri, 11 Jun 2021 01:28:04 +0530 Subject: [PATCH 2/9] feat: add text and emphasis transformers - #397 Naming changed to improve readability and understandability Files renamed for better understanding Signed-off-by: k-kumar-01 --- ...helpers.js => CiceroMarkToOOXMLHelpers.js} | 4 +- .../{rules.js => CiceroMarkToOOXMLRules.js} | 20 +++-- .../src/CiceroMarkToOOXMLTransformer.js | 14 ++-- .../src/CiceroMarkToOOXMLTransformer.test.js | 8 +- .../test/data/ciceroMark/ciceroMark.js | 73 ------------------- .../data/ciceroMark/text-and-emphasis.json | 1 + 6 files changed, 28 insertions(+), 92 deletions(-) rename packages/markdown-docx/src/{helpers.js => CiceroMarkToOOXMLHelpers.js} (97%) rename packages/markdown-docx/src/{rules.js => CiceroMarkToOOXMLRules.js} (64%) delete mode 100644 packages/markdown-docx/test/data/ciceroMark/ciceroMark.js create mode 100644 packages/markdown-docx/test/data/ciceroMark/text-and-emphasis.json diff --git a/packages/markdown-docx/src/helpers.js b/packages/markdown-docx/src/CiceroMarkToOOXMLHelpers.js similarity index 97% rename from packages/markdown-docx/src/helpers.js rename to packages/markdown-docx/src/CiceroMarkToOOXMLHelpers.js index cb2b3aaf..1ae3d42f 100644 --- a/packages/markdown-docx/src/helpers.js +++ b/packages/markdown-docx/src/CiceroMarkToOOXMLHelpers.js @@ -30,7 +30,7 @@ function sanitizeHtmlChars(node) { * @param {string} ooxml OOXML to be wrapped * @returns {string} OOXML wraped in docx headers */ -function wrapOOXML(ooxml){ +function wrapAroundDefaultDocxTags(ooxml) { ooxml = ` @@ -85,4 +85,4 @@ function wrapOOXML(ooxml){ return ooxml; } -module.exports = { sanitizeHtmlChars, wrapOOXML }; \ No newline at end of file +module.exports = { sanitizeHtmlChars, wrapAroundDefaultDocxTags }; \ No newline at end of file diff --git a/packages/markdown-docx/src/rules.js b/packages/markdown-docx/src/CiceroMarkToOOXMLRules.js similarity index 64% rename from packages/markdown-docx/src/rules.js rename to packages/markdown-docx/src/CiceroMarkToOOXMLRules.js index 6a8d2736..e516a718 100644 --- a/packages/markdown-docx/src/rules.js +++ b/packages/markdown-docx/src/CiceroMarkToOOXMLRules.js @@ -14,20 +14,26 @@ 'use strict'; -const { sanitizeHtmlChars } = require('./helpers'); +const { sanitizeHtmlChars } = require('./CiceroMarkToOOXMLHelpers'); /** * Inserts text. * * @param {string} value Text to be rendered - * @param {boolean} emphasize true=emphasized text, false=normal text * @returns {string} OOXML for the text */ -const TEXT_RULE = (value, emphasize = false) => { - if (emphasize) { - return `${sanitizeHtmlChars(value)}`; - } +const TEXT_RULE = (value) => { return `${sanitizeHtmlChars(value)}`; }; -module.exports = { TEXT_RULE }; \ No newline at end of file +/** + * Inserts emphsaised text. + * + * @param {string} value Text to be rendered + * @returns {string} OOXML for the emphasised text + */ +const EMPHASIS_RULE = (value) => { + return `${sanitizeHtmlChars(value)}`; +}; + +module.exports = { TEXT_RULE, EMPHASIS_RULE }; \ No newline at end of file diff --git a/packages/markdown-docx/src/CiceroMarkToOOXMLTransformer.js b/packages/markdown-docx/src/CiceroMarkToOOXMLTransformer.js index 5e7a0cf3..4e9bc636 100644 --- a/packages/markdown-docx/src/CiceroMarkToOOXMLTransformer.js +++ b/packages/markdown-docx/src/CiceroMarkToOOXMLTransformer.js @@ -14,8 +14,8 @@ 'use strict'; -const { TEXT_RULE } = require('./rules'); -const {wrapOOXML} = require('./helpers'); +const { TEXT_RULE, EMPHASIS_RULE } = require('./CiceroMarkToOOXMLRules'); +const { wrapAroundDefaultDocxTags } = require('./CiceroMarkToOOXMLHelpers'); const definedNodes = { computedVariable: 'org.accordproject.ciceromark.ComputedVariable', @@ -30,7 +30,7 @@ const definedNodes = { emphasize: 'org.accordproject.commonmark.Emph', }; -let globalOOXML=''; +let globalOOXML = ''; /** * Transforms the ciceromark to OOXML @@ -38,7 +38,7 @@ let globalOOXML=''; class CiceroMarkToOOXMLTransfomer { /** - * Gets the class of a given CiceroMark Node. + * Gets the class of a given CiceroMark node. * * @param {object} node CiceroMark node entity * @returns {string} Class of given node @@ -58,7 +58,7 @@ class CiceroMarkToOOXMLTransfomer { getNodes(node, counter, parent = null) { if (this.getClass(node) === definedNodes.text) { if (parent !== null && parent.class === definedNodes.emphasize) { - return TEXT_RULE(node.text, true); + return EMPHASIS_RULE(node.text, true); } else { return TEXT_RULE(node.text); } @@ -75,7 +75,7 @@ class CiceroMarkToOOXMLTransfomer { if (this.getClass(node) === definedNodes.paragraph) { let ooxml = ''; node.nodes.forEach(subNode => { - ooxml += this.getNodes(subNode, counter, ); + ooxml += this.getNodes(subNode, counter,); }); globalOOXML = `${globalOOXML}${ooxml}`; } @@ -95,7 +95,7 @@ class CiceroMarkToOOXMLTransfomer { ciceromark.nodes.forEach(node => { this.getNodes(node, counter); }); - globalOOXML = wrapOOXML(globalOOXML); + globalOOXML = wrapAroundDefaultDocxTags(globalOOXML); return globalOOXML; } } diff --git a/packages/markdown-docx/src/CiceroMarkToOOXMLTransformer.test.js b/packages/markdown-docx/src/CiceroMarkToOOXMLTransformer.test.js index 406f64b3..8ad9ffcc 100644 --- a/packages/markdown-docx/src/CiceroMarkToOOXMLTransformer.test.js +++ b/packages/markdown-docx/src/CiceroMarkToOOXMLTransformer.test.js @@ -16,22 +16,24 @@ /* eslint-disable no-undef */ 'use strict'; +const fs = require('fs'); const chai = require('chai'); const expect = chai.expect; const OoxmlTransformer = require('./OoxmlTransformer'); const CiceroMarkToOOXMLTransfomer = require('./CiceroMarkToOOXMLTransformer'); -const { paraAndEmphasisJSON } = require('../test/data/ciceroMark/ciceroMark'); describe('Perform roundtripping between CiceroMark and OOXML', () => { it('should parse paragraphs and emphasis nodes.', async () => { + const paraAndEmphasisCiceroMark = await fs.readFileSync('test/data/ciceroMark/text-and-emphasis.json', 'utf-8'); + const ciceroMarkTransformer = new CiceroMarkToOOXMLTransfomer(); - const ooxml = ciceroMarkTransformer.toOOXML(paraAndEmphasisJSON); + const ooxml = ciceroMarkTransformer.toOOXML(JSON.parse(paraAndEmphasisCiceroMark)); const ooxmlTransformer = new OoxmlTransformer(); const convertedObject = ooxmlTransformer.toCiceroMark(ooxml); - expect(convertedObject).to.deep.equal(paraAndEmphasisJSON); + expect(convertedObject).to.deep.equal(JSON.parse(paraAndEmphasisCiceroMark)); }); }); \ No newline at end of file diff --git a/packages/markdown-docx/test/data/ciceroMark/ciceroMark.js b/packages/markdown-docx/test/data/ciceroMark/ciceroMark.js deleted file mode 100644 index 5b4af8e7..00000000 --- a/packages/markdown-docx/test/data/ciceroMark/ciceroMark.js +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -'use strict'; - -const paraAndEmphasisJSON = { - '$class':'org.accordproject.commonmark.Document', - 'xmlns':'http://commonmark.org/xml/1.0', - 'nodes':[ - { - '$class':'org.accordproject.commonmark.Paragraph', - 'nodes':[ - { - '$class':'org.accordproject.commonmark.Text', - 'text':'Hello and Welcome to the testing round. Today we try ' - }, - { - '$class':'org.accordproject.commonmark.Emph', - 'nodes':[ - { - '$class':'org.accordproject.commonmark.Text', - 'text':'testing' - } - ] - }, - { - '$class':'org.accordproject.commonmark.Text', - 'text':' of the converters.' - } - ] - }, - { - '$class':'org.accordproject.commonmark.Paragraph', - 'nodes':[ - { - '$class':'org.accordproject.commonmark.Text', - 'text':'Let\'s start with the basic testing of the converters.' - } - ] - }, - { - '$class':'org.accordproject.commonmark.Paragraph', - 'nodes':[ - { - '$class':'org.accordproject.commonmark.Text', - 'text':'The Accord Project is a non-profit, collaborative, initiative developing an ecosystem and open source tools specifically for smart legal contracts. Open source means that anyone can freely use and contribute to development.' - } - ] - }, - { - '$class':'org.accordproject.commonmark.Paragraph', - 'nodes':[ - { - '$class':'org.accordproject.commonmark.Text', - 'text':'We hope to see many more successful tests.' - } - ] - } - ] -}; - -module.exports = { paraAndEmphasisJSON }; \ No newline at end of file diff --git a/packages/markdown-docx/test/data/ciceroMark/text-and-emphasis.json b/packages/markdown-docx/test/data/ciceroMark/text-and-emphasis.json new file mode 100644 index 00000000..b1198879 --- /dev/null +++ b/packages/markdown-docx/test/data/ciceroMark/text-and-emphasis.json @@ -0,0 +1 @@ +{"$class":"org.accordproject.commonmark.Document","xmlns":"http://commonmark.org/xml/1.0","nodes":[{"$class":"org.accordproject.commonmark.Paragraph","nodes":[{"$class":"org.accordproject.commonmark.Text","text":"Hello and Welcome to the testing round. Today we try "},{"$class":"org.accordproject.commonmark.Emph","nodes":[{"$class":"org.accordproject.commonmark.Text","text":"testing"}]},{"$class":"org.accordproject.commonmark.Text","text":" of the converters."}]},{"$class":"org.accordproject.commonmark.Paragraph","nodes":[{"$class":"org.accordproject.commonmark.Text","text":"Let\"s start with the basic testing of the converters."}]},{"$class":"org.accordproject.commonmark.Paragraph","nodes":[{"$class":"org.accordproject.commonmark.Text","text":"The Accord Project is a non-profit, collaborative, initiative developing an ecosystem and open source tools specifically for smart legal contracts. Open source means that anyone can freely use and contribute to development."}]},{"$class":"org.accordproject.commonmark.Paragraph","nodes":[{"$class":"org.accordproject.commonmark.Text","text":"We hope to see many more successful tests."}]}]} \ No newline at end of file From f875a96887ba490cc3574a0740c1a9707dd4df88 Mon Sep 17 00:00:00 2001 From: k-kumar-01 Date: Fri, 11 Jun 2021 10:37:38 +0530 Subject: [PATCH 3/9] feat: add text and emphasis transformers - #397 Documentation update Signed-off-by: k-kumar-01 --- packages/markdown-docx/src/CiceroMarkToOOXMLRules.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/markdown-docx/src/CiceroMarkToOOXMLRules.js b/packages/markdown-docx/src/CiceroMarkToOOXMLRules.js index e516a718..7b177ddd 100644 --- a/packages/markdown-docx/src/CiceroMarkToOOXMLRules.js +++ b/packages/markdown-docx/src/CiceroMarkToOOXMLRules.js @@ -19,18 +19,18 @@ const { sanitizeHtmlChars } = require('./CiceroMarkToOOXMLHelpers'); /** * Inserts text. * - * @param {string} value Text to be rendered - * @returns {string} OOXML for the text + * @param {string} value Enclosing value of the OOXML tag + * @returns {string} OOXML tag for the text */ const TEXT_RULE = (value) => { return `${sanitizeHtmlChars(value)}`; }; /** - * Inserts emphsaised text. + * Inserts emphasised text. * - * @param {string} value Text to be rendered - * @returns {string} OOXML for the emphasised text + * @param {string} value Enclosing value of the OOXML tag + * @returns {string} OOXML tag for the emphasised text */ const EMPHASIS_RULE = (value) => { return `${sanitizeHtmlChars(value)}`; From 77a0e00fa1974ac2e25cd4f6069aa87e2d0f04e0 Mon Sep 17 00:00:00 2001 From: k-kumar-01 Date: Fri, 11 Jun 2021 11:08:12 +0530 Subject: [PATCH 4/9] feat: add text and emphasis transformers - #397 Declares OOXML as instance variable Signed-off-by: k-kumar-01 --- .../src/CiceroMarkToOOXMLTransformer.js | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/packages/markdown-docx/src/CiceroMarkToOOXMLTransformer.js b/packages/markdown-docx/src/CiceroMarkToOOXMLTransformer.js index 4e9bc636..9d9bd07d 100644 --- a/packages/markdown-docx/src/CiceroMarkToOOXMLTransformer.js +++ b/packages/markdown-docx/src/CiceroMarkToOOXMLTransformer.js @@ -30,13 +30,19 @@ const definedNodes = { emphasize: 'org.accordproject.commonmark.Emph', }; -let globalOOXML = ''; - /** * Transforms the ciceromark to OOXML */ class CiceroMarkToOOXMLTransfomer { + + /** + * Declares the OOXML variable + */ + constructor() { + this.globalOOXML = ''; + } + /** * Gets the class of a given CiceroMark node. * @@ -77,7 +83,7 @@ class CiceroMarkToOOXMLTransfomer { node.nodes.forEach(subNode => { ooxml += this.getNodes(subNode, counter,); }); - globalOOXML = `${globalOOXML}${ooxml}`; + this.globalOOXML = `${this.globalOOXML}${ooxml}`; } return ''; } @@ -91,12 +97,12 @@ class CiceroMarkToOOXMLTransfomer { * @returns {string} Converted OOXML string i.e. CicecoMark->OOXML */ toOOXML(ciceromark, counter, ooxml = '') { - globalOOXML = ooxml; + this.globalOOXML = ooxml; ciceromark.nodes.forEach(node => { this.getNodes(node, counter); }); - globalOOXML = wrapAroundDefaultDocxTags(globalOOXML); - return globalOOXML; + this.globalOOXML = wrapAroundDefaultDocxTags(this.globalOOXML); + return this.globalOOXML; } } From d5f5b36a4e20ea6ecf57bff787147c509f36d3ae Mon Sep 17 00:00:00 2001 From: k-kumar-01 Date: Fri, 11 Jun 2021 13:31:59 +0530 Subject: [PATCH 5/9] feat: add text and emphasis transformers - #397 Variable name changed Spacing improved Signed-off-by: k-kumar-01 --- packages/markdown-docx/src/OoxmlTransformer.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/markdown-docx/src/OoxmlTransformer.js b/packages/markdown-docx/src/OoxmlTransformer.js index 4fa2230d..b6af73d2 100644 --- a/packages/markdown-docx/src/OoxmlTransformer.js +++ b/packages/markdown-docx/src/OoxmlTransformer.js @@ -151,14 +151,18 @@ class OoxmlTransformer { $class: `${NS_PREFIX_CommonMarkModel}Softbreak`, }; case 'w:r': - if(element.elements[0].name==='w:rPr'){ - let emphFound = element.elements[0].elements.some(subElement=>{ - return subElement.name==='w:i' && subElement.attributes['w:val'] === 'true'; - }); - if(emphFound){ + if (element.elements[0].name === 'w:rPr') { + let emphasisedTextFound = element.elements[0].elements.some( + subElement => { + return subElement.name === 'w:i'; + } + ); + if (emphasisedTextFound) { return { $class: `${NS_PREFIX_CommonMarkModel}Emph`, - nodes:[...this.deserializeElements(element.elements)] + nodes: [ + ...this.deserializeElements(element.elements), + ], }; } } From 18fb1fdb8e7d7d34bdf3ecd0cbf755f4fde173d8 Mon Sep 17 00:00:00 2001 From: k-kumar-01 Date: Fri, 11 Jun 2021 14:02:57 +0530 Subject: [PATCH 6/9] feat: add text and emphasis transformers - #397 Remove attribute from ooxml tag Signed-off-by: k-kumar-01 --- packages/markdown-docx/src/CiceroMarkToOOXMLRules.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/markdown-docx/src/CiceroMarkToOOXMLRules.js b/packages/markdown-docx/src/CiceroMarkToOOXMLRules.js index 7b177ddd..bda62b5b 100644 --- a/packages/markdown-docx/src/CiceroMarkToOOXMLRules.js +++ b/packages/markdown-docx/src/CiceroMarkToOOXMLRules.js @@ -33,7 +33,7 @@ const TEXT_RULE = (value) => { * @returns {string} OOXML tag for the emphasised text */ const EMPHASIS_RULE = (value) => { - return `${sanitizeHtmlChars(value)}`; + return `${sanitizeHtmlChars(value)}`; }; module.exports = { TEXT_RULE, EMPHASIS_RULE }; \ No newline at end of file From 1771d6bb1e47b835ab8030e361aeb5e7aee8352d Mon Sep 17 00:00:00 2001 From: k-kumar-01 Date: Fri, 11 Jun 2021 16:23:00 +0530 Subject: [PATCH 7/9] refactor: export transformers from index - #397 OOXML spacing changes Signed-off-by: k-kumar-01 --- .../helpers.js} | 2 +- .../index.js} | 6 +++--- .../index.test.js} | 20 +++++++++++-------- .../rules.js} | 15 ++++++++++---- .../index.js} | 0 .../index.test.js} | 2 +- packages/markdown-docx/src/index.js | 17 ++++++++++++++++ 7 files changed, 45 insertions(+), 17 deletions(-) rename packages/markdown-docx/src/{CiceroMarkToOOXMLHelpers.js => CiceroMarkToOOXML/helpers.js} (99%) rename packages/markdown-docx/src/{CiceroMarkToOOXMLTransformer.js => CiceroMarkToOOXML/index.js} (94%) rename packages/markdown-docx/src/{CiceroMarkToOOXMLTransformer.test.js => CiceroMarkToOOXML/index.test.js} (62%) rename packages/markdown-docx/src/{CiceroMarkToOOXMLRules.js => CiceroMarkToOOXML/rules.js} (73%) rename packages/markdown-docx/src/{OoxmlTransformer.js => OOXMLTransformer/index.js} (100%) rename packages/markdown-docx/src/{OoxmlTransformer.test.js => OOXMLTransformer/index.test.js} (95%) create mode 100644 packages/markdown-docx/src/index.js diff --git a/packages/markdown-docx/src/CiceroMarkToOOXMLHelpers.js b/packages/markdown-docx/src/CiceroMarkToOOXML/helpers.js similarity index 99% rename from packages/markdown-docx/src/CiceroMarkToOOXMLHelpers.js rename to packages/markdown-docx/src/CiceroMarkToOOXML/helpers.js index 1ae3d42f..a7f1ae06 100644 --- a/packages/markdown-docx/src/CiceroMarkToOOXMLHelpers.js +++ b/packages/markdown-docx/src/CiceroMarkToOOXML/helpers.js @@ -85,4 +85,4 @@ function wrapAroundDefaultDocxTags(ooxml) { return ooxml; } -module.exports = { sanitizeHtmlChars, wrapAroundDefaultDocxTags }; \ No newline at end of file +module.exports = { sanitizeHtmlChars, wrapAroundDefaultDocxTags }; diff --git a/packages/markdown-docx/src/CiceroMarkToOOXMLTransformer.js b/packages/markdown-docx/src/CiceroMarkToOOXML/index.js similarity index 94% rename from packages/markdown-docx/src/CiceroMarkToOOXMLTransformer.js rename to packages/markdown-docx/src/CiceroMarkToOOXML/index.js index 9d9bd07d..86629091 100644 --- a/packages/markdown-docx/src/CiceroMarkToOOXMLTransformer.js +++ b/packages/markdown-docx/src/CiceroMarkToOOXML/index.js @@ -14,8 +14,8 @@ 'use strict'; -const { TEXT_RULE, EMPHASIS_RULE } = require('./CiceroMarkToOOXMLRules'); -const { wrapAroundDefaultDocxTags } = require('./CiceroMarkToOOXMLHelpers'); +const { TEXT_RULE, EMPHASIS_RULE } = require('./rules'); +const { wrapAroundDefaultDocxTags } = require('./helpers'); const definedNodes = { computedVariable: 'org.accordproject.ciceromark.ComputedVariable', @@ -106,4 +106,4 @@ class CiceroMarkToOOXMLTransfomer { } } -module.exports = CiceroMarkToOOXMLTransfomer; \ No newline at end of file +module.exports = CiceroMarkToOOXMLTransfomer; diff --git a/packages/markdown-docx/src/CiceroMarkToOOXMLTransformer.test.js b/packages/markdown-docx/src/CiceroMarkToOOXML/index.test.js similarity index 62% rename from packages/markdown-docx/src/CiceroMarkToOOXMLTransformer.test.js rename to packages/markdown-docx/src/CiceroMarkToOOXML/index.test.js index 8ad9ffcc..233f5cf5 100644 --- a/packages/markdown-docx/src/CiceroMarkToOOXMLTransformer.test.js +++ b/packages/markdown-docx/src/CiceroMarkToOOXML/index.test.js @@ -21,19 +21,23 @@ const chai = require('chai'); const expect = chai.expect; -const OoxmlTransformer = require('./OoxmlTransformer'); -const CiceroMarkToOOXMLTransfomer = require('./CiceroMarkToOOXMLTransformer'); +const OoxmlTransformer = require('../OOXMLTransformer'); +const CiceroMarkToOOXMLTransfomer = require('.'); describe('Perform roundtripping between CiceroMark and OOXML', () => { - - it('should parse paragraphs and emphasis nodes.', async () => { - const paraAndEmphasisCiceroMark = await fs.readFileSync('test/data/ciceroMark/text-and-emphasis.json', 'utf-8'); + it('should parse textgraphs and emphasis nodes.', async () => { + let textAndEmphasisCiceroMark = await fs.readFileSync( + 'test/data/ciceroMark/text-and-emphasis.json', + 'utf-8' + ); + // converts from string to JSON object + textAndEmphasisCiceroMark = JSON.parse(textAndEmphasisCiceroMark); const ciceroMarkTransformer = new CiceroMarkToOOXMLTransfomer(); - const ooxml = ciceroMarkTransformer.toOOXML(JSON.parse(paraAndEmphasisCiceroMark)); + const ooxml = ciceroMarkTransformer.toOOXML(textAndEmphasisCiceroMark); const ooxmlTransformer = new OoxmlTransformer(); const convertedObject = ooxmlTransformer.toCiceroMark(ooxml); - expect(convertedObject).to.deep.equal(JSON.parse(paraAndEmphasisCiceroMark)); + expect(convertedObject).to.deep.equal(textAndEmphasisCiceroMark); }); -}); \ No newline at end of file +}); diff --git a/packages/markdown-docx/src/CiceroMarkToOOXMLRules.js b/packages/markdown-docx/src/CiceroMarkToOOXML/rules.js similarity index 73% rename from packages/markdown-docx/src/CiceroMarkToOOXMLRules.js rename to packages/markdown-docx/src/CiceroMarkToOOXML/rules.js index bda62b5b..c432403c 100644 --- a/packages/markdown-docx/src/CiceroMarkToOOXMLRules.js +++ b/packages/markdown-docx/src/CiceroMarkToOOXML/rules.js @@ -14,7 +14,7 @@ 'use strict'; -const { sanitizeHtmlChars } = require('./CiceroMarkToOOXMLHelpers'); +const { sanitizeHtmlChars } = require('./helpers'); /** * Inserts text. @@ -23,7 +23,9 @@ const { sanitizeHtmlChars } = require('./CiceroMarkToOOXMLHelpers'); * @returns {string} OOXML tag for the text */ const TEXT_RULE = (value) => { - return `${sanitizeHtmlChars(value)}`; + return ` + ${sanitizeHtmlChars(value)} + `; }; /** @@ -33,7 +35,12 @@ const TEXT_RULE = (value) => { * @returns {string} OOXML tag for the emphasised text */ const EMPHASIS_RULE = (value) => { - return `${sanitizeHtmlChars(value)}`; + return ` + + + + ${sanitizeHtmlChars(value)} + `; }; -module.exports = { TEXT_RULE, EMPHASIS_RULE }; \ No newline at end of file +module.exports = { TEXT_RULE, EMPHASIS_RULE }; diff --git a/packages/markdown-docx/src/OoxmlTransformer.js b/packages/markdown-docx/src/OOXMLTransformer/index.js similarity index 100% rename from packages/markdown-docx/src/OoxmlTransformer.js rename to packages/markdown-docx/src/OOXMLTransformer/index.js diff --git a/packages/markdown-docx/src/OoxmlTransformer.test.js b/packages/markdown-docx/src/OOXMLTransformer/index.test.js similarity index 95% rename from packages/markdown-docx/src/OoxmlTransformer.test.js rename to packages/markdown-docx/src/OOXMLTransformer/index.test.js index 5fe7c24c..f0740bb7 100644 --- a/packages/markdown-docx/src/OoxmlTransformer.test.js +++ b/packages/markdown-docx/src/OOXMLTransformer/index.test.js @@ -19,7 +19,7 @@ const chai = require('chai'); const expect = chai.expect; -const OoxmlTransformer = require('./OoxmlTransformer'); +const OoxmlTransformer = require('.'); describe('OOXML -> CiceroMark', () => { diff --git a/packages/markdown-docx/src/index.js b/packages/markdown-docx/src/index.js new file mode 100644 index 00000000..ed611a3f --- /dev/null +++ b/packages/markdown-docx/src/index.js @@ -0,0 +1,17 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; + +module.exports.CiceroMarkToOOXMLTransfomer= require('./CiceroMarkToOOXML'); From 6c911c254d147e258de0c755076ef5bb8d7a3e5d Mon Sep 17 00:00:00 2001 From: k-kumar-01 Date: Fri, 11 Jun 2021 17:05:12 +0530 Subject: [PATCH 8/9] refactor: formatting rules - #397 Signed-off-by: k-kumar-01 --- packages/markdown-docx/.prettierrc.json | 5 +++++ .../src/CiceroMarkToOOXML/rules.js | 22 +++++++++++-------- 2 files changed, 18 insertions(+), 9 deletions(-) create mode 100644 packages/markdown-docx/.prettierrc.json diff --git a/packages/markdown-docx/.prettierrc.json b/packages/markdown-docx/.prettierrc.json new file mode 100644 index 00000000..4a3b085d --- /dev/null +++ b/packages/markdown-docx/.prettierrc.json @@ -0,0 +1,5 @@ +{ + "semi": true, + "singleQuote": true, + "tabWidth": 4 +} \ No newline at end of file diff --git a/packages/markdown-docx/src/CiceroMarkToOOXML/rules.js b/packages/markdown-docx/src/CiceroMarkToOOXML/rules.js index c432403c..d9ff161f 100644 --- a/packages/markdown-docx/src/CiceroMarkToOOXML/rules.js +++ b/packages/markdown-docx/src/CiceroMarkToOOXML/rules.js @@ -23,9 +23,11 @@ const { sanitizeHtmlChars } = require('./helpers'); * @returns {string} OOXML tag for the text */ const TEXT_RULE = (value) => { - return ` - ${sanitizeHtmlChars(value)} - `; + return ` + + ${sanitizeHtmlChars(value)} + + `; }; /** @@ -35,12 +37,14 @@ const TEXT_RULE = (value) => { * @returns {string} OOXML tag for the emphasised text */ const EMPHASIS_RULE = (value) => { - return ` - - - - ${sanitizeHtmlChars(value)} - `; + return ` + + + + + ${sanitizeHtmlChars(value)} + + `; }; module.exports = { TEXT_RULE, EMPHASIS_RULE }; From 58482a7f4fec95aa29886fb28f1b2870223b3a3e Mon Sep 17 00:00:00 2001 From: k-kumar-01 Date: Fri, 11 Jun 2021 17:17:46 +0530 Subject: [PATCH 9/9] end- refactor: formattingg rules - #397 Signed-off-by: k-kumar-01 --- packages/markdown-docx/.prettierrc.json | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 packages/markdown-docx/.prettierrc.json diff --git a/packages/markdown-docx/.prettierrc.json b/packages/markdown-docx/.prettierrc.json deleted file mode 100644 index 4a3b085d..00000000 --- a/packages/markdown-docx/.prettierrc.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "semi": true, - "singleQuote": true, - "tabWidth": 4 -} \ No newline at end of file