Skip to content

Commit

Permalink
feat(markdown-docx): add API to return frequency of variables in Cice…
Browse files Browse the repository at this point in the history
…roMark (JSON) - #397 (#411)

Signed-off-by: k-kumar-01 <[email protected]>
  • Loading branch information
K-Kumar-01 authored and algomaster99 committed Jun 17, 2021
1 parent ea1ea32 commit 0e28a30
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions packages/markdown-docx/src/CiceroMarkToOOXML/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ const definedNodes = {
*/
class CiceroMarkToOOXMLTransfomer {
/**
* Declares the OOXML variable
* Declares the OOXML and counter variable.
*/
constructor() {
this.globalOOXML = '';
this.counter = {};
}

/**
Expand All @@ -51,34 +52,42 @@ class CiceroMarkToOOXMLTransfomer {
return node.$class;
}

/**
* Returns the counter holding variable count for a CiceroMark(JSON).
*
* @returns {object} Counter for variables in CiceroMark(JSON)
*/
getCounter() {
return this.counter;
}

/**
* 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) {
getNodes(node, parent = null) {
if (this.getClass(node) === definedNodes.variable) {
const tag = node.name;
const type = node.elementType;
if (Object.prototype.hasOwnProperty.call(counter, tag)) {
counter = {
...counter,
if (Object.prototype.hasOwnProperty.call(this.counter, tag)) {
this.counter = {
...this.counter,
[tag]: {
...counter[tag],
count: ++counter[tag].count,
...this.counter[tag],
count: ++this.counter[tag].count,
},
};
} else {
counter[tag] = {
this.counter[tag] = {
count: 1,
type,
};
}
const value = node.value;
const title = `${tag.toUpperCase()[0]}${tag.substring(1)}${counter[tag].count}`;
const title = `${tag.toUpperCase()[0]}${tag.substring(1)}${this.counter[tag].count}`;
return VARIABLE_RULE(title, tag, value, type);
}

Expand All @@ -96,15 +105,15 @@ class CiceroMarkToOOXMLTransfomer {
if (this.getClass(node) === definedNodes.emphasize) {
let ooxml = '';
node.nodes.forEach(subNode => {
ooxml += this.getNodes(subNode, counter, { class: node.$class });
ooxml += this.getNodes(subNode, { class: node.$class });
});
return ooxml;
}

if (this.getClass(node) === definedNodes.heading) {
let ooxml = '';
node.nodes.forEach(subNode => {
ooxml += this.getNodes(subNode, counter, { class: node.$class, level: node.level });
ooxml += this.getNodes(subNode, { class: node.$class, level: node.level });
});
this.globalOOXML = `
${this.globalOOXML}
Expand All @@ -117,7 +126,7 @@ class CiceroMarkToOOXMLTransfomer {
if (this.getClass(node) === definedNodes.paragraph) {
let ooxml = '';
node.nodes.forEach(subNode => {
ooxml += this.getNodes(subNode, counter);
ooxml += this.getNodes(subNode);
});
this.globalOOXML = `${this.globalOOXML}<w:p>${ooxml}</w:p>`;
}
Expand All @@ -128,14 +137,13 @@ class CiceroMarkToOOXMLTransfomer {
* 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 = '') {
toOOXML(ciceromark, ooxml = '') {
this.globalOOXML = ooxml;
ciceromark.nodes.forEach(node => {
this.getNodes(node, counter);
this.getNodes(node);
});
this.globalOOXML = wrapAroundDefaultDocxTags(this.globalOOXML);
return this.globalOOXML;
Expand Down

0 comments on commit 0e28a30

Please sign in to comment.