aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2013-03-30 22:26:18 +0000
committerMichael Williamson <mike@zwobble.org>2013-03-30 22:26:18 +0000
commit099c86ab5fabea429eb226fbff179f0317ec755c (patch)
tree7d633240cc4c8de39da667bab1d9aaf6584a3701
parentc127f0d75175d2ab06682bac1bb22dcf31de2b81 (diff)
Read style names from paragraph properties
-rw-r--r--lib/docx-reader.js34
-rw-r--r--tests/docx-reader.tests.js20
-rw-r--r--tests/test-data/paragraphStyles/word/document.xml21
3 files changed, 74 insertions, 1 deletions
diff --git a/lib/docx-reader.js b/lib/docx-reader.js
index 3078d2f..606c61b 100644
--- a/lib/docx-reader.js
+++ b/lib/docx-reader.js
@@ -53,7 +53,30 @@ function readXmlElement(element) {
var xmlElementReaders = {
"w:p": function(element) {
var children = readXmlElements(element.children);
- return new documents.Paragraph(children);
+ var properties;
+ var propertiesElement = _.find(children, isParagraphProperties);
+ if (propertiesElement) {
+ properties = propertiesElement.properties;
+ }
+
+ return new documents.Paragraph(
+ children.filter(negate(isParagraphProperties)),
+ properties
+ );
+ },
+ "w:pPr": function(element) {
+ var properties = {
+ };
+
+ var styleElement = element.first("w:pStyle");
+ if (styleElement) {
+ properties.styleName = styleElement.attributes["w:val"];
+ }
+
+ return {
+ type: "paragraphProperties",
+ properties: properties
+ };
},
"w:r": function(element) {
var children = readXmlElements(element.children);
@@ -72,3 +95,12 @@ function notNull(value) {
return value !== null;
}
+function isParagraphProperties(element) {
+ return element.type === "paragraphProperties";
+}
+
+function negate(predicate) {
+ return function(value) {
+ return !predicate(value);
+ };
+}
diff --git a/tests/docx-reader.tests.js b/tests/docx-reader.tests.js
index b7e1c29..c486f3b 100644
--- a/tests/docx-reader.tests.js
+++ b/tests/docx-reader.tests.js
@@ -25,4 +25,24 @@ describe("docx-reader", function() {
assert.deepEqual(expectedDocument, result);
});
});
+
+ test("can read paragraph styles", function() {
+ var docxFile = createFakeDocxFile({
+ "word/document.xml": testData("paragraphStyles/word/document.xml")
+ });
+ return docxReader.read(docxFile).then(function(result) {
+ var paragraph = result.children[0];
+ assert.deepEqual({styleName: "Heading1"}, paragraph.properties);
+ });
+ });
+
+ test("paragraph properties are not included as child of paragraph", function() {
+ var docxFile = createFakeDocxFile({
+ "word/document.xml": testData("paragraphStyles/word/document.xml")
+ });
+ return docxReader.read(docxFile).then(function(result) {
+ var paragraph = result.children[0];
+ assert.equal(1, paragraph.children.length);
+ });
+ });
});
diff --git a/tests/test-data/paragraphStyles/word/document.xml b/tests/test-data/paragraphStyles/word/document.xml
new file mode 100644
index 0000000..f7ab26d
--- /dev/null
+++ b/tests/test-data/paragraphStyles/word/document.xml
@@ -0,0 +1,21 @@
+<?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:pPr>
+ <w:pStyle w:val="Heading1"/>
+ </w:pPr>
+ <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>