UNPKG

9.49 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
17var _throwParseException = require('./errorutil')._throwParseException;
18var {
19 templateMarkManager,
20 templateToTokens,
21 tokensToUntypedTemplateMark,
22 templateMarkTyping
23} = require('./templatemarkutil');
24var ParserManager = require('./parsermanager');
25var normalizeToMarkdownCicero = require('./normalize').normalizeToMarkdownCicero;
26var normalizeFromMarkdownCicero = require('./normalize').normalizeFromMarkdownCicero;
27var ToMarkdownTemplateVisitor = require('./ToMarkdownTemplateVisitor');
28var ToCiceroMarkVisitor = require('./ToCiceroMarkVisitor');
29
30/**
31 * Support for CiceroMark Templates
32 */
33class TemplateMarkTransformer {
34 /**
35 * Constructor
36 * @param {object} parsingTable - optional parsing table
37 */
38 constructor() {
39 var parsingTable = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
40 this.parsingTable = parsingTable;
41 }
42
43 /**
44 * Converts a template string to a token stream
45 * @param {object} templateInput the template template
46 * @returns {object} the token stream
47 */
48 toTokens(templateInput) {
49 return templateToTokens(templateInput.content);
50 }
51
52 /**
53 * Converts a template token strean string to a TemplateMark DOM
54 * @param {object} tokenStream the template token stream
55 * @param {object} modelManager - the model manager for this template
56 * @param {string} templateKind - either 'clause' or 'contract'
57 * @param {object} [options] configuration options
58 * @param {boolean} [options.verbose] verbose output
59 * @returns {object} the result of parsing
60 */
61 tokensToMarkdownTemplate(tokenStream, modelManager, templateKind, options) {
62 var template = tokensToUntypedTemplateMark(tokenStream, templateKind);
63 if (options && options.verbose) {
64 console.log('===== Untyped TemplateMark ');
65 console.log(JSON.stringify(template, null, 2));
66 }
67 var typedTemplate = templateMarkTyping(template, modelManager, templateKind);
68 if (options && options.verbose) {
69 console.log('===== TemplateMark ');
70 console.log(JSON.stringify(typedTemplate, null, 2));
71 }
72 return typedTemplate;
73 }
74
75 /**
76 * Converts a markdown string to a TemplateMark DOM
77 * @param {{fileName:string,content:string}} templateInput the template template
78 * @param {object} modelManager - the model manager for this template
79 * @param {string} templateKind - either 'clause' or 'contract'
80 * @param {object} [options] configuration options
81 * @param {boolean} [options.verbose] verbose output
82 * @returns {object} the result of parsing
83 */
84 fromMarkdownTemplate(templateInput, modelManager, templateKind, options) {
85 if (!modelManager) {
86 throw new Error('Cannot parse without template model');
87 }
88 var tokenStream = this.toTokens(templateInput);
89 if (options && options.verbose) {
90 console.log('===== MarkdownIt Tokens ');
91 console.log(JSON.stringify(tokenStream, null, 2));
92 }
93 return this.tokensToMarkdownTemplate(tokenStream, modelManager, templateKind, options);
94 }
95
96 /**
97 * Converts a TemplateMark DOM to a template markdown string
98 * @param {object} input TemplateMark DOM
99 * @returns {string} the template markdown text
100 */
101 toMarkdownTemplate(input) {
102 var visitor = new ToMarkdownTemplateVisitor();
103 return visitor.toMarkdownTemplate(templateMarkManager.serializer, input);
104 }
105
106 /**
107 * Parse a CiceroMark DOM against a TemplateMark DOM
108 * @param {{fileName:string,content:string}} input the ciceromark input
109 * @param {object} parserManager - the parser manager for this template
110 * @param {object} [options] configuration options
111 * @param {boolean} [options.verbose] verbose output
112 * @returns {object} the result of parsing
113 */
114 dataFromCiceroMark(input, parserManager, options) {
115 var serializer = parserManager.getSerializer();
116 var parser = parserManager.getParser();
117
118 // Load the markdown input
119 var markdown = normalizeToMarkdownCicero(input.content);
120 var markdownFileName = input.fileName;
121
122 // Parse the markdown
123 var result;
124 try {
125 result = parser.parse(markdown);
126 } catch (err) {
127 _throwParseException(markdown, err.message, markdownFileName);
128 }
129 if (result.status) {
130 var utcOffset = parserManager.getUtcOffset();
131 var output = serializer.toJSON(serializer.fromJSON(result.value, {
132 utcOffset
133 }), {
134 utcOffset
135 });
136 return output;
137 } else {
138 _throwParseException(markdown, result, markdownFileName);
139 }
140 }
141
142 /**
143 * Parse a CiceroMark DOM against a TemplateMark DOM
144 * @param {object} input the CiceroMark DOM
145 * @param {object} templateMark the templatemark template
146 * @param {object} modelManager - the model manager for this template
147 * @param {string} templateKind - either 'clause' or 'contract'
148 * @param {string} currentTime - the definition of 'now'
149 * @param {number} utcOffset - the UTC offset
150 * @param {object} [options] configuration options
151 * @param {boolean} [options.verbose] verbose output
152 * @returns {object} the result of parsing
153 */
154 fromCiceroMark(input, templateMark, modelManager, templateKind, currentTime, utcOffset, options) {
155 // Construct the template parser
156 var parserManager = new ParserManager(modelManager, this.parsingTable, templateKind, options);
157 parserManager.setCurrentTime(currentTime, utcOffset);
158 parserManager.setTemplateMark(templateMark);
159 parserManager.buildParser();
160 return this.dataFromCiceroMark(input, parserManager, options);
161 }
162
163 /**
164 * Parse a markdown cicero file to data
165 * @param {{fileName:string,content:string}} markdownInput the markdown input
166 * @param {{fileName:string,content:string}} templateInput the template template
167 * @param {object} modelManager - the model manager for this template
168 * @param {string} templateKind - either 'clause' or 'contract'
169 * @param {string} currentTime - the definition of 'now'
170 * @param {number} utcOffset - the UTC offset
171 * @param {object} [options] configuration options
172 * @param {boolean} [options.verbose] verbose output
173 * @returns {object} the result of parsing
174 */
175 fromMarkdownCicero(markdownInput, templateInput, modelManager, templateKind, currentTime, utcOffset, options) {
176 // Translate template to TemplateMark
177 var typedTemplate = this.fromMarkdownTemplate(templateInput, modelManager, templateKind, options);
178
179 // Load the markdown input
180 var ciceroMark = {
181 fileName: markdownInput.fileName,
182 content: normalizeFromMarkdownCicero(markdownInput.content)
183 };
184 return this.fromCiceroMark(ciceroMark, typedTemplate, modelManager, templateKind, currentTime, utcOffset, options);
185 }
186
187 /**
188 * Draft a CiceroMark DOM from a TemplateMarkDOM
189 * @param {*} data the contract/clause data input
190 * @param {*} parserManager - the parser manager for this template
191 * @param {string} templateKind - either 'clause' or 'contract'
192 * @param {object} [options] configuration options
193 * @param {boolean} [options.verbose] verbose output
194 * @returns {object} the result
195 */
196 draftCiceroMark(data, parserManager, templateKind, options) {
197 var parameters = {
198 parserManager: parserManager,
199 templateMarkModelManager: templateMarkManager.modelManager,
200 templateMarkSerializer: templateMarkManager.serializer,
201 fullData: data,
202 data: data,
203 kind: templateKind,
204 currentTime: parserManager.getCurrentTime()
205 };
206 var input = templateMarkManager.serializer.fromJSON(parserManager.getTemplateMark());
207 var visitor = new ToCiceroMarkVisitor();
208 input.accept(visitor, parameters);
209 var result = Object.assign({}, templateMarkManager.serializer.toJSON(input));
210 return result;
211 }
212
213 /**
214 * Instantiate a CiceroMark DOM from a TemplateMarkDOM
215 * @param {*} data the contract/clause data input
216 * @param {*} templateMark - the TemplateMark DOM
217 * @param {object} modelManager - the model manager for this template
218 * @param {string} templateKind - either 'clause' or 'contract'
219 * @param {string} currentTime - the definition of 'now'
220 * @param {number} utcOffset - the UTC offset
221 * @param {object} [options] configuration options
222 * @param {boolean} [options.verbose] verbose output
223 * @returns {object} the result
224 */
225 instantiateCiceroMark(data, templateMark, modelManager, templateKind, currentTime, utcOffset, options) {
226 // Construct the template parser
227 var parserManager = new ParserManager(modelManager, this.parsingTable, templateKind);
228 parserManager.setCurrentTime(currentTime, utcOffset);
229 parserManager.setTemplateMark(templateMark);
230 return this.draftCiceroMark(data, parserManager, templateKind, options);
231 }
232
233 /**
234 * Get TemplateMark serializer
235 * @return {*} templatemark serializer
236 */
237 getSerializer() {
238 return templateMarkManager.serializer;
239 }
240}
241module.exports = TemplateMarkTransformer;
\No newline at end of file