UNPKG

9.07 kBJavaScriptView Raw
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
17const { NS_PREFIX_CiceroMarkModel } = require('./externalModels/CiceroMarkModel');
18
19/**
20 * Converts a CommonMark DOM to a CiceroMark DOM
21 */
22class FromCiceroEditVisitor {
23 /**
24 * Visits a sub-tree and return CiceroMark DOM
25 * @param {*} visitor the visitor to use
26 * @param {*} thing the node to visit
27 * @param {*} [parameters] optional parameters
28 */
29 static visitChildren(visitor, thing, parameters) {
30 if(thing.nodes) {
31 FromCiceroEditVisitor.visitNodes(visitor, thing.nodes, parameters);
32 }
33 }
34
35 /**
36 * Visits a list of nodes and return the CiceroMark DOM
37 * @param {*} visitor the visitor to use
38 * @param {*} things the list node to visit
39 * @param {*} [parameters] optional parameters
40 */
41 static visitNodes(visitor, things, parameters) {
42 things.forEach(node => {
43 node.accept(visitor, parameters);
44 });
45 }
46
47 /**
48 * Visit a node
49 * @param {*} thing the object being visited
50 * @param {*} parameters the parameters
51 */
52 visit(thing, parameters) {
53 switch(thing.getType()) {
54 case 'CodeBlock': {
55 const tag = thing.tag;
56 if (tag && tag.tagName === 'clause' && tag.attributes.length === 2) {
57 const ciceroMarkTag = NS_PREFIX_CiceroMarkModel + 'Clause';
58 // Remove last new line, needed by CommonMark parser to identify ending code block (\n```)
59 const clauseText = thing.text;
60
61 //console.log('CONTENT! : ' + tag.content);
62 if (FromCiceroEditVisitor.getAttribute(tag.attributes, 'src') &&
63 FromCiceroEditVisitor.getAttribute(tag.attributes, 'clauseid')) {
64 thing.$classDeclaration = parameters.modelManager.getType(ciceroMarkTag);
65 thing.src = FromCiceroEditVisitor.getAttribute(tag.attributes, 'src').value;
66 thing.name = FromCiceroEditVisitor.getAttribute(tag.attributes, 'clauseid').value;
67
68 const commonMark = parameters.commonMark.fromMarkdown(clauseText);
69 thing.nodes = parameters.serializer.fromJSON(commonMark).nodes;
70 FromCiceroEditVisitor.visitNodes(this, thing.nodes, parameters);
71
72 thing.text = null; // Remove text
73 delete thing.tag;
74 delete thing.info;
75 }
76 } else if (tag && tag.tagName === 'list' && tag.attributes.length === 0) {
77 const ciceroMarkTag = NS_PREFIX_CiceroMarkModel + 'ListBlock';
78 // Remove last new line, needed by CommonMark parser to identify ending code block (\n```)
79 const listText = thing.text;
80
81 const commonMark = parameters.commonMark.fromMarkdown(listText);
82 const newNodes = parameters.serializer.fromJSON(commonMark).nodes;
83 if (newNodes.length === 1 && newNodes[0].getType() === 'List') {
84 const listNode = newNodes[0];
85 thing.$classDeclaration = parameters.modelManager.getType(ciceroMarkTag);
86 thing.name = ''; // XXX Hack -- since there is no name in CiceroEdit -- will be filled in later
87 thing.type = listNode.type;
88 thing.start = listNode.start;
89 thing.tight = listNode.tight;
90 thing.delimiter = listNode.delimiter;
91 thing.nodes = listNode.nodes;
92 FromCiceroEditVisitor.visitNodes(this, thing.nodes, parameters);
93
94 thing.text = null; // Remove text
95 delete thing.tag;
96 delete thing.info;
97 }
98 }
99 }
100 break;
101 //case 'HtmlBlock':
102 case 'HtmlInline': {
103 if (thing.tag &&
104 thing.tag.tagName === 'variable' &&
105 (thing.tag.attributes.length === 2 || thing.tag.attributes.length === 3)) {
106 const tag = thing.tag;
107 if (FromCiceroEditVisitor.getAttribute(tag.attributes, 'id') &&
108 FromCiceroEditVisitor.getAttribute(tag.attributes, 'value')) {
109 const format = FromCiceroEditVisitor.getAttribute(tag.attributes, 'format');
110 const ciceroMarkTag = format ? NS_PREFIX_CiceroMarkModel + 'FormattedVariable' : NS_PREFIX_CiceroMarkModel + 'Variable';
111 thing.$classDeclaration = parameters.modelManager.getType(ciceroMarkTag);
112 thing.name = FromCiceroEditVisitor.getAttribute(tag.attributes, 'id').value;
113 thing.value = decodeURIComponent(FromCiceroEditVisitor.getAttribute(tag.attributes, 'value').value);
114 if (format) { // For FormattedVariables
115 thing.format = decodeURIComponent(format.value);
116 }
117 delete thing.tag;
118 delete thing.text;
119 }
120 }
121 if (thing.tag &&
122 thing.tag.tagName === 'if' &&
123 thing.tag.attributes.length === 4) {
124 const tag = thing.tag;
125 if (FromCiceroEditVisitor.getAttribute(tag.attributes, 'id') &&
126 FromCiceroEditVisitor.getAttribute(tag.attributes, 'value') &&
127 FromCiceroEditVisitor.getAttribute(tag.attributes, 'whenTrue') &&
128 FromCiceroEditVisitor.getAttribute(tag.attributes, 'whenFalse')) {
129 const ciceroMarkTag = NS_PREFIX_CiceroMarkModel + 'Conditional';
130 thing.$classDeclaration = parameters.modelManager.getType(ciceroMarkTag);
131 thing.name = FromCiceroEditVisitor.getAttribute(tag.attributes, 'id').value;
132 const valueText = decodeURIComponent(FromCiceroEditVisitor.getAttribute(tag.attributes, 'value').value);
133 const valueNode = parameters.serializer.fromJSON({
134 $class: 'org.accordproject.commonmark.Text',
135 text: valueText,
136 });
137 thing.nodes = [valueNode];
138 const whenTrueText = decodeURIComponent(FromCiceroEditVisitor.getAttribute(tag.attributes, 'whenTrue').value);
139 const whenTrueNodes = whenTrueText ? [parameters.serializer.fromJSON({
140 $class: 'org.accordproject.commonmark.Text',
141 text: whenTrueText,
142 })] : [];
143 thing.isTrue = valueText === whenTrueText;
144 thing.whenTrue = whenTrueNodes;
145 const whenFalseText = decodeURIComponent(FromCiceroEditVisitor.getAttribute(tag.attributes, 'whenFalse').value);
146 const whenFalseNodes = whenFalseText ? [parameters.serializer.fromJSON({
147 $class: 'org.accordproject.commonmark.Text',
148 text: whenFalseText,
149 })] : [];
150 thing.whenFalse = whenFalseNodes;
151 delete thing.tag;
152 delete thing.text;
153 }
154 }
155 if (thing.tag && thing.tag.tagName === 'computed' && thing.tag.attributes.length === 1) {
156 const tag = thing.tag;
157 const ciceroMarkTag = NS_PREFIX_CiceroMarkModel + 'Formula';
158 if (FromCiceroEditVisitor.getAttribute(tag.attributes, 'value')) {
159 thing.$classDeclaration = parameters.modelManager.getType(ciceroMarkTag);
160 thing.name = ''; // XXX Hack -- since there is no name in CiceroEdit -- will be filled in later
161 thing.value = decodeURIComponent(FromCiceroEditVisitor.getAttribute(tag.attributes, 'value').value);
162 delete thing.tag;
163 delete thing.text;
164 }
165 }
166 }
167 break;
168 default:
169 FromCiceroEditVisitor.visitChildren(this, thing, parameters);
170 }
171 }
172
173 /**
174 * Find an attribute from its name
175 * @param {*} attributes - the array of attributes
176 * @param {string} name - the name of the attributes
177 * @return {*} the attribute or undefined
178 */
179 static getAttribute(attributes, name) {
180 const atts = attributes.filter(x => x.name === name);
181 return atts.length === 0 ? null : atts[0];
182 }
183
184}
185
186module.exports = FromCiceroEditVisitor;
\No newline at end of file