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

82 changes: 68 additions & 14 deletions packages/markdown-docx/src/CiceroMarkToOOXMLTransformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
K-Kumar-01 marked this conversation as resolved.
Show resolved Hide resolved
*
* @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);
K-Kumar-01 marked this conversation as resolved.
Show resolved Hide resolved
} 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}<w:p>${ooxml}</w:p>`;
}
return '';
}

/**
* Transforms the given CiceroMark JSON to OOXML
Expand All @@ -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);
K-Kumar-01 marked this conversation as resolved.
Show resolved Hide resolved
return globalOOXML;
}
}
Expand Down
37 changes: 37 additions & 0 deletions packages/markdown-docx/src/CiceroMarkToOOXMLTransformer.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
11 changes: 11 additions & 0 deletions packages/markdown-docx/src/OoxmlTransformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
K-Kumar-01 marked this conversation as resolved.
Show resolved Hide resolved
});
if(emphFound){
Copy link
Member

Choose a reason for hiding this comment

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

emphasisedTextFound

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
I used the default JS and Ts formatters above.
Also, for prettier, there was no config file I didn't use it. I will use the default config for prettier that match the codebase.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, use prettier. It should take care of everything. Just make sure it doesn't change very unrelated things in codebase. For example, removes all the ;. This is why I am asking you to write a config file. You can check the docs on how to write one. It's very straightforward.

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
I have formatted using prettier. Let me know if there are any more changes required.

Copy link
Member

Choose a reason for hiding this comment

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

Sure. Go ahead. Do that in a separate PR though. And then we can merge the changes with this PR.

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 I didn't made any drastic prettier change. Only small area of code needed proper spacing so I don't think it would need a PR.

return {
$class: `${NS_PREFIX_CommonMarkModel}Emph`,
nodes:[...this.deserializeElements(element.elements)]
};
}
}
Copy link
Member

Choose a reason for hiding this comment

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

I will see to this later in the morning.

Copy link
Member

Choose a reason for hiding this comment

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

Improve the spacing by using a good formatter such as prettier. You can integrate its extension in VSCode. Make sure the rules your configure are almost consistent with the entire codebase.

return [...this.deserializeElements(element.elements)];
case 'w:color':
return element.attributes['w:color'];
Expand Down
88 changes: 88 additions & 0 deletions packages/markdown-docx/src/helpers.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 wrapOOXML(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, wrapOOXML };
33 changes: 33 additions & 0 deletions packages/markdown-docx/src/rules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
K-Kumar-01 marked this conversation as resolved.
Show resolved Hide resolved
* 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 `<w:r><w:rPr><w:i w:val="true" /></w:rPr><w:t>${sanitizeHtmlChars(value)}</w:t></w:r>`;
}
return `<w:r><w:t xml:space="preserve">${sanitizeHtmlChars(value)}</w:t></w:r>`;
};

module.exports = { TEXT_RULE };
73 changes: 73 additions & 0 deletions packages/markdown-docx/test/data/ciceroMark/ciceroMark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
K-Kumar-01 marked this conversation as resolved.
Show resolved Hide resolved
* 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':[
K-Kumar-01 marked this conversation as resolved.
Show resolved Hide resolved
{
'$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 };