aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2013-04-18 20:07:23 +0100
committerMichael Williamson <mike@zwobble.org>2013-04-18 20:07:23 +0100
commit554e939ed6f3ce697fca925a333cb9fc14ccbe5a (patch)
treea777f5c2fb2b67066196951a7d954c16c6ba12fd
parent58d57f995fcf2077c3c4147dfc637cb1759de98b (diff)
Return Result from document-to-HTML conversion
-rw-r--r--browser-demo/demo.js2
-rw-r--r--lib/document-to-html.js5
-rw-r--r--lib/index.js5
-rw-r--r--lib/results.js6
-rw-r--r--tests/document-to-html.tests.js33
-rw-r--r--tests/mammoth.tests.js8
6 files changed, 28 insertions, 31 deletions
diff --git a/browser-demo/demo.js b/browser-demo/demo.js
index a9da37d..20dcda0 100644
--- a/browser-demo/demo.js
+++ b/browser-demo/demo.js
@@ -2,7 +2,7 @@
mammoth.fileInput(
document.getElementById("document"),
function(result) {
- document.getElementById("output").innerHTML = result.html;
+ document.getElementById("output").innerHTML = result.value;
var messageHtml = result.messages.map(function(message) {
return '<li class="' + message.type + '">' + escapeHtml(message.message) + "</li>";
diff --git a/lib/document-to-html.js b/lib/document-to-html.js
index 64672ee..ef39da8 100644
--- a/lib/document-to-html.js
+++ b/lib/document-to-html.js
@@ -3,6 +3,7 @@ var async = require("async");
var htmlPaths = require("./html-paths");
var HtmlGenerator = require("./html-generator").HtmlGenerator;
+var results = require("./results");
exports.DocumentConverter = DocumentConverter;
@@ -20,9 +21,7 @@ function DocumentConverter(options) {
return q.nfcall(elementToHtml, document, html).then(function() {
html.closeAll();
- return {
- html: html.asString()
- };
+ return new results.Result(html.asString(), []);
});
}
diff --git a/lib/index.js b/lib/index.js
index fb734e6..2a772bb 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -20,11 +20,8 @@ Converter.prototype.convertToHtml = function(inputOptions) {
var documentConverter = this._documentConverter;
return docxReader.read(inputOptions)
.then(function(documentResult) {
- return documentResult.mapThen(function(document) {
+ return documentResult.flatMapThen(function(document) {
return documentConverter.convertToHtml(document);
});
- }).then(function(result) {
- result.html = result.value.html;
- return result;
});
}
diff --git a/lib/results.js b/lib/results.js
index 5bd81c0..d5dd7af 100644
--- a/lib/results.js
+++ b/lib/results.js
@@ -11,10 +11,10 @@ Result.prototype.map = function(func) {
return new Result(func(this.value), this.messages);
};
-Result.prototype.mapThen = function(func) {
+Result.prototype.flatMapThen = function(func) {
var messages = this.messages;
- return func(this.value).then(function(resultValue) {
- return new Result(resultValue, messages);
+ return func(this.value).then(function(otherResult) {
+ return new Result(otherResult.value, messages.concat(otherResult.messages));
});
};
diff --git a/tests/document-to-html.tests.js b/tests/document-to-html.tests.js
index 5ed2224..f1e6402 100644
--- a/tests/document-to-html.tests.js
+++ b/tests/document-to-html.tests.js
@@ -6,6 +6,7 @@ var DocumentConverter = require("../lib/document-to-html").DocumentConverter;
var test = require("./testing").test;
var htmlPaths = require("../lib/html-paths");
var xmlreader = require("../lib/xmlreader");
+var results = require("../lib/results");
describe('DocumentConverter', function() {
@@ -13,7 +14,7 @@ describe('DocumentConverter', function() {
var document = new documents.Document([]);
var converter = new DocumentConverter();
return converter.convertToHtml(document).then(function(result) {
- assert.equal(result.html, "");
+ assert.equal(result.value, "");
});
});
@@ -23,7 +24,7 @@ describe('DocumentConverter', function() {
]);
var converter = new DocumentConverter();
return converter.convertToHtml(document).then(function(result) {
- assert.equal(result.html, "<p>Hello.</p>");
+ assert.equal(result.value, "<p>Hello.</p>");
});
});
@@ -33,7 +34,7 @@ describe('DocumentConverter', function() {
]);
var converter = new DocumentConverter();
return converter.convertToHtml(document).then(function(result) {
- assert.equal(result.html, "");
+ assert.equal(result.value, "");
});
});
@@ -45,7 +46,7 @@ describe('DocumentConverter', function() {
defaultParagraphStyle: htmlPaths.topLevelElement("h1")
});
return converter.convertToHtml(document).then(function(result) {
- assert.equal(result.html, "<h1>Hello.</h1>");
+ assert.equal(result.value, "<h1>Hello.</h1>");
});
});
@@ -55,7 +56,7 @@ describe('DocumentConverter', function() {
]);
var converter = new DocumentConverter();
return converter.convertToHtml(document).then(function(result) {
- assert.equal(result.html, "<p>1 &lt; 2</p>");
+ assert.equal(result.value, "<p>1 &lt; 2</p>");
});
});
@@ -66,7 +67,7 @@ describe('DocumentConverter', function() {
]);
var converter = new DocumentConverter();
return converter.convertToHtml(document).then(function(result) {
- assert.equal(result.html, "<p>Hello.</p><p>Goodbye.</p>");
+ assert.equal(result.value, "<p>Hello.</p><p>Goodbye.</p>");
});
});
@@ -80,7 +81,7 @@ describe('DocumentConverter', function() {
}
});
return converter.convertToHtml(document).then(function(result) {
- assert.equal(result.html, "<h1>Hello.</h1>");
+ assert.equal(result.value, "<h1>Hello.</h1>");
});
});
@@ -94,7 +95,7 @@ describe('DocumentConverter', function() {
}
});
return converter.convertToHtml(document).then(function(result) {
- assert.equal(result.html, "<h1><span>Hello.</span></h1>");
+ assert.equal(result.value, "<h1><span>Hello.</span></h1>");
});
});
@@ -102,7 +103,7 @@ describe('DocumentConverter', function() {
var run = runOfText("Hello.", {isBold: true});
var converter = new DocumentConverter();
return converter.convertToHtml(run).then(function(result) {
- assert.equal(result.html, "<strong>Hello.</strong>");
+ assert.equal(result.value, "<strong>Hello.</strong>");
});
});
@@ -112,7 +113,7 @@ describe('DocumentConverter', function() {
]);
var converter = new DocumentConverter();
return converter.convertToHtml(run).then(function(result) {
- assert.equal(result.html, "<p><strong>Hello.</strong></p>");
+ assert.equal(result.value, "<p><strong>Hello.</strong></p>");
});
});
@@ -120,7 +121,7 @@ describe('DocumentConverter', function() {
var run = runOfText("Hello.", {isItalic: true});
var converter = new DocumentConverter();
return converter.convertToHtml(run).then(function(result) {
- assert.equal(result.html, "<em>Hello.</em>");
+ assert.equal(result.value, "<em>Hello.</em>");
});
});
@@ -128,7 +129,7 @@ describe('DocumentConverter', function() {
var run = runOfText("Hello.", {isBold: true, isItalic: true});
var converter = new DocumentConverter();
return converter.convertToHtml(run).then(function(result) {
- assert.equal(result.html, "<strong><em>Hello.</em></strong>");
+ assert.equal(result.value, "<strong><em>Hello.</em></strong>");
});
});
@@ -140,7 +141,7 @@ describe('DocumentConverter', function() {
}
});
return converter.convertToHtml(run).then(function(result) {
- assert.equal(result.html, "<strong>Hello.</strong>");
+ assert.equal(result.value, "<strong>Hello.</strong>");
});
});
@@ -151,7 +152,7 @@ describe('DocumentConverter', function() {
);
var converter = new DocumentConverter();
return converter.convertToHtml(hyperlink).then(function(result) {
- assert.equal(result.html, '<a href="http://www.example.com">Hello.</a>');
+ assert.equal(result.value, '<a href="http://www.example.com">Hello.</a>');
});
});
@@ -164,7 +165,7 @@ describe('DocumentConverter', function() {
);
var converter = new DocumentConverter();
return converter.convertToHtml(image).then(function(result) {
- assert.equal(result.html, '<img src="data:image/png;base64,' + imageBuffer.toString("base64") + '" />');
+ assert.equal(result.value, '<img src="data:image/png;base64,' + imageBuffer.toString("base64") + '" />');
});
});
@@ -179,7 +180,7 @@ describe('DocumentConverter', function() {
var converter = new DocumentConverter();
return converter.convertToHtml(image)
.then(function(result) {
- return xmlreader.read(result.html);
+ return xmlreader.read(result.value);
})
.then(function(htmlImageElementDocument) {
var htmlImageElement = htmlImageElementDocument.root;
diff --git a/tests/mammoth.tests.js b/tests/mammoth.tests.js
index fe5d72a..49bc02f 100644
--- a/tests/mammoth.tests.js
+++ b/tests/mammoth.tests.js
@@ -18,7 +18,7 @@ describe('mammoth', function() {
var docxPath = path.join(__dirname, "test-data/single-paragraph.docx");
var converter = new mammoth.Converter();
return converter.convertToHtml({path: docxPath}).then(function(result) {
- assert.equal("<p>Walking on imported air</p>", result.html);
+ assert.equal("<p>Walking on imported air</p>", result.value);
});
});
@@ -32,7 +32,7 @@ describe('mammoth', function() {
}
var converter = new mammoth.Converter();
return converter.convertToHtml({uint8Array: array}).then(function(result) {
- assert.equal("<p>Walking on imported air</p>", result.html);
+ assert.equal("<p>Walking on imported air</p>", result.value);
});
});
});
@@ -45,7 +45,7 @@ describe('mammoth', function() {
defaultParagraphStyle: htmlPaths.topLevelElement("h1")
});
return converter.convertToHtml({file: docxFile}).then(function(result) {
- assert.equal("<h1>Hello.</h1>", result.html);
+ assert.equal("<h1>Hello.</h1>", result.value);
});
});
@@ -53,7 +53,7 @@ describe('mammoth', function() {
var converter = new mammoth.Converter();
var docxPath = path.join(__dirname, "test-data/tiny-picture.docx");
return converter.convertToHtml({path: docxPath}).then(function(result) {
- assert.equal(result.html, '<p><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAOvgAADr4B6kKxwAAAABNJREFUKFNj/M+ADzDhlWUYqdIAQSwBE8U+X40AAAAASUVORK5CYII=" /></p>');
+ assert.equal(result.value, '<p><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAOvgAADr4B6kKxwAAAABNJREFUKFNj/M+ADzDhlWUYqdIAQSwBE8U+X40AAAAASUVORK5CYII=" /></p>');
});
});
})