aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2013-03-29 20:42:38 +0000
committerMichael Williamson <mike@zwobble.org>2013-03-29 20:42:38 +0000
commite3891290bd3034d0ce8034387ffab1e5a9793a70 (patch)
tree3e9ba68c93ac030c34aaee66a5929815cf037c21
parent59421d9ef6d1521f19b83a8409934682592ccae6 (diff)
Add basic style mappings for paragraphs
-rw-r--r--lib/documents.js75
-rw-r--r--tests/documents.tests.js24
2 files changed, 66 insertions, 33 deletions
diff --git a/lib/documents.js b/lib/documents.js
index d51e604..5d2d0b7 100644
--- a/lib/documents.js
+++ b/lib/documents.js
@@ -1,3 +1,5 @@
+var util = require("util");
+
var q = require("q");
exports.DocumentConverter = DocumentConverter;
@@ -6,38 +8,52 @@ exports.Paragraph = Paragraph;
exports.Run = Run;
exports.Text = Text;
-function DocumentConverter() {
-}
-
-DocumentConverter.prototype.convertToHtml = function(document) {
- var html = document.children.map(elementToHtml).join("");
+function DocumentConverter(options) {
+ options = options || {};
+ options.paragraphStyleMap = options.paragraphStyleMap || {};
- return q.when({
- html: html
- });
-}
-
-function elementToHtml(element) {
- var handler = elementConverters[element.type];
- if (handler) {
- return handler(element);
- } else {
- return "";
+ var paragraphStyleMap = options.paragraphStyleMap;
+
+ function convertToHtml(document) {
+ var html = document.children.map(elementToHtml).join("");
+
+ return q.when({
+ html: html
+ });
}
-}
-var elementConverters = {
- "paragraph": function(element) {
+ function elementToHtml(element) {
+ var handler = elementConverters[element.type];
+ if (handler) {
+ return handler(element);
+ } else {
+ return "";
+ }
+ }
+
+ function convertParagraph(element) {
var innerHtml = element.children.map(elementToHtml).join("");
- return "<p>" + innerHtml + "</p>";
- },
- "run": function(element) {
- return element.children.map(elementToHtml).join("");
- },
- "text": function(element) {
- return element.value;
+ var tagName = styleForParagraph(element);
+ return util.format("<%s>%s</%s>", tagName, innerHtml, tagName);
}
-};
+
+ function styleForParagraph(element) {
+ return paragraphStyleMap[element.properties.styleName] || "p";
+ }
+
+ var elementConverters = {
+ "paragraph": convertParagraph,
+ "run": function(element) {
+ return element.children.map(elementToHtml).join("");
+ },
+ "text": function(element) {
+ return element.value;
+ }
+ };
+ return {
+ convertToHtml: convertToHtml
+ }
+}
function Document(children) {
return {
@@ -46,10 +62,11 @@ function Document(children) {
};
}
-function Paragraph(children) {
+function Paragraph(children, properties) {
return {
type: "paragraph",
- children: children
+ children: children,
+ properties: properties || {}
};
}
diff --git a/tests/documents.tests.js b/tests/documents.tests.js
index 76f237e..a5e81af 100644
--- a/tests/documents.tests.js
+++ b/tests/documents.tests.js
@@ -14,7 +14,7 @@ describe('DocumentConverter', function() {
return converter.convertToHtml(document).then(function(result) {
assert.equal("<p>Hello.</p>", result.html);
});
- })
+ });
test('should convert document containing multiple paragraphs to multiple p elements', function() {
var document = new documents.Document([
@@ -25,12 +25,28 @@ describe('DocumentConverter', function() {
return converter.convertToHtml(document).then(function(result) {
assert.equal("<p>Hello.</p><p>Goodbye.</p>", result.html);
});
- })
+ });
+
+ test('uses style mappings to pick HTML element for docx paragraph', function() {
+ var document = new documents.Document([
+ paragraphOfText("Hello.", "Heading1"),
+ ]);
+ var converter = new DocumentConverter({
+ paragraphStyleMap: {
+ "Heading1": "h1"
+ }
+ });
+ return converter.convertToHtml(document).then(function(result) {
+ assert.equal("<h1>Hello.</h1>", result.html);
+ });
+ });
});
-function paragraphOfText(text) {
+function paragraphOfText(text, styleName) {
var run = runOfText(text);
- return new documents.Paragraph([run]);
+ return new documents.Paragraph([run], {
+ styleName: styleName
+ });
}
function runOfText(text) {