1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | 'use strict';
|
16 |
|
17 | var dayjs = require('dayjs');
|
18 | var utc = require('dayjs/plugin/utc');
|
19 | dayjs.extend(utc);
|
20 | var {
|
21 | ModelManager,
|
22 | Factory,
|
23 | Serializer,
|
24 | Introspector
|
25 | } = require('@accordproject/concerto-core');
|
26 | var {
|
27 | CommonMarkModel
|
28 | } = require('@accordproject/markdown-common').CommonMarkModel;
|
29 | var {
|
30 | CiceroMarkModel
|
31 | } = require('@accordproject/markdown-cicero').CiceroMarkModel;
|
32 | var {
|
33 | ConcertoMetaModel
|
34 | } = require('@accordproject/markdown-cicero').ConcertoMetaModel;
|
35 | var TemplateMarkModel = require('./externalModels/TemplateMarkModel').TemplateMarkModel;
|
36 | var normalizeNLs = require('./normalize').normalizeNLs;
|
37 | var TypeVisitor = require('./TypeVisitor');
|
38 | var FormulaVisitor = require('./FormulaVisitor');
|
39 | var MarkdownIt = require('markdown-it');
|
40 | var MarkdownItTemplate = require('@accordproject/markdown-it-template');
|
41 | var FromMarkdownIt = require('@accordproject/markdown-common').FromMarkdownIt;
|
42 | var templaterules = require('./templaterules');
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 | function mkTemplateMarkManager(options) {
|
51 | var result = {};
|
52 | result.modelManager = new ModelManager(options);
|
53 | result.modelManager.addCTOModel(CommonMarkModel, 'commonmark.cto');
|
54 | result.modelManager.addCTOModel(ConcertoMetaModel, 'metamodel.cto');
|
55 | result.modelManager.addCTOModel(CiceroMarkModel, 'ciceromark.cto');
|
56 | result.modelManager.addCTOModel(TemplateMarkModel, 'templatemark.cto');
|
57 | result.factory = new Factory(result.modelManager);
|
58 | result.serializer = new Serializer(result.factory, result.modelManager, {
|
59 | utcOffset: 0
|
60 | });
|
61 | return result;
|
62 | }
|
63 | var templateMarkManager = mkTemplateMarkManager();
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 | function instanceOf(classDeclaration, fqt) {
|
75 |
|
76 | if (classDeclaration.getFullyQualifiedName() === fqt) {
|
77 | return true;
|
78 | }
|
79 |
|
80 | var superTypeDeclaration = classDeclaration.getSuperTypeDeclaration();
|
81 | while (superTypeDeclaration) {
|
82 | if (superTypeDeclaration.getFullyQualifiedName() === fqt) {
|
83 | return true;
|
84 | }
|
85 | superTypeDeclaration = superTypeDeclaration.getSuperTypeDeclaration();
|
86 | }
|
87 | return false;
|
88 | }
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 | function findTemplateModel(introspector, templateKind) {
|
98 | var modelType = 'org.accordproject.contract.Contract';
|
99 | if (templateKind !== 'contract') {
|
100 | modelType = 'org.accordproject.contract.Clause';
|
101 | }
|
102 | var templateModels = introspector.getClassDeclarations().filter(item => {
|
103 | return !item.isAbstract() && instanceOf(item, modelType);
|
104 | });
|
105 | if (templateModels.length > 1) {
|
106 | throw new Error("Found multiple instances of ".concat(modelType, ". The model for the template must contain a single asset that extends ").concat(modelType, "."));
|
107 | } else if (templateModels.length === 0) {
|
108 | throw new Error("Failed to find an asset that extends ".concat(modelType, ". The model for the template must contain a single asset that extends ").concat(modelType, "."));
|
109 | } else {
|
110 | return templateModels[0];
|
111 | }
|
112 | }
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 | function findElementModel(introspector, elementType) {
|
122 | return introspector.getClassDeclaration(elementType);
|
123 | }
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 | function templateMarkTypingGen(template, introspector, model, templateKind, options) {
|
136 | var input = templateMarkManager.serializer.fromJSON(template, options);
|
137 | var parameters = {
|
138 | templateMarkModelManager: templateMarkManager.modelManager,
|
139 | introspector: introspector,
|
140 | model: model,
|
141 | kind: templateKind
|
142 | };
|
143 | var visitor = new TypeVisitor();
|
144 | input.accept(visitor, parameters);
|
145 | var result = Object.assign({}, templateMarkManager.serializer.toJSON(input, options));
|
146 |
|
147 |
|
148 | var fvisitor = new FormulaVisitor();
|
149 | result = fvisitor.calculateDependencies(templateMarkManager.modelManager.serializer, result, options);
|
150 | return result;
|
151 | }
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 | function templateMarkTyping(template, modelManager, templateKind) {
|
161 | var introspector = new Introspector(modelManager);
|
162 | var model = findTemplateModel(introspector, templateKind);
|
163 | return templateMarkTypingGen(template, introspector, model, templateKind);
|
164 | }
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 | function templateMarkTypingFromType(template, modelManager, elementType) {
|
174 | var introspector = new Introspector(modelManager);
|
175 | var model = findElementModel(introspector, elementType);
|
176 | var rootNode = {
|
177 | '$class': 'org.accordproject.commonmark.Document',
|
178 | 'xmlns': 'http://commonmark.org/xml/1.0',
|
179 | 'nodes': [{
|
180 | '$class': 'org.accordproject.templatemark.ContractDefinition',
|
181 | 'name': 'top',
|
182 | 'nodes': template
|
183 | }]
|
184 | };
|
185 | var rootNodeTyped = templateMarkTypingGen(rootNode, introspector, model, 'clause');
|
186 | return rootNodeTyped.nodes[0].nodes;
|
187 | }
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 | function templateToTokens(input) {
|
195 | var norm = normalizeNLs(input);
|
196 | var parser = new MarkdownIt({
|
197 | html: true
|
198 | }).use(MarkdownItTemplate);
|
199 | return parser.parse(norm, {});
|
200 | }
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 | function tokensToUntypedTemplateMarkGen(tokenStream) {
|
208 | var fromMarkdownIt = new FromMarkdownIt(templaterules);
|
209 | var partialTemplate = fromMarkdownIt.toCommonMark(tokenStream);
|
210 | var result = templateMarkManager.serializer.toJSON(templateMarkManager.serializer.fromJSON(partialTemplate));
|
211 | return result.nodes;
|
212 | }
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 | function tokensToUntypedTemplateMark(tokenStream, templateKind) {
|
221 | var partialTemplate = tokensToUntypedTemplateMarkGen(tokenStream);
|
222 | if (templateKind === 'contract') {
|
223 | return {
|
224 | '$class': 'org.accordproject.commonmark.Document',
|
225 | 'xmlns': 'http://commonmark.org/xml/1.0',
|
226 | 'nodes': [{
|
227 | '$class': 'org.accordproject.templatemark.ContractDefinition',
|
228 | 'name': 'top',
|
229 | 'nodes': partialTemplate
|
230 | }]
|
231 | };
|
232 | } else {
|
233 | return {
|
234 | '$class': 'org.accordproject.commonmark.Document',
|
235 | 'xmlns': 'http://commonmark.org/xml/1.0',
|
236 | 'nodes': [{
|
237 | '$class': 'org.accordproject.templatemark.ClauseDefinition',
|
238 | 'name': 'top',
|
239 | 'nodes': partialTemplate
|
240 | }]
|
241 | };
|
242 | }
|
243 | }
|
244 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 | function tokensToUntypedTemplateMarkFragment(tokenStream) {
|
252 | var partialTemplate = tokensToUntypedTemplateMarkGen(tokenStream);
|
253 | return {
|
254 | '$class': 'org.accordproject.commonmark.Document',
|
255 | 'xmlns': 'http://commonmark.org/xml/1.0',
|
256 | 'nodes': [{
|
257 | '$class': 'org.accordproject.templatemark.ClauseDefinition',
|
258 | 'name': 'top',
|
259 | 'nodes': partialTemplate
|
260 | }]
|
261 | };
|
262 | }
|
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 | function generateJSON(modelManager, type) {
|
270 | var factory = new Factory(modelManager);
|
271 | var serializer = new Serializer(factory, modelManager);
|
272 | switch (type) {
|
273 | case 'DateTime':
|
274 | return dayjs.utc();
|
275 | case 'Integer':
|
276 | return 0;
|
277 | case 'Long':
|
278 | return 0;
|
279 | case 'Double':
|
280 | return 0.0;
|
281 | case 'Boolean':
|
282 | return false;
|
283 | case 'String':
|
284 | return '';
|
285 | default:
|
286 | {
|
287 | var classDeclaration = modelManager.getType(type);
|
288 | if (classDeclaration.isEnum()) {
|
289 | throw new Error('Cannot generate JSON for an enumerated type directly, the type should be contained in Concept, Asset, Transaction or Event declaration');
|
290 | }
|
291 | var ns = classDeclaration.getNamespace();
|
292 | var name = classDeclaration.getName();
|
293 | var factoryOptions = {
|
294 | includeOptionalFields: true,
|
295 | generate: true
|
296 | };
|
297 | if (classDeclaration.isConcept()) {
|
298 | var concept = factory.newConcept(ns, name, null, factoryOptions);
|
299 | return serializer.toJSON(concept);
|
300 | }
|
301 | var resource = factory.newResource(ns, name, 'resource1', null, factoryOptions);
|
302 | return serializer.toJSON(resource);
|
303 | }
|
304 | }
|
305 | }
|
306 | module.exports.findTemplateModel = findTemplateModel;
|
307 | module.exports.templateMarkManager = templateMarkManager;
|
308 | module.exports.templateToTokens = templateToTokens;
|
309 | module.exports.tokensToUntypedTemplateMarkFragment = tokensToUntypedTemplateMarkFragment;
|
310 | module.exports.tokensToUntypedTemplateMark = tokensToUntypedTemplateMark;
|
311 | module.exports.templateMarkTyping = templateMarkTyping;
|
312 | module.exports.templateMarkTypingFromType = templateMarkTypingFromType;
|
313 | module.exports.generateJSON = generateJSON; |
\ | No newline at end of file |