Skip to content

Commit

Permalink
feat(markdown-docx): add optional transformer - #397
Browse files Browse the repository at this point in the history
transformation logic:OOXML<->CiceroMark
tests for optional
update tests for other(xml changes in variable and clause)

Signed-off-by: k-kumar-01 <[email protected]>
  • Loading branch information
K-Kumar-01 committed Aug 10, 2021
1 parent 12cf1a1 commit 9c00a23
Show file tree
Hide file tree
Showing 8 changed files with 895 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
<w:sz w:val="24"/>
</w:rPr>
<w:alias w:val="Shipper1 | org.accordproject.organization.Organization"/>
<w:tag w:val="shipper"/>
<w:tag w:val="org.accordproject.ciceromark.Variable----shipper"/>
</w:sdtPr>
<w:sdtContent>
<w:r>
Expand Down Expand Up @@ -105,7 +105,7 @@
<w:sz w:val="24"/>
</w:rPr>
<w:alias w:val="Receiver1 | org.accordproject.organization.Organization"/>
<w:tag w:val="receiver"/>
<w:tag w:val="org.accordproject.ciceromark.Variable----receiver"/>
</w:sdtPr>
<w:sdtContent>
<w:r>
Expand All @@ -129,7 +129,7 @@
<w:sz w:val="24"/>
</w:rPr>
<w:alias w:val="Deliverable1 | String"/>
<w:tag w:val="deliverable"/>
<w:tag w:val="org.accordproject.ciceromark.Variable----deliverable"/>
</w:sdtPr>
<w:sdtContent>
<w:r>
Expand Down Expand Up @@ -163,7 +163,7 @@
<w:sz w:val="24"/>
</w:rPr>
<w:alias w:val="Receiver2 | org.accordproject.organization.Organization"/>
<w:tag w:val="receiver"/>
<w:tag w:val="org.accordproject.ciceromark.Variable----receiver"/>
</w:sdtPr>
<w:sdtContent>
<w:r>
Expand All @@ -187,7 +187,7 @@
<w:sz w:val="24"/>
</w:rPr>
<w:alias w:val="Shipper2 | org.accordproject.organization.Organization"/>
<w:tag w:val="shipper"/>
<w:tag w:val="org.accordproject.ciceromark.Variable----shipper"/>
</w:sdtPr>
<w:sdtContent>
<w:r>
Expand Down Expand Up @@ -221,7 +221,7 @@
<w:sz w:val="24"/>
</w:rPr>
<w:alias w:val="Deliverable2 | String"/>
<w:tag w:val="deliverable"/>
<w:tag w:val="org.accordproject.ciceromark.Variable----deliverable"/>
</w:sdtPr>
<w:sdtContent>
<w:r>
Expand Down Expand Up @@ -263,7 +263,7 @@
<w:sz w:val="24"/>
</w:rPr>
<w:alias w:val="Receiver3 | org.accordproject.organization.Organization"/>
<w:tag w:val="receiver"/>
<w:tag w:val="org.accordproject.ciceromark.Variable----receiver"/>
</w:sdtPr>
<w:sdtContent>
<w:r>
Expand All @@ -287,7 +287,7 @@
<w:sz w:val="24"/>
</w:rPr>
<w:alias w:val="BusinessDays1 | Long"/>
<w:tag w:val="businessDays"/>
<w:tag w:val="org.accordproject.ciceromark.Variable----businessDays"/>
</w:sdtPr>
<w:sdtContent>
<w:r>
Expand Down Expand Up @@ -321,7 +321,7 @@
<w:sz w:val="24"/>
</w:rPr>
<w:alias w:val="Deliverable3 | String"/>
<w:tag w:val="deliverable"/>
<w:tag w:val="org.accordproject.ciceromark.Variable----deliverable"/>
</w:sdtPr>
<w:sdtContent>
<w:r>
Expand Down Expand Up @@ -349,7 +349,7 @@
<w:sz w:val="24"/>
</w:rPr>
<w:alias w:val="Shipper3 | org.accordproject.organization.Organization"/>
<w:tag w:val="shipper"/>
<w:tag w:val="org.accordproject.ciceromark.Variable----shipper"/>
</w:sdtPr>
<w:sdtContent>
<w:r>
Expand Down Expand Up @@ -377,7 +377,7 @@
<w:sz w:val="24"/>
</w:rPr>
<w:alias w:val="Deliverable4 | String"/>
<w:tag w:val="deliverable"/>
<w:tag w:val="org.accordproject.ciceromark.Variable----deliverable"/>
</w:sdtPr>
<w:sdtContent>
<w:r>
Expand Down Expand Up @@ -425,7 +425,7 @@
<w:sz w:val="24"/>
</w:rPr>
<w:alias w:val="Deliverable5 | String"/>
<w:tag w:val="deliverable"/>
<w:tag w:val="org.accordproject.ciceromark.Variable----deliverable"/>
</w:sdtPr>
<w:sdtContent>
<w:r>
Expand Down Expand Up @@ -453,7 +453,7 @@
<w:sz w:val="24"/>
</w:rPr>
<w:alias w:val="Shipper4 | org.accordproject.organization.Organization"/>
<w:tag w:val="shipper"/>
<w:tag w:val="org.accordproject.ciceromark.Variable----shipper"/>
</w:sdtPr>
<w:sdtContent>
<w:r>
Expand Down Expand Up @@ -487,7 +487,7 @@
<w:sz w:val="24"/>
</w:rPr>
<w:alias w:val="Attachment1 | String"/>
<w:tag w:val="attachment"/>
<w:tag w:val="org.accordproject.ciceromark.Variable----attachment"/>
</w:sdtPr>
<w:sdtContent>
<w:r>
Expand Down
70 changes: 56 additions & 14 deletions packages/markdown-docx/src/ToCiceroMarkVisitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ class ToCiceroMarkVisitor {
// All the nodes generated from given OOXML
this.nodes = [];

// contains the realtionship part of a given OOXML
// contains the relationship part of a given OOXML
this.relationshipXML = [];

// contains the nodes present in a conditional or optional OOXML
this.conditionalOrOptionalNodes = [];
}

