summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2013-05-03 21:30:16 +0100
committerMichael Williamson <mike@zwobble.org>2013-05-03 21:30:16 +0100
commit29152fa7def7c9c46a0aa3c74e67d1a03878a9f5 (patch)
treea2483e6b3847ee02b687d5cfc060ffe77905383f
parentce01e3ec266ca3cbd96832766174e49337db98f8 (diff)
Remove runStyleMap in favour of general styleMap
-rw-r--r--lib/document-to-html.js29
-rw-r--r--tests/document-to-html.tests.js9
2 files changed, 24 insertions, 14 deletions
diff --git a/lib/document-to-html.js b/lib/document-to-html.js
index 896053f..930e869 100644
--- a/lib/document-to-html.js
+++ b/lib/document-to-html.js
@@ -13,7 +13,6 @@ function DocumentConverter(options) {
var defaultParagraphStyle = options.defaultParagraphStyle || htmlPaths.topLevelElement("p");
var styleMap = options.styleMap || [];
- var runStyleMap = options.runStyleMap || {};
function convertToHtml(document) {
var messages = [];
@@ -47,24 +46,24 @@ function DocumentConverter(options) {
}
function styleForParagraph(element, messages) {
- for (var i = 0; i < styleMap.length; i++) {
- if (styleMap[i].from(element)) {
- return styleMap[i].to;
- }
- }
+ var style = findStyle(element);
- if (element.styleName) {
- messages.push(results.warning("Unrecognised paragraph style: " + element.styleName));
+ if (style) {
+ return style.to;
+ } else {
+ if (element.styleName) {
+ messages.push(results.warning("Unrecognised paragraph style: " + element.styleName));
+ }
+ return defaultParagraphStyle;
}
- return defaultParagraphStyle;
}
function convertRun(run, html, messages, callback) {
var runHtml = new HtmlGenerator();
if (run.styleName) {
- var style = runStyleMap[run.styleName];
+ var style = findStyle(run);
if (style) {
- runHtml.satisfyPath(style);
+ runHtml.satisfyPath(style.to);
} else {
messages.push(results.warning("Unrecognised run style: " + run.styleName));
}
@@ -86,6 +85,14 @@ function DocumentConverter(options) {
});
}
+ function findStyle(element) {
+ for (var i = 0; i < styleMap.length; i++) {
+ if (styleMap[i].from(element)) {
+ return styleMap[i];
+ }
+ }
+ }
+
function defaultConvertImage(element, html, messages, callback) {
element.read("base64").then(function(imageBuffer) {
var src = "data:" + element.contentType + ";base64," + imageBuffer;
diff --git a/tests/document-to-html.tests.js b/tests/document-to-html.tests.js
index 7d49529..447e0b5 100644
--- a/tests/document-to-html.tests.js
+++ b/tests/document-to-html.tests.js
@@ -153,9 +153,12 @@ describe('DocumentConverter', function() {
test('run styles are converted to HTML if mapping exists', function() {
var run = runOfText("Hello.", {styleName: "Emphasis"});
var converter = new DocumentConverter({
- runStyleMap: {
- "Emphasis": htmlPaths.elements(["strong"])
- }
+ styleMap: [
+ {
+ from: documentMatchers.run("Emphasis"),
+ to: htmlPaths.elements(["strong"])
+ }
+ ]
});
return converter.convertToHtml(run).then(function(result) {
assert.equal(result.value, "<strong>Hello.</strong>");