diff options
| author | Michael Williamson <mike@zwobble.org> | 2016-02-04 08:54:04 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2016-02-04 13:34:16 +0000 |
| commit | 9d3874dad6af6234274399ba6cfbad00c3d0a5aa (patch) | |
| tree | 2ce38b2824665c867b7d58b61df7f9b6b3c1ae01 /lib | |
| parent | 8408269bfec3cd2f92e02075cb05ef1a4772402d (diff) | |
Extract common code for reading styles
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/docx/body-reader.js | 54 |
1 files changed, 28 insertions, 26 deletions
diff --git a/lib/docx/body-reader.js b/lib/docx/body-reader.js index 3837d4a..4e7ff03 100644 --- a/lib/docx/body-reader.js +++ b/lib/docx/body-reader.js @@ -37,9 +37,6 @@ function BodyReader(options) { var properties = { type: "runProperties" }; - var messages = []; - - readRunStyle(properties, element, messages); var verticalAlignmentElement = element.first("w:vertAlign"); if (verticalAlignmentElement) { @@ -51,23 +48,38 @@ function BodyReader(options) { properties.isItalic = !!element.first("w:i"); properties.isStrikethrough = !!element.first("w:strike"); - return elementResultWithMessages(properties, messages); + return readRunStyle(element).map(function(style) { + properties.styleId = style.styleId; + properties.styleName = style.name; + return properties; + }); } - function readRunStyle(properties, element, messages) { - var styleElement = element.first("w:rStyle"); + function readParagraphStyle(element) { + return readStyle(element, "w:pStyle", "Paragraph", styles.findParagraphStyleById); + } + + function readRunStyle(element) { + return readStyle(element, "w:rStyle", "Run", styles.findCharacterStyleById); + } + + function readStyle(element, styleTagName, styleType, findStyleById) { + var messages = []; + var styleElement = element.first(styleTagName); + var styleId = null; + var name = null; if (styleElement) { - var styleId = styleElement.attributes["w:val"]; - properties.styleId = styleId; + styleId = styleElement.attributes["w:val"]; if (styleId) { - var style = styles.findCharacterStyleById(styleId); + var style = findStyleById(styleId); if (style) { - properties.styleName = style.name; + name = style.name; } else { - messages.push(undefinedStyleWarning("Run", styleId)); + messages.push(undefinedStyleWarning(styleType, styleId)); } } } + return elementResultWithMessages({styleId: styleId, name: name}, messages); } function noteReferenceReader(noteType) { @@ -100,28 +112,18 @@ function BodyReader(options) { var properties = { type: "paragraphProperties" }; - var messages = []; - var styleElement = element.first("w:pStyle"); - if (styleElement) { - var styleId = styleElement.attributes["w:val"]; - properties.styleId = styleId; - if (styleId) { - var style = styles.findParagraphStyleById(styleId); - if (style) { - properties.styleName = style.name; - } else { - messages.push(undefinedStyleWarning("Paragraph", styleId)); - } - } - } var alignElement = element.first("w:jc"); if (alignElement) { properties.alignment = alignElement.attributes["w:val"]; } properties.numbering = readNumberingProperties(element.firstOrEmpty("w:numPr")); - return elementResultWithMessages(properties, messages); + return readParagraphStyle(element).map(function(style) { + properties.styleId = style.styleId; + properties.styleName = style.name; + return properties; + }); }, "w:r": function(element) { return readXmlElements(element.children) |
