git.iko.soy/typeable/pandoc
on master help sign in
Docx reader: handle absolute URIs in Relationship Target.

Closes #7374.

commitcfa26e3ca0346397f41af9aed5b4cd1d86be1220 authorJohn MacFarlane <jgm@berkeley.edu> dateSat 12 Jun 2021, 13:56 (5 years ago) parentea53a1d
1 file changed+11−5
src/Text/Pandoc/Readers/Docx/Parse.hs+11−5
src/Text/Pandoc/Readers/Docx/Parse.hs+11−5
@@ -479,20 +479,26 @@ filePathToRelType path docXmlPath =
479479 then Just InDocument
480480 else Nothing
481481
482−relElemToRelationship :: DocumentLocation -> Element -> Maybe Relationship
483−relElemToRelationship relType element | qName (elName element) == "Relationship" =
482+relElemToRelationship :: FilePath -> DocumentLocation -> Element
483+ -> Maybe Relationship
484+relElemToRelationship fp relType element | qName (elName element) == "Relationship" =
484485 do
485486 relId <- findAttr (QName "Id" Nothing Nothing) element
486487 target <- findAttr (QName "Target" Nothing Nothing) element
487− return $ Relationship relType relId target
488−relElemToRelationship _ _ = Nothing
488+ -- target may be relative (media/image1.jpeg) or absolute
489+ -- (/word/media/image1.jpeg); we need to relativize it (see #7374)
490+ let frontOfFp = T.pack $ takeWhile (/= '_') fp
491+ let target' = fromMaybe target $
492+ T.stripPrefix frontOfFp $ T.dropWhile (== '/') target
493+ return $ Relationship relType relId target'
494+relElemToRelationship _ _ _ = Nothing
489495
490496filePathToRelationships :: Archive -> FilePath -> FilePath -> [Relationship]
491497filePathToRelationships ar docXmlPath fp
492498 | Just relType <- filePathToRelType fp docXmlPath
493499 , Just entry <- findEntryByPath fp ar
494500 , Just relElems <- parseXMLFromEntry entry =
495− mapMaybe (relElemToRelationship relType) $ elChildren relElems
501+ mapMaybe (relElemToRelationship fp relType) $ elChildren relElems
496502filePathToRelationships _ _ _ = []
497503
498504archiveToRelationships :: Archive -> FilePath -> [Relationship]