diff options
| author | Michael Williamson <mike@zwobble.org> | 2017-07-21 12:05:29 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-07-21 12:05:29 +0100 |
| commit | 2107916e9f5ac9288f69f09048cb992ec7ab92b4 (patch) | |
| tree | 40b6f218fbbac7782effb8b0ab1255b3e773a315 | |
| parent | 534f62e0684af52766adf82b8fd5dbf796e018db (diff) | |
| parent | 33f089bb9f1d8fa599fe10ba1ddead6c10dbd4b4 (diff) | |
Merge pull request #124 from vgmoose/master
Add style mapping for small caps
| -rw-r--r-- | lib/document-to-html.js | 3 | ||||
| -rw-r--r-- | lib/documents.js | 1 | ||||
| -rw-r--r-- | lib/docx/body-reader.js | 3 | ||||
| -rw-r--r-- | lib/style-reader.js | 4 | ||||
| -rw-r--r-- | lib/styles/document-matchers.js | 1 | ||||
| -rw-r--r-- | test/document-to-html.tests.js | 24 | ||||
| -rw-r--r-- | test/docx/body-reader.tests.js | 14 | ||||
| -rw-r--r-- | test/style-reader.tests.js | 8 |
8 files changed, 56 insertions, 2 deletions
diff --git a/lib/document-to-html.js b/lib/document-to-html.js index d0197cb..0dac05e 100644 --- a/lib/document-to-html.js +++ b/lib/document-to-html.js @@ -127,6 +127,9 @@ function DocumentConversion(options, comments) { return convertElements(run.children, messages, options); }; var paths = []; + if (run.isSmallCaps) { + paths.push(findHtmlPathForRunProperty("smallCaps")); + } if (run.isStrikethrough) { paths.push(findHtmlPathForRunProperty("strikethrough", "s")); } diff --git a/lib/documents.js b/lib/documents.js index 605b1b4..a6d5dd0 100644 --- a/lib/documents.js +++ b/lib/documents.js @@ -52,6 +52,7 @@ function Run(children, properties) { isUnderline: properties.isUnderline, isItalic: properties.isItalic, isStrikethrough: properties.isStrikethrough, + isSmallCaps: properties.isSmallCaps, verticalAlignment: properties.verticalAlignment || verticalAlignment.baseline, font: properties.font || null }; diff --git a/lib/docx/body-reader.js b/lib/docx/body-reader.js index 6f6ebe2..c612e5c 100644 --- a/lib/docx/body-reader.js +++ b/lib/docx/body-reader.js @@ -58,7 +58,8 @@ function BodyReader(options) { isBold: readBooleanElement(element.first("w:b")), isUnderline: readBooleanElement(element.first("w:u")), isItalic: readBooleanElement(element.first("w:i")), - isStrikethrough: readBooleanElement(element.first("w:strike")) + isStrikethrough: readBooleanElement(element.first("w:strike")), + isSmallCaps: readBooleanElement(element.first("w:smallCaps")) }; }); } diff --git a/lib/style-reader.js b/lib/style-reader.js index 3a71ef6..692672e 100644 --- a/lib/style-reader.js +++ b/lib/style-reader.js @@ -137,6 +137,7 @@ function documentMatcherRule() { var italic = identifierToConstant("i", documentMatchers.italic); var underline = identifierToConstant("u", documentMatchers.underline); var strikethrough = identifierToConstant("strike", documentMatchers.strikethrough); + var smallCaps = identifierToConstant("small-caps", documentMatchers.smallCaps); var commentReference = identifierToConstant("comment-reference", documentMatchers.commentReference); var breakMatcher = sequence( @@ -159,13 +160,14 @@ function documentMatcherRule() { // TODO: handle unknown document matchers } }); - + return lop.rules.firstOf("element type", paragraphOrRun, bold, italic, underline, strikethrough, + smallCaps, commentReference, breakMatcher ); diff --git a/lib/styles/document-matchers.js b/lib/styles/document-matchers.js index afc0a7d..e3cae46 100644 --- a/lib/styles/document-matchers.js +++ b/lib/styles/document-matchers.js @@ -4,6 +4,7 @@ exports.bold = new Matcher("bold"); exports.italic = new Matcher("italic"); exports.underline = new Matcher("underline"); exports.strikethrough = new Matcher("strikethrough"); +exports.smallCaps = new Matcher("smallCaps"); exports.commentReference = new Matcher("commentReference"); exports.lineBreak = new Matcher("break", {breakType: "line"}); exports.pageBreak = new Matcher("break", {breakType: "page"}); diff --git a/test/document-to-html.tests.js b/test/document-to-html.tests.js index bf6631f..861033b 100644 --- a/test/document-to-html.tests.js +++ b/test/document-to-html.tests.js @@ -296,6 +296,30 @@ test('subscript runs are wrapped in <sub> tags', function() { }); }); +test('small caps runs are ignored by default', function() { + var run = runOfText("Hello.", {isSmallCaps: true}); + var converter = new DocumentConverter(); + return converter.convertToHtml(run).then(function(result) { + assert.equal(result.value, "Hello."); + }); +}); + +test('small caps runs can be configured with style mapping', function() { + var run = runOfText("Hello.", {isSmallCaps: true}); + var converter = new DocumentConverter({ + styleMap: [ + { + from: documentMatchers.smallCaps, + to: htmlPaths.elements([htmlPaths.element("span")]) + } + ] + }); + return converter.convertToHtml(run).then(function(result) { + assert.equal(result.value, "<span>Hello.</span>"); + }); +}); + + test('run styles are converted to HTML if mapping exists', function() { var run = runOfText("Hello.", {styleId: "Heading1Char", styleName: "Heading 1 Char"}); var converter = new DocumentConverter({ diff --git a/test/docx/body-reader.tests.js b/test/docx/body-reader.tests.js index 5257530..f84f0e8 100644 --- a/test/docx/body-reader.tests.js +++ b/test/docx/body-reader.tests.js @@ -431,11 +431,25 @@ test("isItalic is true if bold element is present", function() { assert.equal(run.isItalic, true); }); +test("isSmallCaps is false if smallcaps element is not present", function() { + var runXml = runWithProperties([]); + var run = readXmlElementValue(runXml); + assert.deepEqual(run.isSmallCaps, false); +}); + +test("isSmallCaps is true if smallcaps element is present", function() { + var smallCapsXml = new XmlElement("w:smallCaps"); + var runXml = runWithProperties([smallCapsXml]); + var run = readXmlElementValue(runXml); + assert.equal(run.isSmallCaps, true); +}); + var booleanRunProperties = [ {name: "isBold", tagName: "w:b"}, {name: "isUnderline", tagName: "w:u"}, {name: "isItalic", tagName: "w:i"}, {name: "isStrikethrough", tagName: "w:strike"}, + {name: "isSmallCaps", tagName: "w:smallCaps"}, ]; booleanRunProperties.forEach(function(runProperty) { diff --git a/test/style-reader.tests.js b/test/style-reader.tests.js index 96c0c39..20d4127 100644 --- a/test/style-reader.tests.js +++ b/test/style-reader.tests.js @@ -159,6 +159,13 @@ test("styleReader.readDocumentMatcher", { ); }, + "reads smallcaps": function() { + assertDocumentMatcher( + "small-caps", + documentMatchers.smallCaps + ); + }, + "reads comment-reference": function() { assertDocumentMatcher( "comment-reference", @@ -186,6 +193,7 @@ test("styleReader.readDocumentMatcher", { documentMatchers.columnBreak ); } + }); function assertDocumentMatcher(input, expected) { |
