aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore1
-rw-r--r--lib/index.js49
-rw-r--r--lib/xmlreader.js75
-rw-r--r--makefile4
-rw-r--r--package.json19
-rw-r--r--tests/mammoth.tests.js38
-rw-r--r--tests/test-data/simple/word/document.xml18
-rw-r--r--tests/testing.js10
-rw-r--r--tests/xmlreader.tests.js59
9 files changed, 273 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..07e6e47
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+/node_modules
diff --git a/lib/index.js b/lib/index.js
new file mode 100644
index 0000000..11364b3
--- /dev/null
+++ b/lib/index.js
@@ -0,0 +1,49 @@
+var q = require("q");
+var xmlreader = require("./xmlreader");
+
+exports.Converter = Converter;
+
+function Converter() {
+}
+
+Converter.prototype.convertToHtml = function(docxFile) {
+ return docxFile.read("word/document.xml").then(function(documentXmlString) {
+ return parseXml(documentXmlString);
+ }).then(function(documentXml) {
+ var body = documentXml.root.first("w:body");
+
+ var html = body.children.map(elementToHtml).join("");
+
+ return {
+ html: html
+ };
+ });
+}
+
+function parseXml(string) {
+ return xmlreader.read(string);
+}
+
+function elementToHtml(element) {
+ if (element.type === "element") {
+ var handler = elementConverters[element.name];
+ if (handler) {
+ return handler(element);
+ } else {
+ return "";
+ }
+ }
+}
+
+var elementConverters = {
+ "w:p": function(element) {
+ var innerHtml = element.children.map(elementToHtml).join("");
+ return "<p>" + innerHtml + "</p>";
+ },
+ "w:r": function(element) {
+ return element.children.map(elementToHtml).join("");
+ },
+ "w:t": function(element) {
+ return element.children[0].value;
+ }
+};
diff --git a/lib/xmlreader.js b/lib/xmlreader.js
new file mode 100644
index 0000000..67dda38
--- /dev/null
+++ b/lib/xmlreader.js
@@ -0,0 +1,75 @@
+var q = require("q");
+var sax = require("sax");
+var _ = require("underscore");
+
+exports.read = read;
+
+function read(xmlString) {
+ var parser = sax.parser(true, {xmlns: true, position: false});
+
+ //~ parser.onerror = function(error) {
+ //~ Q.fcall(function () {
+ //~ throw new Error("Can't do it");
+ //~ });
+ //~ };
+
+ var rootElement = {children: []};
+ var currentElement = rootElement;
+ var stack = [];
+
+ var deferred = q.defer();
+
+ parser.onopentag = function(node) {
+ var attributes = mapValues(node.attributes, function(attribute) {
+ return attribute.value;
+ });
+
+ var element = new Element(node.name, attributes);
+ currentElement.children.push(element);
+ stack.push(currentElement);
+ currentElement = element
+ };
+
+ parser.onclosetag = function(node) {
+ currentElement = stack.pop();
+ };
+
+ parser.ontext = function(text) {
+ if (currentElement !== rootElement) {
+ currentElement.children.push({
+ type: "text",
+ value: text
+ });
+ }
+ };
+
+ parser.onend = function() {
+ deferred.resolve({
+ root: rootElement.children[0]
+ });
+ };
+
+ parser.write(xmlString).close();
+
+ return deferred.promise;
+}
+
+function mapValues(input, mapper, context) {
+ return _.reduce(input, function (obj, v, k) {
+ obj[k] = mapper.call(context, v, k, input);
+ return obj;
+ }, {}, context);
+}
+
+function Element(name, attributes) {
+ this.type = "element";
+ this.name = name;
+ this.attributes = attributes;
+ this.children = [];
+}
+
+Element.prototype.first = function(name) {
+ return _.find(this.children, function(child) {
+ return child.name == name;
+ });
+}
diff --git a/makefile b/makefile
new file mode 100644
index 0000000..6fb8f49
--- /dev/null
+++ b/makefile
@@ -0,0 +1,4 @@
+.PHONY: test
+
+test:
+ node_modules/.bin/mocha tests
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..a13b491
--- /dev/null
+++ b/package.json
@@ -0,0 +1,19 @@
+{
+ "name": "mammoth",
+ "version": "0.1.0",
+ "author": "Michael Williamson <mike@zwobble.org>",
+ "description": "Convert Word documents to simple HTML",
+ "main": "./lib/index.js",
+ "repository": {
+ "type": "git",
+ "url": "https://github.com/mwilliamson/mammoth.js.git"
+ },
+ "dependencies": {
+ "q": "~0.9.2",
+ "sax": "~0.5.2",
+ "underscore": "~1.4.4"
+ },
+ "devDependencies": {
+ "mocha": "~1.8"
+ }
+}
diff --git a/tests/mammoth.tests.js b/tests/mammoth.tests.js
new file mode 100644
index 0000000..05ef218
--- /dev/null
+++ b/tests/mammoth.tests.js
@@ -0,0 +1,38 @@
+var assert = require("assert");
+var path = require("path");
+var fs = require("fs");
+var mammoth = require("../")
+var q = require("q");
+var test = require("./testing").test;
+
+
+describe('mammoth', function() {
+ test('should convert docx containing one one paragraph to single p element', function() {
+ var docxFile = createFakeDocxFile({
+ "word/document.xml": testData("simple/word/document.xml")
+ });
+ return convertToHtml(docxFile).then(function(result) {
+ assert.equal("<p>Hello.</p>", result.html);
+ });
+ })
+})
+
+function createFakeDocxFile(files) {
+ function read(path) {
+ return files[path];
+ }
+
+ return {
+ read: read
+ };
+}
+
+function testData(testDataPath) {
+ var fullPath = path.join(__dirname, "test-data", testDataPath);
+ return q.nfcall(fs.readFile, fullPath, "utf-8");
+}
+
+function convertToHtml(docxFile) {
+ var converter = new mammoth.Converter();
+ return converter.convertToHtml(docxFile);
+}
diff --git a/tests/test-data/simple/word/document.xml b/tests/test-data/simple/word/document.xml
new file mode 100644
index 0000000..1723398
--- /dev/null
+++ b/tests/test-data/simple/word/document.xml
@@ -0,0 +1,18 @@
+<?xml version="1.0" ?>
+<w:document mc:Ignorable="w14 wp14" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape">
+ <w:body>
+ <w:p w:rsidR="00E01ECE" w:rsidRDefault="000636A7">
+ <w:r>
+ <w:t>Hello.</w:t>
+ </w:r>
+ <w:bookmarkStart w:id="0" w:name="_GoBack"/>
+ <w:bookmarkEnd w:id="0"/>
+ </w:p>
+ <w:sectPr w:rsidR="00E01ECE">
+ <w:pgSz w:h="16838" w:w="11906"/>
+ <w:pgMar w:bottom="1440" w:footer="708" w:gutter="0" w:header="708" w:left="1440" w:right="1440" w:top="1440"/>
+ <w:cols w:space="708"/>
+ <w:docGrid w:linePitch="360"/>
+ </w:sectPr>
+ </w:body>
+</w:document>
diff --git a/tests/testing.js b/tests/testing.js
new file mode 100644
index 0000000..ac413c8
--- /dev/null
+++ b/tests/testing.js
@@ -0,0 +1,10 @@
+exports.test = test;
+
+function test(name, func) {
+ it(name, function(done) {
+ var result = func();
+ result.then(function() {
+ done()
+ }).done();
+ });
+}
diff --git a/tests/xmlreader.tests.js b/tests/xmlreader.tests.js
new file mode 100644
index 0000000..ad11e62
--- /dev/null
+++ b/tests/xmlreader.tests.js
@@ -0,0 +1,59 @@
+var assert = require("assert");
+
+var xmlreader = require("../lib/xmlreader");
+var test = require("./testing").test;
+
+
+describe('xmlreader.read', function() {
+ test('should read self-closing element', function() {
+ return xmlreader.read("<body/>").then(function(result) {
+ assert.deepEqual({type: "element", name: "body", attributes: {}, children: []}, result.root);
+ });
+ })
+
+ test('should read empty element with separate closing tag', function() {
+ return xmlreader.read("<body></body>").then(function(result) {
+ assert.deepEqual({type: "element", name: "body", attributes: {}, children: []}, result.root);
+ });
+ })
+
+ test('should read attributes of tags', function() {
+ return xmlreader.read('<body name="bob"/>').then(function(result) {
+ assert.deepEqual({name: "bob"}, result.root.attributes);
+ });
+ })
+
+ test('can read text element', function() {
+ return xmlreader.read('<body>Hello!</body>').then(function(result) {
+ assert.deepEqual({type: "text", value: "Hello!"}, result.root.children[0]);
+ });
+ })
+
+ test('should read element with children', function() {
+ return xmlreader.read("<body><a/><b/></body>").then(function(result) {
+ var root = result.root;
+ assert.equal(2, root.children.length);
+ assert.equal("a", root.children[0].name);
+ assert.equal("b", root.children[1].name);
+ });
+ })
+
+ test('should read element with namespace declaration', function() {
+ return xmlreader.read("<w:body/>").then(function(result) {
+ assert.deepEqual("w:body", result.root.name);
+ });
+ })
+
+ test('can find first element with name', function() {
+ return xmlreader.read('<body><a/><b index="1"/><b index="2"/></body>').then(function(result) {
+ var first = result.root.first("b");
+ assert.equal("1", first.attributes.index);
+ });
+ })
+
+ test('whitespace between xml declaration and root tag is ignored', function() {
+ return xmlreader.read('<?xml version="1.0" ?>\n<body/>').then(function(result) {
+ assert.deepEqual("body", result.root.name);
+ });
+ })
+});