diff options
| author | Michael Williamson <mike@zwobble.org> | 2014-07-05 19:45:24 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2014-07-05 19:45:24 +0100 |
| commit | 377e08477c8dce941b2d1504899123274e0c3e80 (patch) | |
| tree | 2a0381fc7e499aa1a5d3a6eab52448a25913716a | |
| parent | 936b74f2d0415dd73fa6471eea4daf6b32b07051 (diff) | |
Add conversion of tables to HTML
| -rw-r--r-- | lib/document-to-html.js | 50 | ||||
| -rw-r--r-- | lib/documents.js | 29 | ||||
| -rw-r--r-- | tests/document-to-html.tests.js | 22 |
3 files changed, 99 insertions, 2 deletions
diff --git a/lib/document-to-html.js b/lib/document-to-html.js index fe601bd..6c954e6 100644 --- a/lib/document-to-html.js +++ b/lib/document-to-html.js @@ -213,7 +213,55 @@ function DocumentConversion(options) { } }); }, - "image": options.convertImage || defaultConvertImage + "image": options.convertImage || defaultConvertImage, + "table": function(element, html, messages, callback) { + html.closeAll(); + html.open(htmlPaths.element("table")); + convertElements(element.children, html, messages, function(err) { + if (err) { + callback(err); + } else { + html.close(); + callback(); + } + }); + }, + "tableRow": function(element, html, messages, callback) { + html.open(htmlPaths.element("tr")); + convertElements(element.children, html, messages, function(err) { + if (err) { + callback(err); + } else { + html.close(); + callback(); + } + }); + }, + "tableCell": function(element, html, messages, callback) { + html.open(htmlPaths.element("td")); + + async.map(element.children, function(child, callback) { + var cellHtml = new HtmlGenerator(); + 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(); + } + }); + } }; return { convertToHtml: convertToHtml diff --git a/lib/documents.js b/lib/documents.js index 86e0709..a781bbd 100644 --- a/lib/documents.js +++ b/lib/documents.js @@ -7,7 +7,10 @@ var types = { hyperlink: "hyperlink", footnoteReference: "footnoteReference", image: "image", - footnote: "footnote" + footnote: "footnote", + table: "table", + tableRow: "tableRow", + tableCell: "tableCell" }; function Document(children, options) { @@ -106,6 +109,27 @@ function Image(options) { }; } +function Table(children) { + return { + type: types.table, + children: children + }; +} + +function TableRow(children) { + return { + type: types.tableRow, + children: children + }; +} + +function TableCell(children) { + return { + type: types.tableCell, + children: children + }; +} + exports.Document = Document; exports.Paragraph = Paragraph; exports.Run = Run; @@ -116,3 +140,6 @@ exports.FootnoteReference = FootnoteReference; exports.Footnotes = Footnotes; exports.Footnote = Footnote; exports.Image = Image; +exports.Table = Table; +exports.TableRow = TableRow; +exports.TableCell = TableCell; diff --git a/tests/document-to-html.tests.js b/tests/document-to-html.tests.js index cfd1c20..ead267b 100644 --- a/tests/document-to-html.tests.js +++ b/tests/document-to-html.tests.js @@ -197,6 +197,28 @@ describe('DocumentConverter', function() { }); }); + test('docx table is converted to table in HTML', function() { + var table = new documents.Table([ + new documents.TableRow([ + new documents.TableCell([paragraphOfText("Top left")]), + new documents.TableCell([paragraphOfText("Top right")]) + ]), + new documents.TableRow([ + new documents.TableCell([paragraphOfText("Bottom left")]), + new documents.TableCell([paragraphOfText("Bottom right")]) + ]) + ]); + var converter = new DocumentConverter(); + + return converter.convertToHtml(table).then(function(result) { + var expectedHtml = "<table>" + + "<tr><td><p>Top left</p></td><td><p>Top right</p></td></tr>" + + "<tr><td><p>Bottom left</p></td><td><p>Bottom right</p></td></tr>" + + "</table>"; + assert.equal(result.value, expectedHtml); + }); + }); + test('footnote reference is converted to superscript intra-page link', function() { var footnoteReference = new documents.FootnoteReference({ footnoteId: "4", |
