aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2014-04-27 15:58:59 +0100
committerMichael Williamson <mike@zwobble.org>2014-04-27 15:58:59 +0100
commit869d5b2a725db8e41dc447227468c173d5292c18 (patch)
tree176267563f49df0d6804437c19dbbf62134569d2
parente9d9ab3c03b6018ba8b63335e6aa2b1809a188a5 (diff)
Remove mammoth.styleMapping
-rw-r--r--NEWS3
-rw-r--r--README.md15
-rw-r--r--lib/index.js7
-rw-r--r--lib/options-reader.js48
-rw-r--r--tests/mammoth.tests.js4
-rw-r--r--tests/options-reader.tests.js5
6 files changed, 39 insertions, 43 deletions
diff --git a/NEWS b/NEWS
index a812c13..30f358a 100644
--- a/NEWS
+++ b/NEWS
@@ -8,6 +8,9 @@
a paragraph with the style name `Heading 1`:
p[style-name='Heading 1']
+
+* Use an array of raw strings rather than an array of style mappings when
+ passing options to convertToHtml.
# 0.2.2
diff --git a/README.md b/README.md
index 0e2c047..dbebe56 100644
--- a/README.md
+++ b/README.md
@@ -103,12 +103,11 @@ and paragraphs with the style name `Subsection Title` should be converted to `h2
```javascript
var mammoth = require("mammoth");
-var styleMapping = mammoth.styleMapping;
var options = {
styleMap: [
- styleMapping("p[style-name='Section Title'] => h1:fresh"),
- styleMapping("p[style-name='Subsection Title'] => h2:fresh")
+ "p[style-name='Section Title'] => h1:fresh",
+ "p[style-name='Subsection Title'] => h2:fresh"
]
};
mammoth.convertToHtml({path: "path/to/document.docx"}, options);
@@ -132,8 +131,8 @@ set `options.includeDefaultStyleMap` to `false`:
```javascript
var options = {
styleMap: [
- styleMapping("p[style-name='Section Title'] => h1:fresh"),
- styleMapping("p[style-name='Subsection Title'] => h2:fresh")
+ "p[style-name='Section Title'] => h1:fresh",
+ "p[style-name='Subsection Title'] => h2:fresh"
],
includeDefaultStyleMap: false
};
@@ -187,7 +186,7 @@ Converts the source document to HTML.
If `options.styleMap` is a string,
each non-blank line is treated as a separate style mapping.
If `options.styleMap` is an array,
- each element is expected to be the result of a call to `mammoth.styleMapping`.
+ each element is expected to be a string representing a single style mapping.
See "Writing style maps" for a reference to the syntax for style maps.
* `includeDefaultStyleMap`: by default,
@@ -205,10 +204,6 @@ Converts the source document to HTML.
* `messages`: any messages, such as errors and warnings, generated during the conversion
-#### `mammoth.styleMapping(string)`
-
-Creates a style mapping using the passed string.
-
#### Messages
Each message has the following properties:
diff --git a/lib/index.js b/lib/index.js
index 6ccfd85..baae852 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -1,10 +1,9 @@
var docxReader = require("./docx/docx-reader");
var DocumentConverter = require("./document-to-html").DocumentConverter;
-var styleMapping = require("./style-reader").readStyle;
+var readStyle = require("./style-reader").readStyle;
var readOptions = require("./options-reader").readOptions;
exports.convertToHtml = convertToHtml;
-exports.styleMapping = styleMapping;
function convertToHtml(input, options) {
var fullOptions = readOptions(options);
@@ -19,7 +18,9 @@ function convertToHtml(input, options) {
}
function convertDocumentToHtml(documentResult, options) {
- var documentConverter = new DocumentConverter(options);
+ var parsedOptions = Object.create(options);
+ parsedOptions.styleMap = (options.styleMap || []).map(readStyle);
+ var documentConverter = new DocumentConverter(parsedOptions);
return documentResult.flatMapThen(function(document) {
return documentConverter.convertToHtml(document);
});
diff --git a/lib/options-reader.js b/lib/options-reader.js
index 0f7e38e..2c6ad4f 100644
--- a/lib/options-reader.js
+++ b/lib/options-reader.js
@@ -2,29 +2,28 @@ exports.readOptions = readOptions;
var _ = require("underscore");
-var style = require("./style-reader").readStyle;
var standardOptions = exports._standardOptions = {
styleMap: [
- style("p.Heading1 => h1:fresh"),
- style("p.Heading2 => h2:fresh"),
- style("p.Heading3 => h3:fresh"),
- style("p.Heading4 => h4:fresh"),
- style("p[style-name='Heading 1'] => h1:fresh"),
- style("p[style-name='Heading 2'] => h2:fresh"),
- style("p[style-name='Heading 3'] => h3:fresh"),
- style("p[style-name='Heading 4'] => h4:fresh"),
- style("p:unordered-list(1) => ul > li:fresh"),
- style("p:unordered-list(2) => ul|ol > li > ul > li:fresh"),
- style("p:unordered-list(3) => ul|ol > li > ul|ol > li > ul > li:fresh"),
- style("p:unordered-list(4) => ul|ol > li > ul|ol > li > ul|ol > li > ul > li:fresh"),
- style("p:unordered-list(5) => ul|ol > li > ul|ol > li > ul|ol > li > ul|ol > li > ul > li:fresh"),
- style("p:ordered-list(1) => ol > li:fresh"),
- style("p:ordered-list(2) => ul|ol > li > ol > li:fresh"),
- style("p:ordered-list(3) => ul|ol > li > ul|ol > li > ol > li:fresh"),
- style("p:ordered-list(4) => ul|ol > li > ul|ol > li > ul|ol > li > ol > li:fresh"),
- style("p:ordered-list(5) => ul|ol > li > ul|ol > li > ul|ol > li > ul|ol > li > ol > li:fresh")
+ "p.Heading1 => h1:fresh",
+ "p.Heading2 => h2:fresh",
+ "p.Heading3 => h3:fresh",
+ "p.Heading4 => h4:fresh",
+ "p[style-name='Heading 1'] => h1:fresh",
+ "p[style-name='Heading 2'] => h2:fresh",
+ "p[style-name='Heading 3'] => h3:fresh",
+ "p[style-name='Heading 4'] => h4:fresh",
+ "p:unordered-list(1) => ul > li:fresh",
+ "p:unordered-list(2) => ul|ol > li > ul > li:fresh",
+ "p:unordered-list(3) => ul|ol > li > ul|ol > li > ul > li:fresh",
+ "p:unordered-list(4) => ul|ol > li > ul|ol > li > ul|ol > li > ul > li:fresh",
+ "p:unordered-list(5) => ul|ol > li > ul|ol > li > ul|ol > li > ul|ol > li > ul > li:fresh",
+ "p:ordered-list(1) => ol > li:fresh",
+ "p:ordered-list(2) => ul|ol > li > ol > li:fresh",
+ "p:ordered-list(3) => ul|ol > li > ul|ol > li > ol > li:fresh",
+ "p:ordered-list(4) => ul|ol > li > ul|ol > li > ul|ol > li > ol > li:fresh",
+ "p:ordered-list(5) => ul|ol > li > ul|ol > li > ul|ol > li > ul|ol > li > ol > li:fresh"
],
transformDocument: identity,
includeDefaultStyleMap: true
@@ -35,15 +34,14 @@ function readOptions(options) {
var fullOptions = {};
_.extend(fullOptions, standardOptions, options);
- fullOptions.styleMap = readStyleMap(options)
- .concat(fullOptions.includeDefaultStyleMap ? standardOptions.styleMap : []);
+ fullOptions.styleMap = readStyleMap(options.styleMap, fullOptions);
return fullOptions;
}
-function readStyleMap(options) {
- var customStyleMap = readCustomStyleMap(options.styleMap);
- return customStyleMap;
+function readStyleMap(styleMap, options) {
+ var customStyleMap = readCustomStyleMap(styleMap);
+ return customStyleMap.concat(options.includeDefaultStyleMap ? standardOptions.styleMap : []);
}
function readCustomStyleMap(styleMap) {
@@ -51,7 +49,7 @@ function readCustomStyleMap(styleMap) {
return [];
} else if (_.isString(styleMap)) {
return styleMap.split("\n").map(function(line) {
- return style(line.trim());
+ return line.trim();
});
} else {
return styleMap;
diff --git a/tests/mammoth.tests.js b/tests/mammoth.tests.js
index cc08565..0c9519e 100644
--- a/tests/mammoth.tests.js
+++ b/tests/mammoth.tests.js
@@ -35,12 +35,12 @@ describe('mammoth', function() {
});
});
- test('style map can be expressed as array of styles', function() {
+ test('style map can be expressed as array of style mappings', function() {
var docxFile = createFakeDocxFile({
"word/document.xml": testData("simple/word/document.xml")
});
var options = {
- styleMap: [mammoth.styleMapping("p => h1")]
+ styleMap: ["p => h1"]
};
return mammoth.convertToHtml({file: docxFile}, options).then(function(result) {
assert.equal("<h1>Hello.</h1>", result.value);
diff --git a/tests/options-reader.tests.js b/tests/options-reader.tests.js
index fcfa1bf..1beb314 100644
--- a/tests/options-reader.tests.js
+++ b/tests/options-reader.tests.js
@@ -1,6 +1,5 @@
var assert = require("assert");
-var styleMapping = require("../").styleMapping;
var optionsReader = require("../lib/options-reader");
var standardOptions = optionsReader._standardOptions;
var readOptions = optionsReader.readOptions;
@@ -19,7 +18,7 @@ describe('readOptions', function() {
var options = readOptions({
styleMap: "p.SectionTitle => h2"
});
- assert.deepEqual(styleMapping("p.SectionTitle => h2"), options.styleMap[0]);
+ assert.deepEqual("p.SectionTitle => h2", options.styleMap[0]);
assert.deepEqual(standardOptions.styleMap, options.styleMap.slice(1));
});
@@ -28,7 +27,7 @@ describe('readOptions', function() {
styleMap: "p.SectionTitle => h2",
includeDefaultStyleMap: false
});
- assert.deepEqual([styleMapping("p.SectionTitle => h2")], options.styleMap);
+ assert.deepEqual(["p.SectionTitle => h2"], options.styleMap);
});
});