1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | 'use strict';
|
16 |
|
17 | const { ModelManager, Factory, Serializer } = require('@accordproject/concerto-core');
|
18 |
|
19 | const MarkdownIt = require('markdown-it');
|
20 | const MarkdownItCicero = require('@accordproject/markdown-it-cicero');
|
21 | const FromMarkdownIt = require('@accordproject/markdown-common').FromMarkdownIt;
|
22 | const cicerorules = require('./cicerorules');
|
23 | const ToMarkdownCiceroVisitor = require('./ToMarkdownCiceroVisitor');
|
24 | const ToCiceroMarkUnwrappedVisitor = require('./ToCiceroMarkUnwrappedVisitor');
|
25 |
|
26 | const CommonMarkTransformer = require('@accordproject/markdown-common').CommonMarkTransformer;
|
27 | const { CommonMarkModel } = require('@accordproject/markdown-common').CommonMarkModel;
|
28 |
|
29 | const FromCiceroEditVisitor = require('./FromCiceroEditVisitor');
|
30 | const ToCommonMarkVisitor = require('./ToCommonMarkVisitor');
|
31 | const { CiceroMarkModel } = require('./externalModels/CiceroMarkModel');
|
32 | const unquoteVariables = require('./UnquoteVariables');
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 | class CiceroMarkTransformer {
|
41 | |
42 |
|
43 |
|
44 | constructor() {
|
45 |
|
46 | this.commonMark = new CommonMarkTransformer();
|
47 |
|
48 |
|
49 | this.modelManager = new ModelManager();
|
50 | this.modelManager.addModelFile(CommonMarkModel, 'commonmark.cto');
|
51 | this.modelManager.addModelFile(CiceroMarkModel, 'ciceromark.cto');
|
52 | const factory = new Factory(this.modelManager);
|
53 | this.serializer = new Serializer(factory, this.modelManager);
|
54 | }
|
55 |
|
56 | |
57 |
|
58 |
|
59 |
|
60 |
|
61 | getClauseText(input) {
|
62 | if (input.$class === 'org.accordproject.ciceromark.Clause') {
|
63 | const docInput = {
|
64 | $class: 'org.accordproject.commonmark.Document',
|
65 | xmlns : 'http://commonmark.org/xml/1.0',
|
66 | nodes: input.nodes,
|
67 | };
|
68 | return this.toMarkdownCicero(docInput);
|
69 | } else {
|
70 | throw new Error('Cannot apply getClauseText to non-clause node');
|
71 | }
|
72 | }
|
73 |
|
74 | |
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 | getSerializer() {
|
81 | return this.serializer;
|
82 | }
|
83 |
|
84 | |
85 |
|
86 |
|
87 |
|
88 |
|
89 | fromCiceroEdit(input) {
|
90 | const commonMark = this.commonMark.fromMarkdown(input);
|
91 | const dom = this.serializer.fromJSON(commonMark);
|
92 |
|
93 |
|
94 | const parameters = {
|
95 | ciceroMark: this,
|
96 | commonMark: this.commonMark,
|
97 | modelManager : this.modelManager,
|
98 | serializer : this.serializer,
|
99 | };
|
100 | const visitor = new FromCiceroEditVisitor();
|
101 | dom.accept(visitor, parameters);
|
102 | return this.serializer.toJSON(dom);
|
103 | }
|
104 |
|
105 | |
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 | toCiceroMarkUnwrapped(input,options) {
|
113 |
|
114 | if(options && Object.prototype.hasOwnProperty.call(options,'unquoteVariables') && options.unquoteVariables) {
|
115 | input = this.unquote(input);
|
116 | }
|
117 |
|
118 | const dom = this.serializer.fromJSON(input);
|
119 |
|
120 |
|
121 | const visitor = new ToCiceroMarkUnwrappedVisitor();
|
122 | dom.accept(visitor, {
|
123 | modelManager : this.modelManager,
|
124 | });
|
125 |
|
126 | return this.serializer.toJSON(dom);
|
127 | }
|
128 |
|
129 | |
130 |
|
131 |
|
132 |
|
133 |
|
134 | fromCommonMark(input) {
|
135 | return input;
|
136 | }
|
137 |
|
138 | |
139 |
|
140 |
|
141 |
|
142 |
|
143 | fromMarkdown(markdown) {
|
144 | const commonMarkDom = this.commonMark.fromMarkdown(markdown);
|
145 | return this.fromCommonMark(commonMarkDom);
|
146 | }
|
147 |
|
148 | |
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 | toMarkdown(input, options) {
|
155 | const commonMarkDom = this.toCommonMark(input, options);
|
156 | return this.commonMark.toMarkdown(commonMarkDom);
|
157 | }
|
158 |
|
159 | |
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 | fromMarkdownCicero(markdown, options) {
|
166 | const tokens = this.toTokens(markdown);
|
167 | return this.fromTokens(tokens);
|
168 | }
|
169 |
|
170 | |
171 |
|
172 |
|
173 |
|
174 |
|
175 | toMarkdownCicero(input) {
|
176 | const visitor = new ToMarkdownCiceroVisitor();
|
177 | return visitor.toMarkdownCicero(this.serializer.fromJSON(input));
|
178 | }
|
179 |
|
180 | |
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 | toCommonMark(input, options) {
|
189 | let json = this.toCiceroMarkUnwrapped(input,options);
|
190 | const dom = this.serializer.fromJSON(json);
|
191 |
|
192 |
|
193 | const visitor = new ToCommonMarkVisitor(options);
|
194 | dom.accept( visitor, {
|
195 | commonMark: this.commonMark,
|
196 | modelManager : this.modelManager,
|
197 | serializer : this.serializer
|
198 | } );
|
199 |
|
200 | let result = this.serializer.toJSON(dom);
|
201 |
|
202 | if(options && options.removeFormatting) {
|
203 | const cmt = new CommonMarkTransformer(options);
|
204 | result = cmt.removeFormatting(result);
|
205 | }
|
206 |
|
207 | return result;
|
208 | }
|
209 |
|
210 | |
211 |
|
212 |
|
213 |
|
214 |
|
215 | unquote(input) {
|
216 | return unquoteVariables(input);
|
217 | }
|
218 |
|
219 | |
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 | toTokens(input) {
|
226 | const parser = new MarkdownIt({html:true}).use(MarkdownItCicero);
|
227 | const tokenStream = parser.parse(input,{});
|
228 | return tokenStream;
|
229 | }
|
230 |
|
231 | |
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 | fromTokens(tokenStream) {
|
238 | const fromMarkdownIt = new FromMarkdownIt(cicerorules);
|
239 | const json = fromMarkdownIt.toCommonMark(tokenStream);
|
240 |
|
241 |
|
242 | const validJson = this.serializer.fromJSON(json);
|
243 | return this.serializer.toJSON(validJson);
|
244 | }
|
245 |
|
246 | }
|
247 |
|
248 | module.exports = CiceroMarkTransformer; |
\ | No newline at end of file |