aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2013-04-18 20:12:51 +0100
committerMichael Williamson <mike@zwobble.org>2013-04-18 20:12:51 +0100
commitf5e1cbb69b1aae679f8629fe13d0c49778b181d0 (patch)
tree97e918bcec20cabb94f050a924578c0f2ea582d0
parent554e939ed6f3ce697fca925a333cb9fc14ccbe5a (diff)
Emit warnings if paragraph style is not recognised
-rw-r--r--lib/document-to-html.js43
-rw-r--r--tests/document-to-html.tests.js10
-rw-r--r--tests/mammoth.tests.js3
3 files changed, 36 insertions, 20 deletions
diff --git a/lib/document-to-html.js b/lib/document-to-html.js
index ef39da8..3eefee9 100644
--- a/lib/document-to-html.js
+++ b/lib/document-to-html.js
@@ -16,40 +16,45 @@ function DocumentConverter(options) {
var runStyleMap = options.runStyleMap || {};
function convertToHtml(document) {
+ var messages = [];
var html = new HtmlGenerator();
- return q.nfcall(elementToHtml, document, html).then(function() {
+ return q.nfcall(elementToHtml, document, html, messages).then(function() {
html.closeAll();
- return new results.Result(html.asString(), []);
+ return new results.Result(html.asString(), messages);
});
}
- function convertElements(elements, html, callback) {
+ function convertElements(elements, html, messages, callback) {
async.eachSeries(elements, function(element, callback) {
- return elementToHtml(element, html, callback);
+ return elementToHtml(element, html, messages, callback);
}, callback);
}
- function elementToHtml(element, html, callback) {
+ function elementToHtml(element, html, messages, callback) {
var handler = elementConverters[element.type];
if (handler) {
- handler(element, html, callback);
+ handler(element, html, messages, callback);
} else {
callback();
}
}
- function convertParagraph(element, html, callback) {
- html.satisfyPath(styleForParagraph(element));
- convertElements(element.children, html, callback);
+ function convertParagraph(element, html, messages, callback) {
+ html.satisfyPath(styleForParagraph(element, messages));
+ convertElements(element.children, html, messages, callback);
}
- function styleForParagraph(element) {
- return paragraphStyleMap[element.styleName] || defaultParagraphStyle;
+ function styleForParagraph(element, messages) {
+ var htmlPath = paragraphStyleMap[element.styleName];
+ if (element.styleName && !htmlPath) {
+ messages.push(results.warning("Unrecognised paragraph style: " + element.styleName));
+ }
+ return htmlPath || defaultParagraphStyle;
}
- function convertRun(run, html, callback) {
+ function convertRun(run, html, messages, callback) {
var runHtml = new HtmlGenerator();
if (run.styleName) {
var style = runStyleMap[run.styleName];
@@ -63,7 +68,7 @@ function DocumentConverter(options) {
if (run.isItalic) {
runHtml.open(htmlPaths.element("em"));
}
- convertElements(run.children, runHtml, function(err) {
+ convertElements(run.children, runHtml, messages, function(err) {
if (err) {
callback(err);
} else {
@@ -75,18 +80,18 @@ function DocumentConverter(options) {
}
var elementConverters = {
- "document": function(document, html, callback) {
- convertElements(document.children, html, callback);
+ "document": function(document, html, messages, callback) {
+ convertElements(document.children, html, messages, callback);
},
"paragraph": convertParagraph,
"run": convertRun,
- "text": function(element, html, callback) {
+ "text": function(element, html, messages, callback) {
html.text(element.value);
callback();
},
- "hyperlink": function(element, html, callback) {
+ "hyperlink": function(element, html, messages, callback) {
html.open(htmlPaths.element("a", {href: element.href}));
- convertElements(element.children, html, function(err) {
+ convertElements(element.children, html, messages, function(err) {
if (err) {
callback(err);
} else {
@@ -95,7 +100,7 @@ function DocumentConverter(options) {
}
});
},
- "image": function(element, html, callback) {
+ "image": function(element, html, messages, callback) {
element.read("base64").then(function(imageBuffer) {
// TODO: set alt attribute to wp:inline/wp:docPr/@descr
var src = "data:image/png;base64," + imageBuffer;
diff --git a/tests/document-to-html.tests.js b/tests/document-to-html.tests.js
index f1e6402..d63813d 100644
--- a/tests/document-to-html.tests.js
+++ b/tests/document-to-html.tests.js
@@ -85,6 +85,16 @@ describe('DocumentConverter', function() {
});
});
+ test('warning is emitted if style is unrecognised', function() {
+ var document = new documents.Document([
+ paragraphOfText("Hello.", "Heading1"),
+ ]);
+ var converter = new DocumentConverter();
+ return converter.convertToHtml(document).then(function(result) {
+ assert.deepEqual(result.messages, [results.warning("Unrecognised paragraph style: Heading1")]);
+ });
+ });
+
test('can use stacked styles to generate nested HTML elements', function() {
var document = new documents.Document([
paragraphOfText("Hello.", "Heading1")
diff --git a/tests/mammoth.tests.js b/tests/mammoth.tests.js
index 49bc02f..d8235ff 100644
--- a/tests/mammoth.tests.js
+++ b/tests/mammoth.tests.js
@@ -18,7 +18,8 @@ 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.value);
+ assert.equal(result.value, "<p>Walking on imported air</p>");
+ assert.deepEqual(result.messages, []);
});
});