1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
var async = require("async");
var _ = require("underscore");
var promises = require("./promises");
var documents = require("./documents");
var htmlPaths = require("./html-paths");
var HtmlGenerator = require("./html-generator").HtmlGenerator;
var results = require("./results");
var images = require("./images");
exports.DocumentConverter = DocumentConverter;
// TODO: investigate tidier solutions
if (typeof setImmediate === "undefined") {
var setImmediate = function(callback) {
setTimeout(callback, 0);
};
}
function DocumentConverter(options) {
return {
convertToHtml: function() {
var conversion = new DocumentConversion(options);
return conversion.convertToHtml.apply(this, arguments);
}
};
}
function DocumentConversion(options) {
var noteNumber = 1;
var noteReferences = [];
options = _.extend({ignoreEmptyParagraphs: true}, options);
var idPrefix = options.idPrefix === undefined ?
Math.floor(Math.random() * 1000000000000000) :
options.idPrefix;
var defaultParagraphStyle = htmlPaths.topLevelElement("p");
var styleMap = options.styleMap || [];
function createHtmlGenerator() {
return new HtmlGenerator({
prettyPrint: options.prettyPrint,
outputFormat: options.outputFormat
});
}
function convertToHtml(document) {
var messages = [];
var html = createHtmlGenerator();
return promises.nfcall(elementToHtml, document, html, messages).then(function() {
html.closeAll();
return new results.Result(html.asString(), messages);
});
}
function convertElements(elements, html, messages, callback) {
async.eachSeries(elements, function(element, callback) {
return elementToHtml(element, html, messages, callback);
}, callback);
}
function elementToHtml(element, html, messages, callback) {
var handler = elementConverters[element.type];
if (handler) {
handler(element, html, messages, callback);
} else {
callback();
}
}
function convertParagraph(element, html, messages, callback) {
html.satisfyPath(styleForParagraph(element, messages));
if (!options.ignoreEmptyParagraphs) {
html.writeAll();
}
setImmediate(function() {
convertElements(element.children, html, messages, callback);
});
}
function styleForParagraph(element, messages) {
var style = findStyle(element);
if (style) {
return style.to;
} else {
if (element.styleId) {
messages.push(unrecognisedStyleWarning("paragraph", element));
}
return defaultParagraphStyle;
}
}
function convertRun(run, html, messages, callback) {
var runHtml = createHtmlGenerator();
if (run.styleId) {
var style = findStyle(run);
if (style) {
runHtml.satisfyPath(style.to);
} else {
messages.push(unrecognisedStyleWarning("run", run));
}
}
if (run.isBold) {
runHtml.open(htmlPaths.element("strong"));
}
if (run.isItalic) {
runHtml.open(htmlPaths.element("em"));
}
if (run.verticalAlignment === documents.verticalAlignment.superscript) {
runHtml.open(htmlPaths.element("sup"));
}
if (run.verticalAlignment === documents.verticalAlignment.subscript) {
runHtml.open(htmlPaths.element("sub"));
}
if (run.isUnderline) {
convertUnderline(runHtml);
}
if (run.isStrikethrough) {
convertStrikethrough(runHtml);
}
convertElements(run.children, runHtml, messages, function(err) {
if (err) {
callback(err);
} else {
runHtml.closeAll();
html.append(runHtml);
callback();
}
});
}
function convertUnderline(runHtml) {
(options.convertUnderline || defaultConvertUnderline)(runHtml);
}
function convertStrikethrough(runHtml) {
var style = findStyle({type: "strikethrough"});
if (style) {
runHtml.appendPath(style.to);
} else {
runHtml.open(htmlPaths.element("s"));
}
}
function defaultConvertUnderline(runHtml) {
var style = findStyle({type: "underline"});
if (style) {
runHtml.appendPath(style.to);
}
}
function findStyle(element) {
for (var i = 0; i < styleMap.length; i++) {
if (styleMap[i].from.matches(element)) {
return styleMap[i];
}
}
}
var defaultConvertImage = images.inline(function(element) {
return element.read("base64").then(function(imageBuffer) {
return {
src: "data:" + element.contentType + ";base64," + imageBuffer
};
});
});
function recoveringConvertImage(convertImage) {
return function(image, html, messages, callback) {
convertImage(image, html, messages, function(error) {
if (error) {
messages.push(results.warning(error.message));
}
callback();
});
};
}
function noteHtmlId(note) {
return htmlId(note.noteType + "-" + note.noteId);
}
function noteRefHtmlId(note) {
return htmlId(note.noteType + "-ref-" + note.noteId);
}
function htmlId(suffix) {
return idPrefix + "-" + suffix;
}
function convertTable(element, html, messages, callback) {
html.closeAll();
wrapChildren(element, "table", html, messages, callback);
}
function convertTableRow(element, html, messages, callback) {
wrapChildren(element, "tr", html, messages, callback);
}
function convertTableCell(element, html, messages, callback) {
html.open(htmlPaths.element("td"), {alwaysWrite: true});
async.map(element.children, function(child, callback) {
var cellHtml = createHtmlGenerator();
elementToHtml(child, cellHtml, messages, function(error) {
if (error) {
callback(error);
} else {
cellHtml.closeAll();
callback(null, cellHtml);
}
});
}, function(error, childrenHtml) {
if (error) {
callback(error);
} else {
childrenHtml.forEach(function(childHtml) {
html.append(childHtml);
});
html.close();
callback();
}
});
}
function wrapChildren(element, wrapElementName, html, messages, callback) {
html.open(htmlPaths.element(wrapElementName));
convertElements(element.children, html, messages, function(err) {
if (err) {
callback(err);
} else {
html.close();
callback();
}
});
}
var elementConverters = {
"document": function(document, html, messages, callback) {
convertElements(document.children, html, messages, function(error) {
if (error) {
callback(error);
} else {
html.closeAll();
html.open(htmlPaths.element("ol"));
var notes = noteReferences.map(function(noteReference) {
return document.notes.resolve(noteReference);
});
convertElements(notes, html, messages, function(error) {
if (error) {
callback(error);
} else {
html.close();
callback();
}
});
}
});
},
"paragraph": convertParagraph,
"run": convertRun,
"text": function(element, html, messages, callback) {
html.text(element.value);
callback();
},
"tab": function(element, html, messages, callback) {
html.text("\t");
callback();
},
"hyperlink": function(element, html, messages, callback) {
var href = element.anchor ? "#" + htmlId(element.anchor) : element.href;
html.open(htmlPaths.element("a", {href: href}));
convertElements(element.children, html, messages, function(err) {
if (err) {
callback(err);
} else {
html.close();
callback();
}
});
},
"bookmarkStart": function(element, html, messages, callback) {
html.open(htmlPaths.element("a", {
id: htmlId(element.name)
}), {alwaysWrite: true});
html.close();
callback();
},
"noteReference": function(element, html, messages, callback) {
html.open(htmlPaths.element("sup"));
html.open(htmlPaths.element("a", {
href: "#" + noteHtmlId(element),
id: noteRefHtmlId(element)
}));
noteReferences.push(element);
html.text("[" + (noteNumber++) + "]");
html.close();
html.close();
callback();
},
"note": function(element, html, messages, callback) {
html.open(htmlPaths.element("li", {id: noteHtmlId(element)}));
var footnoteHtml = createHtmlGenerator();
convertElements(element.body, footnoteHtml, messages, function(err) {
if (err) {
callback(err);
} else {
footnoteHtml.text(" ");
footnoteHtml.open(htmlPaths.element("a", {href: "#" + noteRefHtmlId(element)}));
footnoteHtml.text("↑");
footnoteHtml.closeAll();
html.append(footnoteHtml);
html.close();
callback();
}
});
},
"image": recoveringConvertImage(options.convertImage || defaultConvertImage),
"table": convertTable,
"tableRow": convertTableRow,
"tableCell": convertTableCell,
"lineBreak": function(element, html, messages, callback) {
html.selfClosing(htmlPaths.element("br"));
callback();
}
};
return {
convertToHtml: convertToHtml
};
}
function unrecognisedStyleWarning(type, element) {
return results.warning(
"Unrecognised " + type + " style: '" + element.styleName + "'" +
" (Style ID: " + element.styleId + ")"
);
}
|