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/index.js | 1 + 1 file changed, 1 insertion(+) (limited to 'lib/index.js') 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 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/index.js') 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 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/index.js') 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/index.js') 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