|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | + |
| 15 | +'use strict'; |
| 16 | + |
| 17 | +const { TEXT_RULE, EMPHASIS_RULE } = require('./rules'); |
| 18 | +const { wrapAroundDefaultDocxTags } = require('./helpers'); |
| 19 | + |
| 20 | +const definedNodes = { |
| 21 | + computedVariable: 'org.accordproject.ciceromark.ComputedVariable', |
| 22 | + heading: 'org.accordproject.commonmark.Heading', |
| 23 | + item: 'org.accordproject.commonmark.Item', |
| 24 | + list: 'org.accordproject.commonmark.List', |
| 25 | + listBlock: 'org.accordproject.ciceromark.ListBlock', |
| 26 | + paragraph: 'org.accordproject.commonmark.Paragraph', |
| 27 | + softbreak: 'org.accordproject.commonmark.Softbreak', |
| 28 | + text: 'org.accordproject.commonmark.Text', |
| 29 | + variable: 'org.accordproject.ciceromark.Variable', |
| 30 | + emphasize: 'org.accordproject.commonmark.Emph', |
| 31 | +}; |
| 32 | + |
| 33 | +/** |
| 34 | + * Transforms the ciceromark to OOXML |
| 35 | + */ |
| 36 | +class CiceroMarkToOOXMLTransfomer { |
| 37 | + |
| 38 | + |
| 39 | + /** |
| 40 | + * Declares the OOXML variable |
| 41 | + */ |
| 42 | + constructor() { |
| 43 | + this.globalOOXML = ''; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Gets the class of a given CiceroMark node. |
| 48 | + * |
| 49 | + * @param {object} node CiceroMark node entity |
| 50 | + * @returns {string} Class of given node |
| 51 | + */ |
| 52 | + getClass(node) { |
| 53 | + return node.$class; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Gets the OOXML for the given node. |
| 58 | + * |
| 59 | + * @param {object} node Description of node type |
| 60 | + * @param {object} counter Counter for different variables based on node name |
| 61 | + * @param {object} parent Parent object for a node |
| 62 | + * @returns {string} OOXML for the given node |
| 63 | + */ |
| 64 | + getNodes(node, counter, parent = null) { |
| 65 | + if (this.getClass(node) === definedNodes.text) { |
| 66 | + if (parent !== null && parent.class === definedNodes.emphasize) { |
| 67 | + return EMPHASIS_RULE(node.text, true); |
| 68 | + } else { |
| 69 | + return TEXT_RULE(node.text); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + if (this.getClass(node) === definedNodes.emphasize) { |
| 74 | + let ooxml = ''; |
| 75 | + node.nodes.forEach(subNode => { |
| 76 | + ooxml += this.getNodes(subNode, counter, { class: node.$class }); |
| 77 | + }); |
| 78 | + return ooxml; |
| 79 | + } |
| 80 | + |
| 81 | + if (this.getClass(node) === definedNodes.paragraph) { |
| 82 | + let ooxml = ''; |
| 83 | + node.nodes.forEach(subNode => { |
| 84 | + ooxml += this.getNodes(subNode, counter,); |
| 85 | + }); |
| 86 | + this.globalOOXML = `${this.globalOOXML}<w:p>${ooxml}</w:p>`; |
| 87 | + } |
| 88 | + return ''; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Transforms the given CiceroMark JSON to OOXML |
| 93 | + * |
| 94 | + * @param {Object} ciceromark CiceroMark JSON to be converted |
| 95 | + * @param {Object} counter Counter for different variables based on node name |
| 96 | + * @param {string} ooxml Initial OOXML string |
| 97 | + * @returns {string} Converted OOXML string i.e. CicecoMark->OOXML |
| 98 | + */ |
| 99 | + toOOXML(ciceromark, counter, ooxml = '') { |
| 100 | + this.globalOOXML = ooxml; |
| 101 | + ciceromark.nodes.forEach(node => { |
| 102 | + this.getNodes(node, counter); |
| 103 | + }); |
| 104 | + this.globalOOXML = wrapAroundDefaultDocxTags(this.globalOOXML); |
| 105 | + return this.globalOOXML; |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +module.exports = CiceroMarkToOOXMLTransfomer; |
0 commit comments