1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | 'use strict';
|
16 |
|
17 | const { NS_PREFIX_CiceroMarkModel } = require('./externalModels/CiceroMarkModel');
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | class FromCiceroEditVisitor {
|
23 | |
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | static visitChildren(visitor, thing, parameters) {
|
30 | if(thing.nodes) {
|
31 | FromCiceroEditVisitor.visitNodes(visitor, thing.nodes, parameters);
|
32 | }
|
33 | }
|
34 |
|
35 | |
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | static visitNodes(visitor, things, parameters) {
|
42 | things.forEach(node => {
|
43 | node.accept(visitor, parameters);
|
44 | });
|
45 | }
|
46 |
|
47 | |
48 |
|
49 |
|
50 |
|
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 |
|
59 | const clauseText = thing.text;
|
60 |
|
61 |
|
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;
|
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 |
|
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 = '';
|
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;
|
95 | delete thing.tag;
|
96 | delete thing.info;
|
97 | }
|
98 | }
|
99 | }
|
100 | break;
|
101 |
|
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) {
|
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 = '';
|
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 |
|
175 |
|
176 |
|
177 |
|
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 |
|
186 | module.exports = FromCiceroEditVisitor; |
\ | No newline at end of file |