summaryrefslogtreecommitdiff
path: root/lib/document-to-html.js
blob: 3d3e8db8d9590cde33f84a7a66084112d7dcd1b6 (plain)
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
var async = require("async");
var _ = require("underscore");

var promises = require("./promises");
var documents = require("./documents");
var htmlPaths = require("./html-paths");
var results = require("./results");
var images = require("./images");
var Html = require("./html");
var writers = require("./writers");

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 convertToHtml(document) {
        var messages = [];
        
        return promises.nfcall(elementToHtml, document, messages).then(function(html) {
            var writer = writers.writer({
                prettyPrint: options.prettyPrint,
                outputFormat: options.outputFormat
            });
            Html.write(writer, Html.simplify(html));
            return new results.Result(writer.asString(), messages);
        });
    }
    
    function convertElements(elements, messages, callback) {
        async.mapSeries(elements, function(element, callback) {
            return elementToHtml(element, messages, callback);
        }, callback);
    }

    function elementToHtml(element, messages, callback) {
        var handler = elementConverters[element.type];
        if (handler) {
            handler(element, messages, callback);
        } else {
            callback();
        }
    }
    
    function convertParagraph(element, messages, callback) {
        var style = styleForParagraph(element, messages);
        
        setImmediate(function() {
            convertElements(element.children, messages, function(error, children) {
                if (!options.ignoreEmptyParagraphs) {
                    children = [Html.forceWrite, Html.fragment(children)];
                }
                callback(null, Html.pathToNode(style, children));
            });
        });
    }
    
    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, messages, callback) {
        if (run.styleId) {
            var style = findStyle(run);
            if (!style) {
                messages.push(unrecognisedStyleWarning("run", run));
            }
        }
        convertElements(run.children, messages, function(error, children) {
            var result = Html.fragment(children);
            if (run.isStrikethrough) {
                result = convertStrikethrough(result);
            }
            if (run.isUnderline) {
                result = convertUnderline(result);
            }
            if (run.verticalAlignment === documents.verticalAlignment.subscript) {
                result = wrapInNonFreshElement(result, "sub");
            }
            if (run.verticalAlignment === documents.verticalAlignment.superscript) {
                result = wrapInNonFreshElement(result, "sup");
            }
            if (run.isItalic) {
                result = wrapInNonFreshElement(result, "em");
            }
            if (run.isBold) {
                result = wrapInNonFreshElement(result, "strong");
            }
            if (style) {
                result = Html.pathToNode(style.to, [result]);
            }
            callback(null, result);
        });
    }
    
    function wrapInNonFreshElement(node, tagName) {
        return Html.nonFreshElement(tagName, {}, [node]);
    }
    
    function convertUnderline(runHtml) {
        return (options.convertUnderline || defaultConvertUnderline)(runHtml);
    }
    
    function convertStrikethrough(result) {
        var style = findStyle({type: "strikethrough"});
        if (style) {
            return Html.pathToNode(style.to, [result]);
        } else {
            return wrapInNonFreshElement(result, "s");
        }
    }
    
    function defaultConvertUnderline(runHtml) {
        var style = findStyle({type: "underline"});
        if (style) {
            return Html.pathToNode(style.to, [runHtml]);
        } else {
            return runHtml;
        }
    }
    
    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, messages, callback) {
            convertImage(image, messages, function(error, result) {
                if (error) {
                    messages.push(results.warning(error.message));
                    callback(null, Html.fragment());
                } else {
                    callback(null, result);
                }
            });
        };
    }

    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, messages, callback) {
        wrapChildrenInFreshElement(element, "table", messages, callback);
    }
    
    function convertTableRow(element, messages, callback) {
        wrapChildrenInFreshElement(element, "tr", messages, callback);
    }
    
    function convertTableCell(element, messages, callback) {
        wrapChildrenInFreshElement(element, "td", messages, callback);
    }
    
    function wrapChildrenInFreshElement(element, wrapElementName, messages, callback) {
        convertElements(element.children, messages, function(error, children) {
            callback(null, Html.freshElement(wrapElementName, {}, [
                Html.forceWrite,
                Html.fragment(children)]));
        });
    }

    var elementConverters = {
        "document": function(document, messages, callback) {
            convertElements(document.children, messages, function(error, children) {
                var notes = noteReferences.map(function(noteReference) {
                    return document.notes.resolve(noteReference);
                });
                convertElements(notes, messages, function(error, notesNodes) {
                    callback(null, Html.fragment([
                        Html.fragment(children),
                        Html.freshElement("ol", {}, notesNodes)]));
                });
            });
        },
        "paragraph": convertParagraph,
        "run": convertRun,
        "text": function(element, messages, callback) {
            callback(null, Html.text(element.value));
        },
        "tab": function(element, messages, callback) {
            callback(null, Html.text("\t"));
        },
        "hyperlink": function(element, messages, callback) {
            var href = element.anchor ? "#" + htmlId(element.anchor) : element.href;

            convertElements(element.children, messages, function(error, children) {
                callback(null, Html.freshElement("a", {href: href}, children));
            });
        },
        "bookmarkStart": function(element, messages, callback) {
            callback(null, Html.freshElement("a", {
                id: htmlId(element.name)
            }, [Html.forceWrite]));
        },
        "noteReference": function(element, messages, callback) {
            noteReferences.push(element);
            
            callback(null, Html.freshElement("sup", {}, [
                Html.freshElement("a", {
                    href: "#" + noteHtmlId(element),
                    id: noteRefHtmlId(element)
                }, [Html.text("[" + (noteNumber++) + "]")])
            ]));
        },
        "note": function(element, messages, callback) {
            convertElements(element.body, messages, function(error, children) {
                callback(null, Html.freshElement("li", {id: noteHtmlId(element)}, [
                    Html.fragment(children),
                    
                    Html.elementWithTag(htmlPaths.element("p", {}, {fresh: false}), [
                        Html.text(" "),
                        Html.freshElement("a", {href: "#" + noteRefHtmlId(element)}, [Html.text("↑")]),
                        
                    ])
                ]));
            });
        },
        "image": recoveringConvertImage(options.convertImage || defaultConvertImage),
        "table": convertTable,
        "tableRow": convertTableRow,
        "tableCell": convertTableCell,
        "lineBreak": function(element, messages, callback) {
            callback(null, Html.selfClosingElement("br"));
        }
    };
    return {
        convertToHtml: convertToHtml
    };
}

function unrecognisedStyleWarning(type, element) {
    return results.warning(
        "Unrecognised " + type + " style: '" + element.styleName + "'" +
        " (Style ID: " + element.styleId + ")"
    );
}