-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
4233d14
commit a23129f
Showing
9 changed files
with
324 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, '>').replace(/</g, '<'); | ||
} | ||
|
||
/** | ||
* 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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* 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 { TEXT_RULE, EMPHASIS_RULE } = require('./rules'); | ||
const { wrapAroundDefaultDocxTags } = 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', | ||
}; | ||
|
||
/** | ||
* Transforms the ciceromark to OOXML | ||
*/ | ||
class CiceroMarkToOOXMLTransfomer { | ||
|
||
|
||
/** | ||
* 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 | ||
* | ||
* @param {Object} ciceromark CiceroMark JSON to be converted | ||
* @param {Object} counter Counter for different variables based on node name | ||
* @param {string} ooxml Initial OOXML string | ||
* @returns {string} Converted OOXML string i.e. CicecoMark->OOXML | ||
*/ | ||
toOOXML(ciceromark, counter, ooxml = '') { | ||
this.globalOOXML = ooxml; | ||
ciceromark.nodes.forEach(node => { | ||
this.getNodes(node, counter); | ||
}); | ||
this.globalOOXML = wrapAroundDefaultDocxTags(this.globalOOXML); | ||
return this.globalOOXML; | ||
} | ||
} | ||
|
||
module.exports = CiceroMarkToOOXMLTransfomer; |
43 changes: 43 additions & 0 deletions
43
packages/markdown-docx/src/CiceroMarkToOOXML/index.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* 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('.'); | ||
|
||
describe('Perform roundtripping between CiceroMark and OOXML', () => { | ||
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(textAndEmphasisCiceroMark); | ||
|
||
const ooxmlTransformer = new OoxmlTransformer(); | ||
const convertedObject = ooxmlTransformer.toCiceroMark(ooxml); | ||
expect(convertedObject).to.deep.equal(textAndEmphasisCiceroMark); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* 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 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> | ||
`; | ||
}; | ||
|
||
/** | ||
* 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:rPr> | ||
<w:t>${sanitizeHtmlChars(value)}</w:t> | ||
</w:r> | ||
`; | ||
}; | ||
|
||
module.exports = { TEXT_RULE, EMPHASIS_RULE }; |
49 changes: 0 additions & 49 deletions
49
packages/markdown-docx/src/CiceroMarkToOOXMLTransformer.js
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); |
1 change: 1 addition & 0 deletions
1
packages/markdown-docx/test/data/ciceroMark/text-and-emphasis.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."}]}]} |