aboutsummaryrefslogtreecommitdiff
path: root/lib/docx
diff options
context:
space:
mode:
Diffstat (limited to 'lib/docx')
-rw-r--r--lib/docx/body-reader.js65
1 files changed, 34 insertions, 31 deletions
diff --git a/lib/docx/body-reader.js b/lib/docx/body-reader.js
index ca55bb7..dbb091c 100644
--- a/lib/docx/body-reader.js
+++ b/lib/docx/body-reader.js
@@ -54,6 +54,19 @@ function BodyReader(options) {
return emptyResult();
}
+ function readParagraphProperties(element) {
+ return readParagraphStyle(element).map(function(style) {
+ return {
+ type: "paragraphProperties",
+ styleId: style.styleId,
+ styleName: style.name,
+ alignment: element.firstOrEmpty("w:jc").attributes["w:val"],
+ numbering: readNumberingProperties(style.styleId, element.firstOrEmpty("w:numPr"), numbering),
+ indent: readParagraphIndent(element.firstOrEmpty("w:ind"))
+ };
+ });
+ }
+
function readParagraphIndent(element) {
return {
start: element.attributes["w:start"] || element.attributes["w:left"],
@@ -219,15 +232,14 @@ function BodyReader(options) {
var xmlElementReaders = {
"w:p": function(element) {
- var isDeleted = !!element
- .firstOrEmpty("w:pPr")
+ var paragraphPropertiesElement = element.firstOrEmpty("w:pPr");
+
+ var isDeleted = !!paragraphPropertiesElement
.firstOrEmpty("w:rPr")
.first("w:del");
if (isDeleted) {
- element.children.filter(function(child) {
- return child.name !== "w:pPr";
- }).forEach(function(child) {
+ element.children.forEach(function(child) {
deletedParagraphContents.push(child);
});
return emptyResult();
@@ -237,29 +249,15 @@ function BodyReader(options) {
childrenXml = deletedParagraphContents.concat(childrenXml);
deletedParagraphContents = [];
}
- return readXmlElements(childrenXml)
- .map(function(children) {
- var properties = _.find(children, isParagraphProperties);
- return new documents.Paragraph(
- children.filter(negate(isParagraphProperties)),
- properties
- );
- })
- .insertExtra();
+ return ReadResult.map(
+ readParagraphProperties(paragraphPropertiesElement),
+ readXmlElements(childrenXml),
+ function(properties, children) {
+ return new documents.Paragraph(children, properties);
+ }
+ ).insertExtra();
}
},
- "w:pPr": function(element) {
- return readParagraphStyle(element).map(function(style) {
- return {
- type: "paragraphProperties",
- styleId: style.styleId,
- styleName: style.name,
- alignment: element.firstOrEmpty("w:jc").attributes["w:val"],
- numbering: readNumberingProperties(style.styleId, element.firstOrEmpty("w:numPr"), numbering),
- indent: readParagraphIndent(element.firstOrEmpty("w:ind"))
- };
- });
- },
"w:r": function(element) {
return readXmlElements(element.children)
.map(function(children) {
@@ -594,16 +592,13 @@ var ignoreElements = {
"w:del": true,
"w:footnoteRef": true,
"w:endnoteRef": true,
+ "w:pPr": true,
"w:tblPr": true,
"w:tblGrid": true,
"w:trPr": true,
"w:tcPr": true
};
-function isParagraphProperties(element) {
- return element.type === "paragraphProperties";
-}
-
function isRunProperties(element) {
return element.type === "runProperties";
}
@@ -633,7 +628,7 @@ function elementResultWithMessages(element, messages) {
function ReadResult(element, extra, messages) {
this.value = element || [];
- this.extra = extra;
+ this.extra = extra || [];
this._result = new Result({
element: this.value,
extra: extra
@@ -668,6 +663,14 @@ ReadResult.prototype.flatMap = function(func) {
return new ReadResult(result.value.element, joinElements(this.extra, result.value.extra), result.messages);
};
+ReadResult.map = function(first, second, func) {
+ return new ReadResult(
+ func(first.value, second.value),
+ joinElements(first.extra, second.extra),
+ first.messages.concat(second.messages)
+ );
+};
+
function combineResults(results) {
var result = Result.combine(_.pluck(results, "_result"));
return new ReadResult(