/**
Expand Down Expand Up @@ -64,7 +67,7 @@ class ToCiceroMarkVisitor {
getName(variableProperties) {
for (const property of variableProperties) {
if (property.name === 'w:tag') {
return property.attributes['w:val'];
return property.attributes['w:val'].split('----')[1];
}
}
}
Expand All @@ -87,19 +90,16 @@ class ToCiceroMarkVisitor {
}

/**
* Gets the node type based on the color property.
* Gets the node type based on the tag value.
*
* @param {Array} properties the variable elements
* @returns {string} the type of the node
*/
getNodeType(properties) {
let nodeType = TRANSFORMED_NODES.variable;
for (const property of properties) {
if (property.name === 'w15:color') {
// eg. "Shipper1 | org.accordproject.organization.Organization"
if (property.attributes['w:val'] === '99CCFF') {
nodeType = TRANSFORMED_NODES.clause;
}
if (property.name === 'w:tag') {
return property.attributes['w:val'].split('----')[0];
}
}
return nodeType;
Expand Down Expand Up @@ -217,6 +217,17 @@ class ToCiceroMarkVisitor {
$class: nodeInformation.properties[nodePropertyIndex],
nodes: [ciceroMarkNode],
};
if (nodeInformation.properties[nodePropertyIndex] === TRANSFORMED_NODES.optional) {
let currentNodes = [...ciceroMarkNode.nodes];
ciceroMarkNode = {
$class: TRANSFORMED_NODES.optional,
...nodeInformation.optionalProperties,
whenSome: currentNodes,
whenNone: [],
hasSome: true,
nodes: currentNodes,
};
}
if (nodeInformation.properties[nodePropertyIndex] === TRANSFORMED_NODES.link) {
ciceroMarkNode.title = '';
for (const relationshipElement of this.relationshipXML) {
Expand Down Expand Up @@ -274,12 +285,19 @@ class ToCiceroMarkVisitor {
...rootNode.nodes[rootNodesLength - 1].nodes,
constructedNode,
];
if (propertiesCurrent[commonPropertiesLength - 1] === TRANSFORMED_NODES.optional) {
rootNode.nodes[rootNodesLength - 1].whenSome = rootNode.nodes[rootNodesLength - 1].nodes;
}
} else if (commonPropertiesLength === 2) {
const subNodeLength = rootNode.nodes[rootNodesLength - 1].nodes.length;
rootNode.nodes[rootNodesLength - 1].nodes[subNodeLength - 1].nodes = [
...rootNode.nodes[rootNodesLength - 1].nodes[subNodeLength - 1].nodes,
constructedNode,
];
if (propertiesCurrent[commonPropertiesLength - 1] === TRANSFORMED_NODES.optional) {
rootNode.nodes[rootNodesLength - 1].nodes[subNodeLength - 1].whenSome =
rootNode.nodes[rootNodesLength - 1].nodes[subNodeLength - 1].nodes;
}
} else if (commonPropertiesLength === 3) {
const subNodeLength = rootNode.nodes[rootNodesLength - 1].nodes.length;
const deepSubNodeLength = rootNode.nodes[rootNodesLength - 1].nodes[subNodeLength - 1].nodes.length;
Expand All @@ -288,6 +306,14 @@ class ToCiceroMarkVisitor {
.nodes,
constructedNode,
];
if (propertiesCurrent[commonPropertiesLength - 1] === TRANSFORMED_NODES.optional) {
rootNode.nodes[rootNodesLength - 1].nodes[subNodeLength - 1].nodes[
deepSubNodeLength - 1
].whenSome =
rootNode.nodes[rootNodesLength - 1].nodes[subNodeLength - 1].nodes[
deepSubNodeLength - 1
].nodes;
}
}
}
this.JSONXML = [];
Expand Down Expand Up @@ -342,6 +368,10 @@ class ToCiceroMarkVisitor {
} else if (runTimeNodes.name === 'w:t') {
if (calledBy === TRANSFORMED_NODES.codeBlock) {
ooxmlTagTextValue += runTimeNodes.elements ? runTimeNodes.elements[0].text : '';
} else if (calledBy === TRANSFORMED_NODES.optional) {
ooxmlTagTextValue = runTimeNodes.elements ? runTimeNodes.elements[0].text : ' ';
nodeInformation.value = ooxmlTagTextValue;
this.conditionalOrOptionalNodes = [...this.conditionalOrOptionalNodes, nodeInformation];
} else {
ooxmlTagTextValue = runTimeNodes.elements ? runTimeNodes.elements[0].text : ' ';
nodeInformation.value = ooxmlTagTextValue;
Expand All @@ -351,7 +381,11 @@ class ToCiceroMarkVisitor {
ooxmlTagTextValue += '\n';
} else if (runTimeNodes.name === 'w:sym') {
nodeInformation.nodeType = TRANSFORMED_NODES.softbreak;
this.JSONXML = [...this.JSONXML, nodeInformation];
if (calledBy === TRANSFORMED_NODES.optional) {
this.conditionalOrOptionalNodes = [...this.conditionalOrOptionalNodes, nodeInformation];
} else {
this.JSONXML = [...this.JSONXML, nodeInformation];
}
}
}
return ooxmlTagTextValue;
Expand Down Expand Up @@ -469,14 +503,22 @@ class ToCiceroMarkVisitor {
nodes,
};
this.nodes = [...this.nodes, clauseNode];
} else if (nodeInformation.nodeType === TRANSFORMED_NODES.optional) {
this.traverseElements(variableSubNodes.elements, TRANSFORMED_NODES.optional);

for (let optionalNode of this.conditionalOrOptionalNodes) {
optionalNode.properties = [TRANSFORMED_NODES.optional, ...optionalNode.properties];
optionalNode.optionalProperties = {
elementType: nodeInformation.elementType,
name: nodeInformation.name,
};
this.JSONXML = [...this.JSONXML, { ...optionalNode }];
}
this.conditionalOrOptionalNodes = [];
} else {
for (const variableContentNodes of variableSubNodes.elements) {
if (variableContentNodes.name === 'w:r') {
this.fetchFormattingProperties(
variableContentNodes,
TRANSFORMED_NODES.paragraph,
nodeInformation
);
this.fetchFormattingProperties(variableContentNodes, parent, nodeInformation);
}
}
}
Expand Down
Loading

0 comments on commit 9c00a23

Please sign in to comment.