UNPKG

7.25 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'use strict'; // const CiceroMarkTransformer = require('@accordproject/markdown-cicero').CiceroMarkTransformer;
15
16/**
17 * Converts a commonmark model instance to an html string.
18 *
19 */
20
21class ToHtmlStringVisitor {
22 /**
23 * Construct the visitor
24 * @param {*} [options] configuration options
25 */
26 constructor(options) {
27 this.options = options;
28 }
29 /**
30 * Visits a sub-tree and return the html
31 * @param {*} visitor the visitor to use
32 * @param {*} thing the node to visit
33 * @param {*} [parameters] optional parameters
34 * @returns {string} the html for the sub tree
35 */
36
37
38 static visitChildren(visitor, thing, parameters) {
39 if (!parameters) {
40 parameters = {};
41 parameters.result = '';
42 parameters.first = false;
43 parameters.indent = 0;
44 }
45
46 if (thing.nodes) {
47 thing.nodes.forEach(node => {
48 node.accept(visitor, parameters);
49 });
50 }
51
52 return parameters.result;
53 }
54 /**
55 * Set parameters for inner node
56 * @param {*} parametersOut - the current parameters
57 * @return {*} the new parameters with first set to true
58 */
59
60
61 static mkParametersIn(parametersOut) {
62 let parameters = {};
63 parameters.result = '';
64 parameters.first = true;
65 parameters.indent = parametersOut.indent; // Same indentation
66
67 return parameters;
68 }
69 /**
70 * Set parameters for inner list
71 * @param {*} parametersOut - the current parameters
72 * @return {*} the new parameters with first set to true
73 */
74
75
76 static mkParametersInList(parametersOut) {
77 let parameters = {};
78 parameters.result = '';
79 parameters.first = true;
80 parameters.indent = parametersOut.indent + 1; // Increases indentation
81
82 return parameters;
83 }
84 /**
85 * Visit a node
86 * @param {*} thing the object being visited
87 * @param {*} parameters the parameters
88 */
89
90
91 visit(thing, parameters) {
92 switch (thing.getType()) {
93 case 'Clause':
94 // {
95 // const ciceroMarkTransformer = new CiceroMarkTransformer();
96 // console.log(JSON.stringify(ciceroMarkTransformer.getSerializer().toJSON(thing), null, 4));
97 // }
98 parameters.result += "<div class=\"clause\" clauseid=\"".concat(thing.clauseid, "\" src=\"").concat(thing.src, "\">\n").concat(ToHtmlStringVisitor.visitChildren(this, thing), "</div>\n");
99 break;
100
101 case 'Variable':
102 {
103 const formatString = thing.format ? " format=\"".concat(thing.format, "\"") : '';
104 parameters.result += "<span class=\"variable\" id=\"".concat(thing.id, "\"").concat(formatString, ">").concat(thing.value, "</span>");
105 }
106 break;
107
108 case 'ConditionalVariable':
109 parameters.result += "<span class=\"conditional\" id=\"".concat(thing.id, "\" whenTrue=\"").concat(thing.whenTrue, "\" whenFalse=\"").concat(thing.whenFalse, "\">").concat(thing.value, "</span>");
110 break;
111
112 case 'ComputedVariable':
113 parameters.result += "<span class=\"computed\">".concat(thing.value, "</span>");
114 break;
115
116 case 'CodeBlock':
117 {
118 const info = thing.info;
119
120 if (info) {
121 parameters.result += "<pre class=\"code_block\"><code data-ciceromark=\"".concat(encodeURIComponent(thing.info), "\">").concat(thing.text, "</pre></code>\n");
122 } else {
123 parameters.result += "<pre class=\"code_block\"><code>".concat(thing.text, "</pre></code>\n");
124 }
125 }
126 break;
127
128 case 'Code':
129 parameters.result += "<code>".concat(thing.text, "</code>");
130 break;
131
132 case 'HtmlInline':
133 parameters.result += "<span class=\"html_inline\">".concat(thing.text, "</span>");
134 break;
135
136 case 'Emph':
137 parameters.result += "<em>".concat(ToHtmlStringVisitor.visitChildren(this, thing), "</em>");
138 break;
139
140 case 'Strong':
141 parameters.result += "<strong>".concat(ToHtmlStringVisitor.visitChildren(this, thing), "</strong>");
142 break;
143
144 case 'BlockQuote':
145 {
146 parameters.result += "<blockquote>".concat(ToHtmlStringVisitor.visitChildren(this, thing), "</blockquote>\n");
147 }
148 break;
149
150 case 'Heading':
151 {
152 const level = parseInt(thing.level);
153 parameters.result += "<h".concat(level, ">").concat(ToHtmlStringVisitor.visitChildren(this, thing), "</h").concat(level, ">\n");
154 }
155 break;
156
157 case 'ThematicBreak':
158 parameters.result += '\n<hr>\n';
159 break;
160
161 case 'Linebreak':
162 parameters.result += '<br>';
163 break;
164
165 case 'Softbreak':
166 parameters.result += '\n';
167 break;
168
169 case 'Link':
170 parameters.result += "<a href=\"".concat(thing.destination, "\" title=").concat(thing.title, ">").concat(ToHtmlStringVisitor.visitChildren(this, thing), "</a>");
171 break;
172
173 case 'Image':
174 parameters.result += "<img src=\"".concat(thing.destination, "\" title=\"").concat(thing.title, "\"/>");
175 break;
176
177 case 'Paragraph':
178 parameters.result += "<p>".concat(ToHtmlStringVisitor.visitChildren(this, thing), "</p>\n");
179 break;
180
181 case 'HtmlBlock':
182 {
183 parameters.result += "<pre class=\"html_block\"><code>".concat(thing.text, "</pre></code>\n");
184 break;
185 }
186
187 case 'Text':
188 parameters.result += "".concat(thing.text);
189 break;
190
191 case 'List':
192 {
193 // Always start with a new line
194 parameters.result += '\n';
195 const {
196 delimiter,
197 start,
198 tight
199 } = thing;
200
201 if (thing.type === 'ordered') {
202 parameters.result += "<ol delimiter=".concat(delimiter, " start=").concat(start, " tight=").concat(tight, ">");
203 } else {
204 parameters.result += "<ul tight=".concat(tight, ">");
205 }
206
207 thing.nodes.forEach(item => {
208 parameters.result += "\n<li>".concat(ToHtmlStringVisitor.visitChildren(this, item), "</li>");
209 });
210
211 if (thing.type === 'ordered') {
212 parameters.result += '</ol>';
213 } else {
214 parameters.result += '</ul>';
215 }
216 }
217 break;
218
219 case 'Item':
220 parameters.result += "<li>".concat(ToHtmlStringVisitor.visitChildren(this, thing), "</li>\n");
221 break;
222
223 case 'Document':
224 parameters.result += "<html>\n<body>\n<div class=\"document\">\n".concat(ToHtmlStringVisitor.visitChildren(this, thing), "</div>\n</body>\n</html>");
225 break;
226
227 default:
228 throw new Error("Unhandled type ".concat(thing.getType()));
229 }
230
231 parameters.first = false;
232 }
233
234}
235
236module.exports = ToHtmlStringVisitor;
\No newline at end of file