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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
exports.read = read;
exports.readXmlElement = readXmlElement;
var q = require("q");
var _ = require("underscore");
var ZipFile = require("zipfile").ZipFile;
var xmlreader = require("./xmlreader");
var documents = require("./documents");
function read(docxFile) {
if (_.isString(docxFile)) {
var zipFile = new ZipFile(docxFile);
docxFile = {
read: function(path) {
return q.ninvoke(zipFile, "readFile", path, "utf-8")
.then(function(value) {
return value.toString();
});
}
};
}
return docxFile.read("word/document.xml")
.then(xmlreader.read)
.then(convertXmlToDocument);
}
function convertXmlToDocument(documentXml) {
var body = documentXml.root.first("w:body");
var result = readXmlElements(body.children)
.map(function(children) {
return new documents.Document(children);
});
result.document = result.value;
return result;
}
function readXmlElements(elements) {
var results = elements.map(readXmlElement);
var values = _.pluck(results, "value").filter(notNull);
var messages = _.flatten(_.pluck(results, "messages"), true);
return new Result(values, messages);
}
function readXmlElement(element) {
if (element.type === "element") {
var handler = xmlElementReaders[element.name];
if (handler) {
return handler(element);
} else {
return new Result(
null,
[warning("An unrecognised element was ignored: " + element.name)]
);
}
} else {
return new Result(null);
}
}
var xmlElementReaders = {
"w:p": function(element) {
return readXmlElements(element.children)
.map(function(children) {
var properties = _.find(children, isParagraphProperties);
return new documents.Paragraph(
children.filter(negate(isParagraphProperties)),
properties
);
});
},
"w:pPr": function(element) {
var properties = {
type: "paragraphProperties"
};
var styleElement = element.first("w:pStyle");
if (styleElement) {
properties.styleName = styleElement.attributes["w:val"];
}
return new Result(properties);
},
"w:r": function(element) {
return readXmlElements(element.children)
.map(function(children) {
var properties = _.find(children, isRunProperties);
return new documents.Run(
children.filter(negate(isRunProperties)),
properties
);
});
},
"w:rPr": function(element) {
var properties = {
type: "runProperties"
};
var styleElement = element.first("w:rStyle");
if (styleElement) {
properties.styleName = styleElement.attributes["w:val"];
}
properties.isBold = !!element.first("w:b");
properties.isItalic = !!element.first("w:i");
return new Result(properties);
},
"w:t": function(element) {
return new Result(new documents.Text(element.children[0].value));
}
};
function parseXml(string) {
return xmlreader.read(string);
}
function notNull(value) {
return value !== null;
}
function isParagraphProperties(element) {
return element.type === "paragraphProperties";
}
function isRunProperties(element) {
return element.type === "runProperties";
}
function negate(predicate) {
return function(value) {
return !predicate(value);
};
}
function Result(value, messages) {
this.value = value;
this.messages = messages || [];
}
Result.prototype.map = function(func) {
return new Result(func(this.value), this.messages);
};
Result.prototype.mapThen = function(func) {
var messages = this.messages;
return func(this.value).then(function(resultValue) {
return new Result(resultValue, messages);
});
};
function warning(message) {
return {
type: "warning",
message: message
};
}
|