git.iko.soy/typeable/pandoc
on master help sign in
Docx reader: Dynamically determine document.xml path.

The desktop Word program places the main document file in "word/document.xml", but the online word places it in "word/document2.xml". This file path is actually stated in the root "_rels/.rels" file, in the "Relationship" element with an "http://../officedocument" type. Closes #5277

commit4cce0efa4824b4081a2d971c488c5026c314bbb5 authorJesse Rosenthal <jrosenthal@jhu.edu> dateWed 6 Feb 2019, 21:06 (8 years ago) parent5d3b8ed
1 file changed+12−3
src/Text/Pandoc/Readers/Docx/Parse.hs+12−3
src/Text/Pandoc/Readers/Docx/Parse.hs+12−3
@@ -359,12 +359,21 @@ archiveToDocxWithWarnings archive = do
359359 Right doc -> Right (Docx doc, stateWarnings st)
360360 Left e -> Left e
361361
362−
362+getDocumentPath :: Archive -> Maybe String
363+getDocumentPath zf = do
364+ entry <- findEntryByPath "_rels/.rels" zf
365+ relsElem <- (parseXMLDoc . UTF8.toStringLazy . fromEntry) entry
366+ let rels = filterChildrenName (\n -> qName n == "Relationship") relsElem
367+ rel <- listToMaybe $
368+ filter (\e -> findAttr (QName "Type" Nothing Nothing) e ==
369+ Just "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument")
370+ rels
371+ findAttr (QName "Target" Nothing Nothing) rel
363372
364373archiveToDocument :: Archive -> D Document
365374archiveToDocument zf = do
366− entry <- maybeToD $ findEntryByPath "word/document.xml" zf
367− `mplus` findEntryByPath "word/document2.xml" zf -- see #5277
375+ docPath <- maybeToD $ getDocumentPath zf
376+ entry <- maybeToD $ findEntryByPath docPath zf
368377 docElem <- maybeToD $ (parseXMLDoc . UTF8.toStringLazy . fromEntry) entry
369378 let namespaces = elemToNameSpaces docElem
370379 bodyElem <- maybeToD $ findChildByName namespaces "w" "body" docElem