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 | ;
|
16 |
|
17 | const { Stack } = require('@accordproject/markdown-common');
|
18 |
|
19 | /**
|
20 | * Maps the keys in an object
|
21 | * @param {*} obj input object
|
22 | * @param {*} stack stack object
|
23 | * @param {*} options stack object
|
24 | */
|
25 | function mapObject(obj, stack) {
|
26 | switch (obj.$class) {
|
27 | // Strip quotes
|
28 | case 'org.accordproject.ciceromark.Formula':
|
29 | case 'org.accordproject.ciceromark.Conditional':
|
30 | case 'org.accordproject.ciceromark.Optional':
|
31 | case 'org.accordproject.ciceromark.Variable':
|
32 | case 'org.accordproject.ciceromark.FormattedVariable':
|
33 | stack.append({
|
34 | $class: 'org.accordproject.commonmark.Text',
|
35 | text: obj.value.replace(/^"/, '').replace(/"$/, '')
|
36 | });
|
37 | break;
|
38 |
|
39 | // remove these, visit children
|
40 | case 'org.accordproject.commonmark.Document':
|
41 | obj.nodes.forEach(element => {
|
42 | mapObject(element, stack);
|
43 | });
|
44 | break;
|
45 | // otherwise visit all nodes
|
46 | default:
|
47 | if(obj.nodes){
|
48 | let resObj = Object.assign({}, obj);
|
49 | resObj.nodes = [];
|
50 | stack.push(resObj);
|
51 | obj.nodes.forEach(element => {
|
52 | mapObject(element, stack);
|
53 | });
|
54 | stack.pop();
|
55 | } else {
|
56 | stack.append(obj);
|
57 | }
|
58 | break;
|
59 | }
|
60 | }
|
61 |
|
62 | /**
|
63 | * Replaces variable and formulas with text nodes
|
64 | * @param {*} input input object
|
65 | * @param {*} options options object
|
66 | * @returns {*} the modified object
|
67 | */
|
68 | function unquoteVariables(input) {
|
69 | const root = {
|
70 | $class : 'org.accordproject.commonmark.Document',
|
71 | xmlns : input.xmlns,
|
72 | nodes: []
|
73 | };
|
74 | const stack = new Stack();
|
75 | stack.push(root, false);
|
76 | mapObject(input, stack);
|
77 | return root;
|
78 | }
|
79 |
|
80 | module.exports = unquoteVariables; |
\ | No newline at end of file |