Skip to content

Commit

Permalink
feat: add text and emphasis transformers - accordproject#397
Browse files Browse the repository at this point in the history
Naming changed to improve readability and understandability
Files renamed for better understanding

Signed-off-by: k-kumar-01 <[email protected]>
  • Loading branch information
K-Kumar-01 committed Jun 10, 2021
1 parent 9b0decd commit e43b15a
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `<pkg:package
xmlns:pkg="http://schemas.microsoft.com/office/2006/xmlPackage">
<pkg:part pkg:name="/_rels/.rels" pkg:contentType="application/vnd.openxmlformats-package.relationships+xml" pkg:padding="512">
Expand Down Expand Up @@ -85,4 +85,4 @@ function wrapOOXML(ooxml){
return ooxml;
}

module.exports = { sanitizeHtmlChars, wrapOOXML };
module.exports = { sanitizeHtmlChars, wrapAroundDefaultDocxTags };
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<w:r><w:rPr><w:i w:val="true" /></w:rPr><w:t>${sanitizeHtmlChars(value)}</w:t></w:r>`;
}
const TEXT_RULE = (value) => {
return `<w:r><w:t xml:space="preserve">${sanitizeHtmlChars(value)}</w:t></w:r>`;
};

module.exports = { TEXT_RULE };
/**
* Inserts emphsaised text.
*
* @param {string} value Text to be rendered
* @returns {string} OOXML for the emphasised text
*/
const EMPHASIS_RULE = (value) => {
return `<w:r><w:rPr><w:i w:val="true" /></w:rPr><w:t>${sanitizeHtmlChars(value)}</w:t></w:r>`;
};

module.exports = { TEXT_RULE, EMPHASIS_RULE };
14 changes: 7 additions & 7 deletions packages/markdown-docx/src/CiceroMarkToOOXMLTransformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -30,15 +30,15 @@ const definedNodes = {
emphasize: 'org.accordproject.commonmark.Emph',
};

let globalOOXML='';
let globalOOXML = '';

/**
* Transforms the ciceromark to OOXML
*/
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
Expand All @@ -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);
}
Expand All @@ -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}<w:p>${ooxml}</w:p>`;
}
Expand All @@ -95,7 +95,7 @@ class CiceroMarkToOOXMLTransfomer {
ciceromark.nodes.forEach(node => {
this.getNodes(node, counter);
});
globalOOXML = wrapOOXML(globalOOXML);
globalOOXML = wrapAroundDefaultDocxTags(globalOOXML);
return globalOOXML;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});
});
73 changes: 0 additions & 73 deletions packages/markdown-docx/test/data/ciceroMark/ciceroMark.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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."}]}]}

0 comments on commit e43b15a

Please sign in to comment.