-
Notifications
You must be signed in to change notification settings - Fork 52
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
Merged
algomaster99
merged 9 commits into
accordproject:algoo-ooxml
from
K-Kumar-01:k-kumar-01/i397/ciceromark-ooxml-transformers
Jun 11, 2021
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9b0decd
feat: add text and emphasis transformers - #397
K-Kumar-01 e43b15a
feat: add text and emphasis transformers - #397
K-Kumar-01 f875a96
feat: add text and emphasis transformers - #397
K-Kumar-01 77a0e00
feat: add text and emphasis transformers - #397
K-Kumar-01 d5f5b36
feat: add text and emphasis transformers - #397
K-Kumar-01 18fb1fd
feat: add text and emphasis transformers - #397
K-Kumar-01 1771d6b
refactor: export transformers from index - #397
K-Kumar-01 6c911c2
refactor: formatting rules - #397
K-Kumar-01 58482a7
end-
K-Kumar-01 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,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>`; | ||
}; | ||
|
||
/** | ||
* 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>`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @algomaster99 |
||
}; | ||
|
||
module.exports = { TEXT_RULE, EMPHASIS_RULE }; |
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
39 changes: 39 additions & 0 deletions
39
packages/markdown-docx/src/CiceroMarkToOOXMLTransformer.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,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)); | ||
}); | ||
}); |
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
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."}]}]} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
?There was a problem hiding this comment.
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, inwrapAroundDefaultDocxTags
we do not check for such. So no omission of spaces there.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@algomaster99
Changed the
EMPHASIS_RULE
and failed testThere was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean `<w:t>value</w:t>
Interesting, as it gave me errors (shown above).
We can have a discussion about this in the meeting.