1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | 'use strict';
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | class ToHtmlStringVisitor {
|
22 | |
23 |
|
24 |
|
25 |
|
26 | constructor(options) {
|
27 | this.options = options;
|
28 | }
|
29 | |
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
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 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | static mkParametersIn(parametersOut) {
|
62 | let parameters = {};
|
63 | parameters.result = '';
|
64 | parameters.first = true;
|
65 | parameters.indent = parametersOut.indent;
|
66 |
|
67 | return parameters;
|
68 | }
|
69 | |
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 | static mkParametersInList(parametersOut) {
|
77 | let parameters = {};
|
78 | parameters.result = '';
|
79 | parameters.first = true;
|
80 | parameters.indent = parametersOut.indent + 1;
|
81 |
|
82 | return parameters;
|
83 | }
|
84 | |
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 | visit(thing, parameters) {
|
92 | switch (thing.getType()) {
|
93 | case 'Clause':
|
94 |
|
95 |
|
96 |
|
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 |
|
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 |
|
236 | module.exports = ToHtmlStringVisitor; |
\ | No newline at end of file |