diff options
| author | Michael Williamson <mike@zwobble.org> | 2013-04-01 22:39:47 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2013-04-01 22:39:47 +0100 |
| commit | 89d074071d250a23264e9ab82bb896af3e5ce674 (patch) | |
| tree | abc9ec9b92d3366217abe568bd0985fe6aabe4d0 | |
| parent | 720f997476ab2d0ac3cef17b890b423133ae5e7d (diff) | |
Fix image generation
| -rw-r--r-- | lib/document-to-html.js | 10 | ||||
| -rw-r--r-- | lib/docx-reader.js | 32 | ||||
| -rw-r--r-- | tests/docx-reader.tests.js | 2 | ||||
| -rw-r--r-- | tests/mammoth.tests.js | 9 | ||||
| -rwxr-xr-x | tests/test-data/tiny-picture.docx | bin | 0 -> 13699 bytes |
5 files changed, 35 insertions, 18 deletions
diff --git a/lib/document-to-html.js b/lib/document-to-html.js index b9efe0d..916eb57 100644 --- a/lib/document-to-html.js +++ b/lib/document-to-html.js @@ -42,7 +42,7 @@ function DocumentConverter(options) { function convertParagraph(element, html) { html.style(styleForParagraph(element)); - convertElements(element.children, html); + return convertElements(element.children, html); } function styleForParagraph(element) { @@ -63,9 +63,11 @@ function DocumentConverter(options) { if (run.properties.isItalic) { runHtml.push(styles.element("em")); } - convertElements(run.children, runHtml); - runHtml.closeAll(); - html.append(runHtml); + return convertElements(run.children, runHtml) + .then(function() { + runHtml.closeAll(); + html.append(runHtml); + }); } var elementConverters = { diff --git a/lib/docx-reader.js b/lib/docx-reader.js index eece7bb..2902943 100644 --- a/lib/docx-reader.js +++ b/lib/docx-reader.js @@ -4,6 +4,7 @@ exports.DocumentReader = DocumentReader; var q = require("q"); var _ = require("underscore"); var ZipFile = require("zipfile").ZipFile; +var path = require("path"); var xmlreader = require("./xmlreader"); var documents = require("./documents"); @@ -12,20 +13,25 @@ var documents = require("./documents"); function read(docxFile) { if (_.isString(docxFile)) { var zipFile = new ZipFile(docxFile); + var exists = function(path) { + return zipFile.names.indexOf(path) !== -1; + } docxFile = { read: function(path, encoding) { - return q.ninvoke(zipFile, "readFile", path) - .then(function(value) { - if (encoding) { - return value; - } else { - return value.toString(encoding); - } - }); + if (exists(path)) { + return q.ninvoke(zipFile, "readFile", path) + .then(function(value) { + if (encoding) { + return value.toString(encoding); + } else { + return value; + } + }); + } else { + return q.reject(new Error("No file in zip: " + path)); + } }, - exists: function(path) { - return zipFile.names.indexOf(path) !== -1; - } + exists: exists }; } @@ -41,7 +47,7 @@ function read(docxFile) { function readXmlFile(docxFile, path) { if (docxFile.exists(path)) { - return docxFile.read(path) + return docxFile.read(path, "utf-8") .then(xmlreader.read); } else { return null; @@ -168,7 +174,7 @@ function DocumentReader(relationships, docxFile) { .attributes["r:embed"]; var imagePath = relationships[relationshipId].target; var image = documents.Image(function() { - return docxFile.read(imagePath); + return docxFile.read(path.join("word", imagePath)); }) return new Result(image); } diff --git a/tests/docx-reader.tests.js b/tests/docx-reader.tests.js index 9f431b7..8971782 100644 --- a/tests/docx-reader.tests.js +++ b/tests/docx-reader.tests.js @@ -146,7 +146,7 @@ describe("readElement", function() { "rId5": {target: "media/hat.png"} }, createFakeDocxFile({ - "media/hat.png": imageBuffer + "word/media/hat.png": imageBuffer }) ); var result = reader.readXmlElement(drawing); diff --git a/tests/mammoth.tests.js b/tests/mammoth.tests.js index e963257..f8e799d 100644 --- a/tests/mammoth.tests.js +++ b/tests/mammoth.tests.js @@ -1,4 +1,5 @@ var assert = require("assert"); +var path = require("path"); var mammoth = require("../") var styles = require("../lib/styles"); @@ -31,4 +32,12 @@ describe('mammoth', function() { assert.equal("<h1>Hello.</h1>", result.html); }); }); + + test('inline images are included in output', function() { + var converter = new mammoth.Converter(); + var docxPath = path.join(__dirname, "test-data/tiny-picture.docx"); + return converter.convertToHtml(docxPath).then(function(result) { + assert.equal(result.html, '<p><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAOvgAADr4B6kKxwAAAABNJREFUKFNj/M+ADzDhlWUYqdIAQSwBE8U+X40AAAAASUVORK5CYII=" /></p>'); + }); + }); }) diff --git a/tests/test-data/tiny-picture.docx b/tests/test-data/tiny-picture.docx Binary files differnew file mode 100755 index 0000000..d45bb9e --- /dev/null +++ b/tests/test-data/tiny-picture.docx |
