aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2014-01-06 22:06:44 +0000
committerMichael Williamson <mike@zwobble.org>2014-01-06 22:06:44 +0000
commitad7500f87900f3db170c11c6cf997e0c7493babc (patch)
tree3721cfd360f6eb48dbc40100098a1e489825f0ba
parent552f0309fcc96fdf12dd2ed9db309093ba3af281 (diff)
Reduce to a single export
-rw-r--r--lib/index.js40
-rw-r--r--package.json3
-rw-r--r--tests/mammoth.tests.js36
3 files changed, 28 insertions, 51 deletions
diff --git a/lib/index.js b/lib/index.js
index 0cb55f9..0f45501 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -1,16 +1,11 @@
+var extend = require("xtend");
+
var docxReader = require("./docx-reader");
var DocumentConverter = require("./document-to-html").DocumentConverter;
-var htmlPaths = require("./html-paths");
-var documentMatchers = require("./document-matchers");
var style = require("./style-reader").readStyle;
-exports.Converter = Converter;
exports.convertToHtml = convertToHtml;
-exports.read = read;
-exports.convertDocumentToHtml = convertDocumentToHtml;
-exports.htmlPaths = htmlPaths;
-exports.style = style;
-var standardOptions = exports.standardOptions = {
+var standardOptions = {
styleMap: [
style("p.Heading1 => h1:fresh"),
style("p.Heading2 => h2:fresh"),
@@ -29,27 +24,22 @@ var standardOptions = exports.standardOptions = {
]
};
-function Converter(options) {
- this._options = options || standardOptions;
-}
-
-Converter.prototype.convertToHtml = function(inputOptions) {
- var options = this._options;
- return read(inputOptions)
+function convertToHtml(input, options) {
+ options = options || {};
+ if (options.styleMap) {
+ options.styleMap = options.styleMap.split("\n").map(function(line) {
+ return style(line.trim());
+ });
+ }
+
+ var fullOptions = extend(standardOptions, options);
+
+ return docxReader.read(input)
.then(function(documentResult) {
- return convertDocumentToHtml(documentResult, options);
+ return convertDocumentToHtml(documentResult, fullOptions);
});
}
-function convertToHtml(input, options) {
- var converter = new Converter(options);
- return converter.convertToHtml(input);
-}
-
-function read(inputOptions) {
- return docxReader.read(inputOptions);
-}
-
function convertDocumentToHtml(documentResult, options) {
var documentConverter = new DocumentConverter(options);
return documentResult.flatMapThen(function(document) {
diff --git a/package.json b/package.json
index 76a6a2b..a99b6fb 100644
--- a/package.json
+++ b/package.json
@@ -25,7 +25,8 @@
"dustjs-linkedin": "~1.2.1",
"async": "~0.2.6",
"buffers": "~0.1.1",
- "stream-buffers": "~0.2.4"
+ "stream-buffers": "~0.2.4",
+ "xtend": "~2.1.2"
},
"devDependencies": {
"mocha": "~1.8",
diff --git a/tests/mammoth.tests.js b/tests/mammoth.tests.js
index a7385ca..c78bc19 100644
--- a/tests/mammoth.tests.js
+++ b/tests/mammoth.tests.js
@@ -15,16 +15,15 @@ var createFakeDocxFile = testing.createFakeDocxFile;
describe('mammoth', function() {
- test('should convert docx containing one one paragraph to single p element', function() {
+ test('should convert docx containing one paragraph to single p element', function() {
var docxPath = path.join(__dirname, "test-data/single-paragraph.docx");
- var converter = new mammoth.Converter();
- return converter.convertToHtml({path: docxPath}).then(function(result) {
+ return mammoth.convertToHtml({path: docxPath}).then(function(result) {
assert.equal(result.value, "<p>Walking on imported air</p>");
assert.deepEqual(result.messages, []);
});
});
- test('should convert Uint8Array containing one one paragraph to single p element', function() {
+ test('should convert Uint8Array containing one paragraph to single p element', function() {
var docxPath = path.join(__dirname, "test-data/single-paragraph.docx");
return q.nfcall(fs.readFile, docxPath).then(function(buffer) {
@@ -32,8 +31,7 @@ describe('mammoth', function() {
for (var i = 0; i < buffer.length; i++) {
array[i] = buffer[i];
}
- var converter = new mammoth.Converter();
- return converter.convertToHtml({uint8Array: array}).then(function(result) {
+ return mammoth.convertToHtml({uint8Array: array}).then(function(result) {
assert.equal("<p>Walking on imported air</p>", result.value);
});
});
@@ -43,15 +41,10 @@ describe('mammoth', function() {
var docxFile = createFakeDocxFile({
"word/document.xml": testData("simple/word/document.xml")
});
- var converter = new mammoth.Converter({
- styleMap: [
- {
- from: documentMatchers.paragraph(),
- to: htmlPaths.topLevelElement("h1")
- }
- ]
- });
- return converter.convertToHtml({file: docxFile}).then(function(result) {
+ var options = {
+ styleMap: "p => h1"
+ };
+ return mammoth.convertToHtml({file: docxFile}, options).then(function(result) {
assert.equal("<h1>Hello.</h1>", result.value);
});
});
@@ -61,12 +54,7 @@ describe('mammoth', function() {
"word/document.xml": testData("simple/word/document.xml")
});
var options = {
- styleMap: [
- {
- from: documentMatchers.paragraph(),
- to: htmlPaths.topLevelElement("h1")
- }
- ]
+ styleMap: "p => h1"
};
return mammoth.convertToHtml({file: docxFile}, options).then(function(result) {
assert.equal("<h1>Hello.</h1>", result.value);
@@ -74,17 +62,15 @@ describe('mammoth', function() {
});
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({path: docxPath}).then(function(result) {
+ return mammoth.convertToHtml({path: docxPath}).then(function(result) {
assert.equal(result.value, '<p><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAOvgAADr4B6kKxwAAAABNJREFUKFNj/M+ADzDhlWUYqdIAQSwBE8U+X40AAAAASUVORK5CYII=" /></p>');
});
});
test('simple list is converted to list elements', function() {
- var converter = new mammoth.Converter();
var docxPath = path.join(__dirname, "test-data/simple-list.docx");
- return converter.convertToHtml({path: docxPath}).then(function(result) {
+ return mammoth.convertToHtml({path: docxPath}).then(function(result) {
assert.equal(result.value, '<ul><li>Apple</li><li>Banana</li></ul>');
});
});