1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | 'use strict';
|
16 |
|
17 | const { NS_PREFIX_CommonMarkModel } = require('@accordproject/markdown-common').CommonMarkModel;
|
18 | const { NS_PREFIX_CiceroMarkModel } = require('@accordproject/markdown-cicero').CiceroMarkModel;
|
19 | const { NS_PREFIX_TemplateMarkModel } = require('@accordproject/markdown-template').TemplateMarkModel;
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | function removeEmptyParagraphs(input) {
|
27 | let nodesWithoutBlankParagraphs = [];
|
28 | input.nodes.forEach(node => {
|
29 | if (node.$class === `${NS_PREFIX_CommonMarkModel}Paragraph` &&
|
30 | node.nodes.length === 1 &&
|
31 | node.nodes[0].$class === `${NS_PREFIX_CommonMarkModel}Text` &&
|
32 | node.nodes[0].text === '') {
|
33 | return;
|
34 | }
|
35 | nodesWithoutBlankParagraphs.push(node);
|
36 | });
|
37 | input.nodes = nodesWithoutBlankParagraphs;
|
38 | return input;
|
39 | }
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | function getText (input) {
|
47 | let result = '';
|
48 |
|
49 | if (input.type === 'paragraph') {
|
50 | result += '\n';
|
51 | }
|
52 |
|
53 | if (input.text) {
|
54 | result += input.text;
|
55 | }
|
56 | if (input.children) {
|
57 | input.children.forEach(node => {
|
58 | result += getText(node);
|
59 | });
|
60 | }
|
61 | return result;
|
62 | }
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 | function quoteString(value) {
|
70 | return '"' + value + '"';
|
71 | }
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 | function handleMarks(slateNode,newNode) {
|
80 | let strong = null;
|
81 | let emph = null;
|
82 | let result = newNode;
|
83 |
|
84 | const isBold = slateNode.bold;
|
85 | const isItalic = slateNode.italic;
|
86 |
|
87 | if (isBold) {
|
88 | strong = {$class : `${NS_PREFIX_CommonMarkModel}Strong`, nodes: []};
|
89 | }
|
90 |
|
91 | if (isItalic) {
|
92 | emph = {$class : `${NS_PREFIX_CommonMarkModel}Emph`, nodes: []};
|
93 | }
|
94 |
|
95 | if(strong) {
|
96 | strong.nodes.push(result);
|
97 | result = strong;
|
98 | }
|
99 | if(emph) {
|
100 | emph.nodes.push(result);
|
101 | result = emph;
|
102 | }
|
103 |
|
104 | return result;
|
105 | }
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 | function handleText(node) {
|
113 | let result = null;
|
114 | const isCode = node.code;
|
115 | if (node.object === 'text' && node.text === '') { return null; }
|
116 | if (isCode) {
|
117 | result = {$class : `${NS_PREFIX_CommonMarkModel}Code`, text: node.text};
|
118 | } else {
|
119 | result = {$class : `${NS_PREFIX_CommonMarkModel}Text`, text : node.text};
|
120 | }
|
121 |
|
122 | return handleMarks(node,result);
|
123 | }
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 | function handleVariable(node) {
|
131 | let result = null;
|
132 |
|
133 | const textNode = node.children[0];
|
134 | node.children = [];
|
135 |
|
136 | const data = node.data;
|
137 |
|
138 | const baseName = Object.prototype.hasOwnProperty.call(data,'format') ? 'FormattedVariable' : (Object.prototype.hasOwnProperty.call(data,'enumValues') ? 'EnumVariable' : 'Variable');
|
139 | const className = `${NS_PREFIX_CiceroMarkModel}${baseName}`;
|
140 |
|
141 | result = {
|
142 | $class : className,
|
143 | name : data.name,
|
144 | value : textNode.text
|
145 | };
|
146 |
|
147 | if (Object.prototype.hasOwnProperty.call(data,'format')) {
|
148 | result.format = data.format;
|
149 | }
|
150 | if (node.data.identifiedBy) {
|
151 | result.identifiedBy = node.data.identifiedBy;
|
152 | result.value = quoteString(result.value);
|
153 | }
|
154 | if (Object.prototype.hasOwnProperty.call(data,'enumValues')) {
|
155 | result.enumValues = data.enumValues;
|
156 | }
|
157 | if (Object.prototype.hasOwnProperty.call(data,'elementType')) {
|
158 | result.elementType = data.elementType;
|
159 | if (result.elementType === 'String') {
|
160 | result.value = quoteString(result.value);
|
161 | }
|
162 | }
|
163 | if (Object.prototype.hasOwnProperty.call(data,'decorators')) {
|
164 | result.decorators = data.decorators;
|
165 | }
|
166 |
|
167 | return handleMarks(node,result);
|
168 | }
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 | function handleVariableDefinition(node) {
|
176 | let result = null;
|
177 |
|
178 | node.children = [];
|
179 |
|
180 | const data = node.data;
|
181 |
|
182 | const baseName = Object.prototype.hasOwnProperty.call(data,'format') ? 'FormattedVariableDefinition' : (Object.prototype.hasOwnProperty.call(data,'enumValues') ? 'EnumVariableDefinition' : 'VariableDefinition');
|
183 | const className = `${NS_PREFIX_TemplateMarkModel}${baseName}`;
|
184 |
|
185 | result = {
|
186 | $class : className,
|
187 | name : data.name,
|
188 | };
|
189 |
|
190 | if (Object.prototype.hasOwnProperty.call(data,'format')) {
|
191 | result.format = data.format;
|
192 | }
|
193 | if (node.data.identifiedBy) {
|
194 | result.identifiedBy = node.data.identifiedBy;
|
195 | }
|
196 | if (Object.prototype.hasOwnProperty.call(data,'enumValues')) {
|
197 | result.enumValues = data.enumValues;
|
198 | }
|
199 | if (Object.prototype.hasOwnProperty.call(data,'elementType')) {
|
200 | result.elementType = data.elementType;
|
201 | }
|
202 | if (Object.prototype.hasOwnProperty.call(data,'decorators')) {
|
203 | result.decorators = data.decorators;
|
204 | }
|
205 |
|
206 | return handleMarks(node,result);
|
207 | }
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 | function handleConditional(node, isTrue, whenTrue, whenFalse) {
|
218 | const data = node.data;
|
219 |
|
220 | let result = {
|
221 | $class : `${NS_PREFIX_CiceroMarkModel}Conditional`,
|
222 | name : data.name,
|
223 | nodes: [],
|
224 | };
|
225 |
|
226 | result.isTrue = isTrue;
|
227 | result.whenTrue = whenTrue;
|
228 | result.whenFalse = whenFalse;
|
229 |
|
230 | if (Object.prototype.hasOwnProperty.call(data,'elementType')) {
|
231 | result.elementType = data.elementType;
|
232 | }
|
233 | if (Object.prototype.hasOwnProperty.call(data,'decorators')) {
|
234 | result.decorators = data.decorators;
|
235 | }
|
236 |
|
237 | return handleMarks(node,result);
|
238 | }
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 | function handleConditionalDefinition(node, whenTrue, whenFalse) {
|
248 | const data = node.data;
|
249 |
|
250 | let result = {
|
251 | $class : `${NS_PREFIX_TemplateMarkModel}ConditionalDefinition`,
|
252 | name : data.name,
|
253 | };
|
254 |
|
255 | result.whenTrue = whenTrue;
|
256 | result.whenFalse = whenFalse;
|
257 |
|
258 | if (Object.prototype.hasOwnProperty.call(data,'elementType')) {
|
259 | result.elementType = data.elementType;
|
260 | }
|
261 | if (Object.prototype.hasOwnProperty.call(data,'decorators')) {
|
262 | result.decorators = data.decorators;
|
263 | }
|
264 |
|
265 | return handleMarks(node,result);
|
266 | }
|
267 |
|
268 |
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 |
|
275 |
|
276 | function handleOptional(node, hasSome, whenSome, whenNone) {
|
277 | const data = node.data;
|
278 |
|
279 | let result = {
|
280 | $class : `${NS_PREFIX_CiceroMarkModel}Optional`,
|
281 | name : data.name,
|
282 | nodes: [],
|
283 | };
|
284 |
|
285 | result.hasSome = hasSome;
|
286 | result.whenSome = whenSome;
|
287 | result.whenNone = whenNone;
|
288 |
|
289 | if (Object.prototype.hasOwnProperty.call(data,'elementType')) {
|
290 | result.elementType = data.elementType;
|
291 | }
|
292 | if (Object.prototype.hasOwnProperty.call(data,'decorators')) {
|
293 | result.decorators = data.decorators;
|
294 | }
|
295 |
|
296 | return handleMarks(node,result);
|
297 | }
|
298 |
|
299 |
|
300 |
|
301 |
|
302 |
|
303 |
|
304 |
|
305 |
|
306 | function handleOptionalDefinition(node, whenSome, whenNone) {
|
307 | const data = node.data;
|
308 |
|
309 | let result = {
|
310 | $class : `${NS_PREFIX_TemplateMarkModel}OptionalDefinition`,
|
311 | name : data.name,
|
312 | };
|
313 |
|
314 | result.whenSome = whenSome;
|
315 | result.whenNone = whenNone;
|
316 |
|
317 | if (Object.prototype.hasOwnProperty.call(data,'elementType')) {
|
318 | result.elementType = data.elementType;
|
319 | }
|
320 | if (Object.prototype.hasOwnProperty.call(data,'decorators')) {
|
321 | result.decorators = data.decorators;
|
322 | }
|
323 |
|
324 | return handleMarks(node,result);
|
325 | }
|
326 |
|
327 |
|
328 |
|
329 |
|
330 |
|
331 |
|
332 | function handleFormula(node) {
|
333 | let result = null;
|
334 |
|
335 | const textNode = node.children[0];
|
336 | node.children = [];
|
337 |
|
338 | const className = `${NS_PREFIX_CiceroMarkModel}Formula`;
|
339 |
|
340 | result = {
|
341 | $class : className,
|
342 | value : textNode.text
|
343 | };
|
344 |
|
345 | const data = node.data;
|
346 | if (Object.prototype.hasOwnProperty.call(data,'elementType')) {
|
347 | result.elementType = data.elementType;
|
348 | }
|
349 | if (Object.prototype.hasOwnProperty.call(data,'decorators')) {
|
350 | result.decorators = data.decorators;
|
351 | }
|
352 | if (Object.prototype.hasOwnProperty.call(data,'dependencies')) {
|
353 | result.dependencies = data.dependencies;
|
354 | }
|
355 | if (Object.prototype.hasOwnProperty.call(data,'code')) {
|
356 | result.code = data.code;
|
357 | }
|
358 | if (Object.prototype.hasOwnProperty.call(data,'name')) {
|
359 | result.name = data.name;
|
360 | }
|
361 |
|
362 | return handleMarks(node,result);
|
363 | }
|
364 |
|
365 |
|
366 |
|
367 |
|
368 |
|
369 |
|
370 | function handleFormulaDefinition(node) {
|
371 | let result = null;
|
372 |
|
373 | node.children = [];
|
374 |
|
375 | const className = `${NS_PREFIX_CiceroMarkModel}Formula`;
|
376 |
|
377 | result = {
|
378 | $class : className,
|
379 | };
|
380 |
|
381 | const data = node.data;
|
382 | if (Object.prototype.hasOwnProperty.call(data,'elementType')) {
|
383 | result.elementType = data.elementType;
|
384 | }
|
385 | if (Object.prototype.hasOwnProperty.call(data,'decorators')) {
|
386 | result.decorators = data.decorators;
|
387 | }
|
388 | if (Object.prototype.hasOwnProperty.call(data,'dependencies')) {
|
389 | result.dependencies = data.dependencies;
|
390 | }
|
391 | if (Object.prototype.hasOwnProperty.call(data,'code')) {
|
392 | result.code = data.code;
|
393 | }
|
394 | if (Object.prototype.hasOwnProperty.call(data,'name')) {
|
395 | result.name = data.name;
|
396 | }
|
397 |
|
398 | return handleMarks(node,result);
|
399 | }
|
400 |
|
401 | module.exports = {
|
402 | removeEmptyParagraphs,
|
403 | getText,
|
404 | quoteString,
|
405 | handleMarks,
|
406 | handleText,
|
407 | handleVariable,
|
408 | handleVariableDefinition,
|
409 | handleConditional,
|
410 | handleConditionalDefinition,
|
411 | handleOptional,
|
412 | handleOptionalDefinition,
|
413 | handleFormula,
|
414 | handleFormulaDefinition,
|
415 | }; |
\ | No newline at end of file |