diff options
Diffstat (limited to 'lib')
| -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)); -} |
