diff options
| author | Michael Williamson <mike@zwobble.org> | 2017-02-19 21:18:04 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2017-02-19 21:18:04 +0000 |
| commit | 21617da1757fbcb86b9d66064f1483711597b09e (patch) | |
| tree | 4a0161a32c41bc161eff68b7d80062fe131861bb | |
| parent | da79f71bc6bcc39d006bbf9c8171dc32292641c6 (diff) | |
Move related complex field handling functions together
| -rw-r--r-- | lib/docx/body-reader.js | 79 |
1 files changed, 40 insertions, 39 deletions
diff --git a/lib/docx/body-reader.js b/lib/docx/body-reader.js index 5c2c7b3..ab6a6e7 100644 --- a/lib/docx/body-reader.js +++ b/lib/docx/body-reader.js @@ -99,6 +99,41 @@ function BodyReader(options) { return elementResultWithMessages({styleId: styleId, name: name}, messages); } + function readFldChar(element) { + var type = element.attributes["w:fldCharType"]; + if (type === "begin") { + currentInstrText = []; + } else if (type === "end") { + complexFieldStack.pop(); + } else if (type === "separate") { + var href = parseHyperlinkFieldCode(currentInstrText.join('')); + var complexField = href === null ? {type: "unknown"} : {type: "hyperlink", href: href}; + complexFieldStack.push(complexField); + } + return emptyResult(); + } + + function currentHyperlinkHref() { + var topHyperlink = _.last(complexFieldStack.filter(function(complexField) { + return complexField.type === "hyperlink"; + })); + return topHyperlink ? topHyperlink.href : null; + } + + function parseHyperlinkFieldCode(code) { + var result = /\s*HYPERLINK "(.*)"/.exec(code); + if (result) { + return result[1]; + } else { + return null; + } + } + + function readInstrText(element) { + currentInstrText.push(element.text()); + return emptyResult(); + } + function noteReferenceReader(noteType) { return function(element) { var noteId = element.attributes["w:id"]; @@ -148,32 +183,17 @@ function BodyReader(options) { var properties = _.find(children, isRunProperties); children = children.filter(negate(isRunProperties)); - var scopedHyperlink = scopedComplexFieldOfType(complexFieldStack, "hyperlink"); - if (scopedHyperlink !== undefined) { - children = [new documents.Hyperlink(children, {href: scopedHyperlink.href})]; + var hyperlinkHref = currentHyperlinkHref(); + if (hyperlinkHref !== null) { + children = [new documents.Hyperlink(children, {href: hyperlinkHref})]; } return new documents.Run(children, properties); }); }, "w:rPr": readRunProperties, - "w:fldChar": function(element) { - var type = element.attributes["w:fldCharType"]; - if (type === "begin") { - currentInstrText = []; - } else if (type === "end") { - complexFieldStack.pop(); - } else if (type === "separate") { - var href = parseHyperlinkFieldCode(currentInstrText.join('')); - var complexField = href === null ? {type: "unknown"} : {type: "hyperlink", href: href}; - complexFieldStack.push(complexField); - } - return emptyResult(); - }, - "w:instrText": function(element) { - currentInstrText.push(element.text()); - return emptyResult(); - }, + "w:fldChar": readFldChar, + "w:instrText": readInstrText, "w:t": function(element) { return elementResult(new documents.Text(element.text())); }, @@ -526,22 +546,3 @@ function joinElements(first, second) { function identity(value) { return value; } - -function parseHyperlinkFieldCode(code) { - var result = /\s*HYPERLINK "(.*)"/.exec(code); - if (result) { - return result[1]; - } else { - return null; - } -} - -function complexFieldsOfType(complexFields, type) { - return complexFields.filter(function(complexField) { - return complexField.type === type; - }); -} - -function scopedComplexFieldOfType(complexFields, type) { - return _.last(complexFieldsOfType(complexFields, type)); -} |
