aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2015-08-15 10:00:12 +0100
committerMichael Williamson <mike@zwobble.org>2015-08-15 10:00:12 +0100
commit57299a40f7517ac4934ac391b44656500bac024d (patch)
treee7028502e2e2f5450266ca90fc0942898417000a
parent1507381c0afbd2c2aaefe4fba8970c4a875886e9 (diff)
Generate warnings for not-understood style mappings and continue
-rw-r--r--NEWS3
-rw-r--r--lib/index.js18
-rw-r--r--lib/results.js5
-rw-r--r--lib/style-reader.js20
-rw-r--r--tests/mammoth.tests.js14
-rw-r--r--tests/style-reader.tests.js7
6 files changed, 54 insertions, 13 deletions
diff --git a/NEWS b/NEWS
index 7fdc2e3..ae800c4 100644
--- a/NEWS
+++ b/NEWS
@@ -2,6 +2,9 @@
* When the styleMap option is passed as a string, ignore lines beginning with #.
+* Generate warnings for not-understood style mappings and continue, rather than
+ stopping with an error.
+
# 0.3.24
* Ignore w:numPr elements without w:numId or w:ilvl children.
diff --git a/lib/index.js b/lib/index.js
index a988745..983a32a 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -4,6 +4,7 @@ var DocumentConverter = require("./document-to-html").DocumentConverter;
var readStyle = require("./style-reader").readStyle;
var readOptions = require("./options-reader").readOptions;
var unzip = require("./unzip");
+var Result = require("./results").Result;
exports.convertToHtml = convertToHtml;
exports.convertToMarkdown = convertToMarkdown;
@@ -50,13 +51,26 @@ function convert(input, options) {
function convertDocumentToHtml(documentResult, options) {
var parsedOptions = Object.create(options);
- parsedOptions.styleMap = (options.styleMap || []).map(readStyle);
+ var styleMapResult = parseStyleMap(options.styleMap);
+ parsedOptions.styleMap = styleMapResult.value;
var documentConverter = new DocumentConverter(parsedOptions);
+
return documentResult.flatMapThen(function(document) {
- return documentConverter.convertToHtml(document);
+ return styleMapResult.flatMapThen(function(styleMap) {
+ return documentConverter.convertToHtml(document);
+ });
});
}
+function parseStyleMap(styleMap) {
+ return Result.combine((styleMap || []).map(readStyle))
+ .map(function(styleMap) {
+ return styleMap.filter(function(styleMapping) {
+ return !!styleMapping;
+ });
+ });
+}
+
function extractRawText(input) {
return unzip.openZip(input)
diff --git a/lib/results.js b/lib/results.js
index bd66430..e86d171 100644
--- a/lib/results.js
+++ b/lib/results.js
@@ -2,6 +2,7 @@ var _ = require("underscore");
exports.Result = Result;
+exports.success = success;
exports.warning = warning;
@@ -32,6 +33,10 @@ Result.combine = function(results) {
return new Result(values, messages);
};
+function success(value) {
+ return new Result(value, []);
+}
+
function warning(message) {
return {
type: "warning",
diff --git a/lib/style-reader.js b/lib/style-reader.js
index 6a86eab..6d36c42 100644
--- a/lib/style-reader.js
+++ b/lib/style-reader.js
@@ -4,6 +4,7 @@ var RegexTokeniser = lop.RegexTokeniser;
var documentMatchers = require("./document-matchers");
var htmlPaths = require("./html-paths");
+var results = require("../lib/results");
exports.readHtmlPath = readHtmlPath;
exports.readDocumentMatcher = readDocumentMatcher;
@@ -195,18 +196,21 @@ function parseString(rule, string) {
var tokens = tokenise(string);
var parser = lop.Parser();
var parseResult = parser.parseTokens(rule, tokens);
- if (!parseResult.isSuccess()) {
- throw new Error("Failed to parse: " + describeFailure(parseResult));
+ if (parseResult.isSuccess()) {
+ return results.success(parseResult.value());
+ } else {
+ return new results.Result(null, [results.warning(describeFailure(string, parseResult))]);
}
- return parseResult.value();
}
-function describeFailure(parseResult) {
- return parseResult.errors().map(describeError).join("\n");
+function describeFailure(input, parseResult) {
+ return "Did not understand this style mapping, so ignored it: " + input + "\n" +
+ parseResult.errors().map(describeError).join("\n");
+}
- function describeError(error) {
- return error.describe();
- }
+function describeError(error) {
+ return "Error was at character number " + error.characterNumber() + ": " +
+ "Expected " + error.expected + " but got " + error.actual;
}
function tokenise(string) {
diff --git a/tests/mammoth.tests.js b/tests/mammoth.tests.js
index 6fdd60f..64439f1 100644
--- a/tests/mammoth.tests.js
+++ b/tests/mammoth.tests.js
@@ -4,6 +4,7 @@ var fs = require("fs");
var mammoth = require("../");
var promises = require("../lib/promises");
+var results = require("../lib/results");
var testing = require("./testing");
var test = testing.test;
@@ -95,6 +96,19 @@ describe('mammoth', function() {
});
});
+ test('warning if style mapping is not understood', function() {
+ var docxPath = path.join(__dirname, "test-data/single-paragraph.docx");
+ var options = {
+ styleMap: "!!!!\np => h1"
+ };
+ return mammoth.convertToHtml({path: docxPath}, options).then(function(result) {
+ assert.equal("<h1>Walking on imported air</h1>", result.value);
+ var warning = "Did not understand this style mapping, so ignored it: !!!!\n" +
+ 'Error was at character number 1: Expected element type but got unrecognisedCharacter "!"';
+ assert.deepEqual(result.messages, [results.warning(warning)]);
+ });
+ });
+
test('options are passed to document converter when calling mammoth.convertToHtml', function() {
var docxFile = createFakeDocxFile({
"word/document.xml": testData("simple/word/document.xml")
diff --git a/tests/style-reader.tests.js b/tests/style-reader.tests.js
index 5082a40..76d3f97 100644
--- a/tests/style-reader.tests.js
+++ b/tests/style-reader.tests.js
@@ -2,6 +2,7 @@ var assert = require("assert");
var htmlPaths = require("../lib/html-paths");
var documentMatchers = require("../lib/document-matchers");
var styleReader = require("../lib/style-reader");
+var results = require("../lib/results");
var test = require("./testing").test;
@@ -54,7 +55,7 @@ describe('styleReader.readHtmlPath', function() {
});
function assertHtmlPath(input, expected) {
- assert.deepEqual(readHtmlPath(input), expected);
+ assert.deepEqual(readHtmlPath(input), results.success(expected));
}
});
@@ -113,7 +114,7 @@ describe("styleReader.readDocumentMatcher", function() {
});
function assertDocumentMatcher(input, expected) {
- assert.deepEqual(readDocumentMatcher(input), expected);
+ assert.deepEqual(readDocumentMatcher(input), results.success(expected));
}
});
@@ -139,6 +140,6 @@ describe("styleReader.read", function() {
});
function assertStyleMapping(input, expected) {
- assert.deepEqual(readStyle(input), expected);
+ assert.deepEqual(readStyle(input), results.success(expected));
}
});