From e43b15ab1feae539d1136ed2375a77736fc4483c Mon Sep 17 00:00:00 2001 From: k-kumar-01 Date: Fri, 11 Jun 2021 01:28:04 +0530 Subject: [PATCH] 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