aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2014-01-07 21:37:21 +0000
committerMichael Williamson <mike@zwobble.org>2014-01-07 21:37:21 +0000
commitd9d55dece1861bbbf08f3688254447992cf1957f (patch)
tree6c56fd5f00ebf4de9a8fbc0f202836dc262e84a3
parent53e9f9200a781e488830c3c01ff58f50d667a812 (diff)
parent51c625fb8474bcf790a0341ae5bbabb1316a2583 (diff)
Merge remote-tracking branch 'offlein/master'
Conflicts: lib/index.js
-rw-r--r--lib/document-xml-reader.js4
-rw-r--r--lib/documents.js3
-rw-r--r--lib/index.js7
-rw-r--r--tests/style-applicator.tests.js57
4 files changed, 70 insertions, 1 deletions
diff --git a/lib/document-xml-reader.js b/lib/document-xml-reader.js
index f4ab760..0dcd624 100644
--- a/lib/document-xml-reader.js
+++ b/lib/document-xml-reader.js
@@ -65,6 +65,10 @@ function DocumentXmlReader(relationships, contentTypes, docxFile, numbering) {
if (styleElement) {
properties.styleName = styleElement.attributes["w:val"];
}
+ var alignElement = element.first("w:jc");
+ if (alignElement) {
+ properties.alignment = alignElement.attributes["w:val"];
+ }
var numberingPropertiesElement = element.first("w:numPr");
if (numberingPropertiesElement) {
var levelElement = numberingPropertiesElement.first("w:ilvl");
diff --git a/lib/documents.js b/lib/documents.js
index 4dfbe74..3af5bd5 100644
--- a/lib/documents.js
+++ b/lib/documents.js
@@ -11,7 +11,8 @@ function Paragraph(children, properties) {
type: "paragraph",
children: children,
styleName: properties.styleName || null,
- numbering: properties.numbering || null
+ numbering: properties.numbering || null,
+ alignment: properties.alignment || null
};
}
diff --git a/lib/index.js b/lib/index.js
index bec626c..1928424 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -38,6 +38,13 @@ function convertToHtml(input, options) {
return docxReader.read(input)
.then(function(documentResult) {
+ if (fullOptions.transformDocument) {
+ return fullOptions.transformDocument(documentResult);
+ } else {
+ return documentResult;
+ }
+ })
+ .then(function(documentResult) {
return convertDocumentToHtml(documentResult, fullOptions);
});
}
diff --git a/tests/style-applicator.tests.js b/tests/style-applicator.tests.js
new file mode 100644
index 0000000..3164fa2
--- /dev/null
+++ b/tests/style-applicator.tests.js
@@ -0,0 +1,57 @@
+var assert = require("assert");
+
+var documents = require("../lib/documents");
+
+var testing = require("./testing");
+var test = testing.test;
+var style = require("../lib/style-reader").readStyle;
+
+
+describe("style-applicator", function() {
+ test("can use custom documentTransform to process styleless center-aligned paragraph into h2.centerAlign", function() {
+ var testOptions = {
+ styleMap: [
+ style("p.centerAligned => h2.sectionHeader:fresh")
+ ],
+ transformDocument: transformDocument
+ };
+ var testDocument = documents.Document([
+ documents.Paragraph([
+ documents.Run([
+ documents.Text("Test.")
+ ])
+ ], {alignment: 'center'})
+ ]);
+ var expectedDocument = documents.Document([
+ documents.Paragraph([
+ documents.Run([
+ documents.Text("Test.")
+ ])
+ ], {styleName: 'centerAligned', alignment: 'center'})
+ ]);
+
+ assert.deepEqual(expectedDocument, transformDocument(testDocument));
+ });
+});
+
+function transformDocument(document) {
+ if (document.children) {
+ for (var i = 0; i < document.children.length; i++) {
+ var element = document.children[i];
+ switch (element.type) {
+ case 'paragraph':
+ document.children[i] = processParagraphStyles(document.children[i]);
+ break;
+ }
+ }
+ }
+ return document;
+}
+function processParagraphStyles(element) {
+ if (!element.styleName) {
+ if (element.alignment) {
+ element.styleName = element.alignment+"Aligned";
+ }
+ }
+ return element;
+} \ No newline at end of file