From dd28d971d67ce558b86ac12f01d38e9ff21c91b7 Mon Sep 17 00:00:00 2001 From: Craig Leinoff Date: Fri, 3 Jan 2014 12:42:31 -0500 Subject: Adding fixed "p.CenteredHeading" in the event of a centered heading, which will, by default, create an h2.centered tag. --- lib/document-xml-reader.js | 5 +++++ lib/documents.js | 3 ++- lib/index.js | 1 + 3 files changed, 8 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/document-xml-reader.js b/lib/document-xml-reader.js index f4ab760..418c45e 100644 --- a/lib/document-xml-reader.js +++ b/lib/document-xml-reader.js @@ -65,6 +65,11 @@ 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"]; + properties.styleName = "CenteredHeading"; + } 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..193bb4f 100644 --- a/lib/documents.js +++ b/lib/documents.js @@ -22,7 +22,8 @@ function Run(children, properties) { children: children, styleName: properties.styleName || null, isBold: properties.isBold, - isItalic: properties.isItalic + isItalic: properties.isItalic, + alignment: properties.alignment || null }; } diff --git a/lib/index.js b/lib/index.js index 0cb55f9..4c35f31 100644 --- a/lib/index.js +++ b/lib/index.js @@ -16,6 +16,7 @@ var standardOptions = exports.standardOptions = { style("p.Heading2 => h2:fresh"), style("p.Heading3 => h3:fresh"), style("p.Heading4 => h4:fresh"), + style("p.CenteredHeading => h2.centered:fresh"), style("p:unordered-list(1) => ul > li:fresh"), style("p:unordered-list(2) => ul|ol > li > ul > li:fresh"), style("p:unordered-list(3) => ul|ol > li > ul|ol > li > ul > li:fresh"), -- cgit v1.2.3 From 0e8c64944707701480ff154187a70457a2253349 Mon Sep 17 00:00:00 2001 From: Craig Leinoff Date: Fri, 3 Jan 2014 12:47:27 -0500 Subject: Only applying CenteredHeading style if the element was actually centered. --- lib/document-xml-reader.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/document-xml-reader.js b/lib/document-xml-reader.js index 418c45e..764dc11 100644 --- a/lib/document-xml-reader.js +++ b/lib/document-xml-reader.js @@ -68,7 +68,9 @@ function DocumentXmlReader(relationships, contentTypes, docxFile, numbering) { var alignElement = element.first("w:jc"); if (alignElement) { properties.alignment = alignElement.attributes["w:val"]; - properties.styleName = "CenteredHeading"; + if (properties.alignment == 'center') { + properties.styleName = "CenteredHeading"; + } } var numberingPropertiesElement = element.first("w:numPr"); if (numberingPropertiesElement) { -- cgit v1.2.3 From e03ab48068c2850b852462581f5be23aada68bd7 Mon Sep 17 00:00:00 2001 From: Craig Leinoff Date: Fri, 3 Jan 2014 12:51:33 -0500 Subject: Don't set styleName from alignment if already set. --- lib/document-xml-reader.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/document-xml-reader.js b/lib/document-xml-reader.js index 764dc11..1f32597 100644 --- a/lib/document-xml-reader.js +++ b/lib/document-xml-reader.js @@ -68,7 +68,7 @@ function DocumentXmlReader(relationships, contentTypes, docxFile, numbering) { var alignElement = element.first("w:jc"); if (alignElement) { properties.alignment = alignElement.attributes["w:val"]; - if (properties.alignment == 'center') { + if (properties.alignment == 'center' && !properties.styleName) { properties.styleName = "CenteredHeading"; } } -- cgit v1.2.3 From 24b0789dfacffd38fc16724ea1b2b8a14b49b29d Mon Sep 17 00:00:00 2001 From: Craig Leinoff Date: Mon, 6 Jan 2014 18:10:42 -0500 Subject: Fixing initialization of "alignment" to paragraphs from runs --- lib/documents.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/documents.js b/lib/documents.js index 193bb4f..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 }; } @@ -22,8 +23,7 @@ function Run(children, properties) { children: children, styleName: properties.styleName || null, isBold: properties.isBold, - isItalic: properties.isItalic, - alignment: properties.alignment || null + isItalic: properties.isItalic }; } -- cgit v1.2.3 From d9fbde9f0b200da41888df0189e0b83f983ed46a Mon Sep 17 00:00:00 2001 From: Craig Leinoff Date: Mon, 6 Jan 2014 18:11:33 -0500 Subject: Adding heuristicis processing as separate step. --- lib/document-xml-reader.js | 3 --- lib/heuristics-processor.js | 28 ++++++++++++++++++++++++++++ lib/index.js | 9 +++++++++ 3 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 lib/heuristics-processor.js (limited to 'lib') diff --git a/lib/document-xml-reader.js b/lib/document-xml-reader.js index 1f32597..0dcd624 100644 --- a/lib/document-xml-reader.js +++ b/lib/document-xml-reader.js @@ -68,9 +68,6 @@ function DocumentXmlReader(relationships, contentTypes, docxFile, numbering) { var alignElement = element.first("w:jc"); if (alignElement) { properties.alignment = alignElement.attributes["w:val"]; - if (properties.alignment == 'center' && !properties.styleName) { - properties.styleName = "CenteredHeading"; - } } var numberingPropertiesElement = element.first("w:numPr"); if (numberingPropertiesElement) { diff --git a/lib/heuristics-processor.js b/lib/heuristics-processor.js new file mode 100644 index 0000000..1c49f9d --- /dev/null +++ b/lib/heuristics-processor.js @@ -0,0 +1,28 @@ +exports.HeuristicsProcessor = HeuristicsProcessor; + +var q = require("q"); + +function HeuristicsProcessor() { + function processHeuristics(document) { + for (var i = 0; i < document.value.children.length; i++) { + var element = document.value.children[i]; + switch (element.type) { + case 'paragraph': + document.value.children[i] = processParagraph(document.value.children[i]); + break; + } + } + return document; + } + + function processParagraph(element) { + if (element.alignment == 'center' && !element.styleName) { + element.styleName = "CenteredHeading"; + } + return element; + } + + return { + processHeuristics: processHeuristics + } +} \ No newline at end of file diff --git a/lib/index.js b/lib/index.js index 4c35f31..65ec467 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,4 +1,5 @@ var docxReader = require("./docx-reader"); +var HeuristicsProcessor = require("./heuristics-processor").HeuristicsProcessor; var DocumentConverter = require("./document-to-html").DocumentConverter; var htmlPaths = require("./html-paths"); var documentMatchers = require("./document-matchers"); @@ -37,6 +38,9 @@ function Converter(options) { Converter.prototype.convertToHtml = function(inputOptions) { var options = this._options; return read(inputOptions) + .then(function(documentResult) { + return processHeuristics(documentResult); + }) .then(function(documentResult) { return convertDocumentToHtml(documentResult, options); }); @@ -51,6 +55,11 @@ function read(inputOptions) { return docxReader.read(inputOptions); } +function processHeuristics(documentResult) { + var heuristicsProcessor = new HeuristicsProcessor(); + return heuristicsProcessor.processHeuristics(documentResult); +} + function convertDocumentToHtml(documentResult, options) { var documentConverter = new DocumentConverter(options); return documentResult.flatMapThen(function(document) { -- cgit v1.2.3 From 6b6872357118572f7243967888e1f4c621db0f99 Mon Sep 17 00:00:00 2001 From: Craig Leinoff Date: Mon, 6 Jan 2014 18:21:11 -0500 Subject: Removing q from heuristics processor where it's unneeded. --- lib/heuristics-processor.js | 2 -- 1 file changed, 2 deletions(-) (limited to 'lib') diff --git a/lib/heuristics-processor.js b/lib/heuristics-processor.js index 1c49f9d..0e3c97d 100644 --- a/lib/heuristics-processor.js +++ b/lib/heuristics-processor.js @@ -1,7 +1,5 @@ exports.HeuristicsProcessor = HeuristicsProcessor; -var q = require("q"); - function HeuristicsProcessor() { function processHeuristics(document) { for (var i = 0; i < document.value.children.length; i++) { -- cgit v1.2.3 From b3cc8c0c5ce2aa2e556e995d587da5a00c6a3185 Mon Sep 17 00:00:00 2001 From: Craig Leinoff Date: Tue, 7 Jan 2014 11:50:49 -0500 Subject: Renaming heuristics-processor to style-applicator --- lib/heuristics-processor.js | 26 -------------------------- lib/style-applicator.js | 26 ++++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 26 deletions(-) delete mode 100644 lib/heuristics-processor.js create mode 100644 lib/style-applicator.js (limited to 'lib') diff --git a/lib/heuristics-processor.js b/lib/heuristics-processor.js deleted file mode 100644 index 0e3c97d..0000000 --- a/lib/heuristics-processor.js +++ /dev/null @@ -1,26 +0,0 @@ -exports.HeuristicsProcessor = HeuristicsProcessor; - -function HeuristicsProcessor() { - function processHeuristics(document) { - for (var i = 0; i < document.value.children.length; i++) { - var element = document.value.children[i]; - switch (element.type) { - case 'paragraph': - document.value.children[i] = processParagraph(document.value.children[i]); - break; - } - } - return document; - } - - function processParagraph(element) { - if (element.alignment == 'center' && !element.styleName) { - element.styleName = "CenteredHeading"; - } - return element; - } - - return { - processHeuristics: processHeuristics - } -} \ No newline at end of file diff --git a/lib/style-applicator.js b/lib/style-applicator.js new file mode 100644 index 0000000..0e3c97d --- /dev/null +++ b/lib/style-applicator.js @@ -0,0 +1,26 @@ +exports.HeuristicsProcessor = HeuristicsProcessor; + +function HeuristicsProcessor() { + function processHeuristics(document) { + for (var i = 0; i < document.value.children.length; i++) { + var element = document.value.children[i]; + switch (element.type) { + case 'paragraph': + document.value.children[i] = processParagraph(document.value.children[i]); + break; + } + } + return document; + } + + function processParagraph(element) { + if (element.alignment == 'center' && !element.styleName) { + element.styleName = "CenteredHeading"; + } + return element; + } + + return { + processHeuristics: processHeuristics + } +} \ No newline at end of file -- cgit v1.2.3 From b688f825e40c4bd20d645a9f145e4e445a4f66f0 Mon Sep 17 00:00:00 2001 From: Craig Leinoff Date: Tue, 7 Jan 2014 11:51:29 -0500 Subject: Renaming heuristics processing code to style applicator code. --- lib/index.js | 24 +++++++++++++++++------- lib/style-applicator.js | 20 ++++++++++++-------- 2 files changed, 29 insertions(+), 15 deletions(-) (limited to 'lib') diff --git a/lib/index.js b/lib/index.js index 65ec467..bf12eb0 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,5 +1,5 @@ var docxReader = require("./docx-reader"); -var HeuristicsProcessor = require("./heuristics-processor").HeuristicsProcessor; +var StyleApplicator = require("./style-applicator").StyleApplicator; var DocumentConverter = require("./document-to-html").DocumentConverter; var htmlPaths = require("./html-paths"); var documentMatchers = require("./document-matchers"); @@ -17,7 +17,6 @@ var standardOptions = exports.standardOptions = { style("p.Heading2 => h2:fresh"), style("p.Heading3 => h3:fresh"), style("p.Heading4 => h4:fresh"), - style("p.CenteredHeading => h2.centered:fresh"), style("p:unordered-list(1) => ul > li:fresh"), style("p:unordered-list(2) => ul|ol > li > ul > li:fresh"), style("p:unordered-list(3) => ul|ol > li > ul|ol > li > ul > li:fresh"), @@ -28,7 +27,10 @@ var standardOptions = exports.standardOptions = { style("p:ordered-list(3) => ul|ol > li > ul|ol > li > ol > li:fresh"), style("p:ordered-list(4) => ul|ol > li > ul|ol > li > ul|ol > li > ol > li:fresh"), style("p:ordered-list(5) => ul|ol > li > ul|ol > li > ul|ol > li > ul|ol > li > ol > li:fresh") - ] + ], + styleApplicator: { + alignment: true + } }; function Converter(options) { @@ -39,7 +41,15 @@ Converter.prototype.convertToHtml = function(inputOptions) { var options = this._options; return read(inputOptions) .then(function(documentResult) { - return processHeuristics(documentResult); + var styleApplicatorEnabled = false; + for (var optionName in options.styleApplicator) { + if (options.styleApplicator[optionName] == true) { + styleApplicatorEnabled = true; + } + } + if (styleApplicatorEnabled == true) { + return applyStyles(documentResult, options); + } }) .then(function(documentResult) { return convertDocumentToHtml(documentResult, options); @@ -55,9 +65,9 @@ function read(inputOptions) { return docxReader.read(inputOptions); } -function processHeuristics(documentResult) { - var heuristicsProcessor = new HeuristicsProcessor(); - return heuristicsProcessor.processHeuristics(documentResult); +function applyStyles(documentResult, options) { + var styleApplicator = new StyleApplicator(options); + return styleApplicator.processDocumentStyles(documentResult); } function convertDocumentToHtml(documentResult, options) { diff --git a/lib/style-applicator.js b/lib/style-applicator.js index 0e3c97d..d6d909d 100644 --- a/lib/style-applicator.js +++ b/lib/style-applicator.js @@ -1,26 +1,30 @@ -exports.HeuristicsProcessor = HeuristicsProcessor; +exports.StyleApplicator = StyleApplicator; -function HeuristicsProcessor() { - function processHeuristics(document) { +function StyleApplicator(options) { + styleApplicatorOptions = options.styleApplicator || {}; + + function processDocumentStyles(document) { for (var i = 0; i < document.value.children.length; i++) { var element = document.value.children[i]; switch (element.type) { case 'paragraph': - document.value.children[i] = processParagraph(document.value.children[i]); + document.value.children[i] = processParagraphStyles(document.value.children[i]); break; } } return document; } - function processParagraph(element) { - if (element.alignment == 'center' && !element.styleName) { - element.styleName = "CenteredHeading"; + function processParagraphStyles(element) { + if (!element.styleName) { + if (styleApplicatorOptions.alignment && element.alignment) { + element.styleName = element.alignment+"Aligned"; + } } return element; } return { - processHeuristics: processHeuristics + processDocumentStyles: processDocumentStyles } } \ No newline at end of file -- cgit v1.2.3 From 05e67a227cabe6e2b0ddefae13570465b5de7ce4 Mon Sep 17 00:00:00 2001 From: Craig Leinoff Date: Tue, 7 Jan 2014 14:10:38 -0500 Subject: Factoring out style-applicator.js to enduser. --- lib/index.js | 22 +++++----------------- lib/style-applicator.js | 30 ------------------------------ 2 files changed, 5 insertions(+), 47 deletions(-) delete mode 100644 lib/style-applicator.js (limited to 'lib') diff --git a/lib/index.js b/lib/index.js index bf12eb0..f32844f 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,5 +1,4 @@ var docxReader = require("./docx-reader"); -var StyleApplicator = require("./style-applicator").StyleApplicator; var DocumentConverter = require("./document-to-html").DocumentConverter; var htmlPaths = require("./html-paths"); var documentMatchers = require("./document-matchers"); @@ -27,10 +26,7 @@ var standardOptions = exports.standardOptions = { style("p:ordered-list(3) => ul|ol > li > ul|ol > li > ol > li:fresh"), style("p:ordered-list(4) => ul|ol > li > ul|ol > li > ul|ol > li > ol > li:fresh"), style("p:ordered-list(5) => ul|ol > li > ul|ol > li > ul|ol > li > ul|ol > li > ol > li:fresh") - ], - styleApplicator: { - alignment: true - } + ] }; function Converter(options) { @@ -41,14 +37,11 @@ Converter.prototype.convertToHtml = function(inputOptions) { var options = this._options; return read(inputOptions) .then(function(documentResult) { - var styleApplicatorEnabled = false; - for (var optionName in options.styleApplicator) { - if (options.styleApplicator[optionName] == true) { - styleApplicatorEnabled = true; - } + if (options.transformDocument) { + return options.transformDocument(documentResult); } - if (styleApplicatorEnabled == true) { - return applyStyles(documentResult, options); + else { + return documentResult; } }) .then(function(documentResult) { @@ -65,11 +58,6 @@ function read(inputOptions) { return docxReader.read(inputOptions); } -function applyStyles(documentResult, options) { - var styleApplicator = new StyleApplicator(options); - return styleApplicator.processDocumentStyles(documentResult); -} - function convertDocumentToHtml(documentResult, options) { var documentConverter = new DocumentConverter(options); return documentResult.flatMapThen(function(document) { diff --git a/lib/style-applicator.js b/lib/style-applicator.js deleted file mode 100644 index d6d909d..0000000 --- a/lib/style-applicator.js +++ /dev/null @@ -1,30 +0,0 @@ -exports.StyleApplicator = StyleApplicator; - -function StyleApplicator(options) { - styleApplicatorOptions = options.styleApplicator || {}; - - function processDocumentStyles(document) { - for (var i = 0; i < document.value.children.length; i++) { - var element = document.value.children[i]; - switch (element.type) { - case 'paragraph': - document.value.children[i] = processParagraphStyles(document.value.children[i]); - break; - } - } - return document; - } - - function processParagraphStyles(element) { - if (!element.styleName) { - if (styleApplicatorOptions.alignment && element.alignment) { - element.styleName = element.alignment+"Aligned"; - } - } - return element; - } - - return { - processDocumentStyles: processDocumentStyles - } -} \ No newline at end of file -- cgit v1.2.3