aboutsummaryrefslogtreecommitdiff
path: root/lib/docx-reader.js
blob: ecaa10cc5082ee67ff53228f4bf323d4a2695782 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
exports.read = read;

var q = require("q");
var _ = require("underscore");
var ZipFile = require("zipfile").ZipFile;
var path = require("path");

var xmlreader = require("./xmlreader");
var documents = require("./documents");
var DocumentXmlReader = require("./document-xml-reader").DocumentXmlReader;
var Result = require("./results").Result;


function read(docxFile) {
    if (_.isString(docxFile)) {
        var zipFile = new ZipFile(docxFile);
        var exists = function(path) {
            return zipFile.names.indexOf(path) !== -1;
        }
        docxFile = {
            read: function(path, encoding) {
                if (exists(path)) {
                    return q.ninvoke(zipFile, "readFile", path)
                        .then(function(value) {
                            if (encoding) {
                                return value.toString(encoding);
                            } else {
                                return value;
                            }
                        });
                } else {
                    return q.reject(new Error("No file in zip: " + path));
                }
            },
            exists: exists
        };
    }
    
    return q.all([
        readXmlFile(docxFile, "word/_rels/document.xml.rels"),
        readXmlFile(docxFile, "word/document.xml")
    ]).spread(function(relationshipsXml, documentXml) {
        var relationships = relationshipsXml ? readRelationships(relationshipsXml) : {};
        var reader = new DocumentXmlReader(relationships, docxFile);
        return reader.convertXmlToDocument(documentXml);
    });
}

function readXmlFile(docxFile, path) {
    if (docxFile.exists(path)) {
        return docxFile.read(path, "utf-8")
            .then(xmlreader.read);
    } else {
        return null;
    }
}

function readRelationships(relationshipsXml) {
    var relationships = {};
    relationshipsXml.root.children.forEach(function(child) {
        if (child.name === "Relationship") {
            relationships[child.attributes.Id] = {
                target: child.attributes.Target
            };
        }
    });
    return relationships;
}

function parseXml(string) {
    return xmlreader.read(string);
}