Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add text and emphasis transformers - #397 #401

88 changes: 88 additions & 0 deletions packages/markdown-docx/src/CiceroMarkToOOXMLHelpers.js
Original file line number Diff line number Diff line change
@@ -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, '&gt;').replace(/</g, '&lt;');
}

/**
* Wraps OOXML in docx headers.
*
* @param {string} ooxml OOXML to be wrapped
* @returns {string} OOXML wraped in docx headers
*/
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">
<pkg:xmlData>
<Relationships
xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/>
</Relationships>
</pkg:xmlData>
</pkg:part>
<pkg:part pkg:name="/word/document.xml" pkg:contentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml">
<pkg:xmlData>
<w:document
xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas"
xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex"
xmlns:cx1="http://schemas.microsoft.com/office/drawing/2015/9/8/chartex"
xmlns:cx2="http://schemas.microsoft.com/office/drawing/2015/10/21/chartex"
xmlns:cx3="http://schemas.microsoft.com/office/drawing/2016/5/9/chartex"
xmlns:cx4="http://schemas.microsoft.com/office/drawing/2016/5/10/chartex"
xmlns:cx5="http://schemas.microsoft.com/office/drawing/2016/5/11/chartex"
xmlns:cx6="http://schemas.microsoft.com/office/drawing/2016/5/12/chartex"
xmlns:cx7="http://schemas.microsoft.com/office/drawing/2016/5/13/chartex"
xmlns:cx8="http://schemas.microsoft.com/office/drawing/2016/5/14/chartex"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:aink="http://schemas.microsoft.com/office/drawing/2016/ink"
xmlns:am3d="http://schemas.microsoft.com/office/drawing/2017/model3d"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math"
xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing"
xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"
xmlns:w10="urn:schemas-microsoft-com:office:word"
xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml"
xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml"
xmlns:w16cid="http://schemas.microsoft.com/office/word/2016/wordml/cid"
xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex"
xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup"
xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk"
xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml"
xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 w15 w16se w16cid wp14">
<w:body>
${ooxml}
<w:p/>
</w:body>
</w:document>
</pkg:xmlData>
</pkg:part>
</pkg:package>`;

return ooxml;
}

module.exports = { sanitizeHtmlChars, wrapAroundDefaultDocxTags };
39 changes: 39 additions & 0 deletions packages/markdown-docx/src/CiceroMarkToOOXMLRules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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('./CiceroMarkToOOXMLHelpers');

/**
* Inserts text.
*
* @param {string} value Enclosing value of the OOXML tag
* @returns {string} OOXML tag for the text
*/
const TEXT_RULE = (value) => {
return `<w:r><w:t xml:space="preserve">${sanitizeHtmlChars(value)}</w:t></w:r>`;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason you omitted the spaces in these tags and not the tags in wrapAroundDefaultDocxTags?

Copy link
Collaborator Author

@K-Kumar-01 K-Kumar-01 Jun 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@algomaster99 These tags are not used for conversion. So any space between them will act as ' <value>' instead of '<value>'. However, in wrapAroundDefaultDocxTags we do not check for such. So no omission of spaces there.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So any space between them will act as ' ' instead of ''

Any problems with that? This worked fine in the Word add-in. I think the parser we use, xml-js, is also smart enough to ignore such spaces.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@algomaster99
It included the spaces also in values of nodes if it is done similar to add-in. So i dedcided to do it in later way. I also posted pn Slack regarding this. I will also reconfirm it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@algomaster99
Screenshot from 2021-06-11 15-01-12
Changed the EMPHASIS_RULE and failed test

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By value, do you mean - <w:value /> or <w:t>value</w:t>?

I read your post on slack and I thought it was causing some problems for you. But I don't think there is any issue as I just tried on my local with spaces and breaks.

If you think the size of the string (in bytes) is an issue, you can add a package to uglify an OOXML string after transformation is done.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is because you literally put a space before and after your value. I am just asking to format the rest of the nodes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's discuss this in the meeting.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By value, do you mean - <w:value /> or <w:t>value</w:t>?

I mean `<w:t>value</w:t>

I read your post on slack and I thought it was causing some problems for you. But I don't think there is any issue as I just tried on my local with spaces and breaks.

Interesting, as it gave me errors (shown above).

If you think the size of the string (in bytes) is an issue, you can add a package to uglify an OOXML string after transformation is done.

We can have a discussion about this in the meeting.

};

/**
* Inserts emphasised text.
*
* @param {string} value Enclosing value of the OOXML tag
* @returns {string} OOXML tag 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>`;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

w:val="true" is not needed like I reasoned before.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

};

module.exports = { TEXT_RULE, EMPHASIS_RULE };
90 changes: 75 additions & 15 deletions packages/markdown-docx/src/CiceroMarkToOOXMLTransformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,79 @@

'use strict';

const { TEXT_RULE, EMPHASIS_RULE } = require('./CiceroMarkToOOXMLRules');
const { wrapAroundDefaultDocxTags } = require('./CiceroMarkToOOXMLHelpers');

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',
};

/**
* 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',
};

/**
* Declares the OOXML variable
*/
constructor() {
this.globalOOXML = '';
}

/**
* 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 EMPHASIS_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,);
});
this.globalOOXML = `${this.globalOOXML}<w:p>${ooxml}</w:p>`;
}
return '';
}

/**
* Transforms the given CiceroMark JSON to OOXML
Expand All @@ -40,9 +96,13 @@ class CiceroMarkToOOXMLTransfomer {
* @param {string} ooxml Initial OOXML string
* @returns {string} Converted OOXML string i.e. CicecoMark->OOXML
*/
toOOXML(ciceromark, counter, ooxml) {
let globalOOXML = ooxml;
return globalOOXML;
toOOXML(ciceromark, counter, ooxml = '') {
this.globalOOXML = ooxml;
ciceromark.nodes.forEach(node => {
this.getNodes(node, counter);
});
this.globalOOXML = wrapAroundDefaultDocxTags(this.globalOOXML);
return this.globalOOXML;
}
}

Expand Down
39 changes: 39 additions & 0 deletions packages/markdown-docx/src/CiceroMarkToOOXMLTransformer.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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 fs = require('fs');
const chai = require('chai');

const expect = chai.expect;

const OoxmlTransformer = require('./OoxmlTransformer');
const CiceroMarkToOOXMLTransfomer = require('./CiceroMarkToOOXMLTransformer');

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(JSON.parse(paraAndEmphasisCiceroMark));
algomaster99 marked this conversation as resolved.
Show resolved Hide resolved

const ooxmlTransformer = new OoxmlTransformer();
const convertedObject = ooxmlTransformer.toCiceroMark(ooxml);
expect(convertedObject).to.deep.equal(JSON.parse(paraAndEmphasisCiceroMark));
});
});
15 changes: 15 additions & 0 deletions packages/markdown-docx/src/OoxmlTransformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,21 @@ class OoxmlTransformer {
$class: `${NS_PREFIX_CommonMarkModel}Softbreak`,
};
case 'w:r':
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),
],
};
}
}
return [...this.deserializeElements(element.elements)];
case 'w:color':
return element.attributes['w:color'];
Expand Down
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."}]}]}