From 9ff40429323056e88bd31c4cf73e5d8420548f1c Mon Sep 17 00:00:00 2001
From: Jesse Rosenthal <jrosenthal@jhu.edu>
Date: Thu, 7 Feb 2019 05:37:50 -0500
Subject: Docx reader: Extend dynamic xml location to detecting relationships

Getting the location used to depend on a hard-coded .rels file based
on "word/document.xml". We now dynamically detect that file based on
the document.xml file specified in "_rels/.rels"
---
 src/Text/Pandoc/Readers/Docx/Parse.hs | 31 +++++++++++++++++++------------
 1 file changed, 19 insertions(+), 12 deletions(-)

(limited to 'src/Text/Pandoc/Readers/Docx')

diff --git a/src/Text/Pandoc/Readers/Docx/Parse.hs b/src/Text/Pandoc/Readers/Docx/Parse.hs
index fa4dc90a6..2c549bac0 100644
--- a/src/Text/Pandoc/Readers/Docx/Parse.hs
+++ b/src/Text/Pandoc/Readers/Docx/Parse.hs
@@ -486,12 +486,15 @@ archiveToComments zf =
       Just c  -> Comments cmts_namespaces c
       Nothing -> Comments cmts_namespaces M.empty
 
-filePathToRelType :: FilePath -> Maybe DocumentLocation
-filePathToRelType "word/_rels/document.xml.rels"  = Just InDocument
-filePathToRelType "word/_rels/document2.xml.rels" = Just InDocument
-filePathToRelType "word/_rels/footnotes.xml.rels" = Just InFootnote
-filePathToRelType "word/_rels/endnotes.xml.rels"  = Just InEndnote
-filePathToRelType _                               = Nothing
+filePathToRelType :: FilePath -> FilePath -> Maybe DocumentLocation
+filePathToRelType "word/_rels/footnotes.xml.rels" _ = Just InFootnote
+filePathToRelType "word/_rels/endnotes.xml.rels" _ = Just InEndnote
+-- -- to see if it's a documentPath, we have to check against the dynamic
+-- -- docPath specified in "_rels/.rels"
+filePathToRelType path docXmlPath =
+  if path == "word/_rels/" ++ (takeFileName docXmlPath) ++ ".rels"
+  then Just InDocument
+  else Nothing
 
 relElemToRelationship :: DocumentLocation -> Element -> Maybe Relationship
 relElemToRelationship relType element | qName (elName element) == "Relationship" =
@@ -501,16 +504,20 @@ relElemToRelationship relType element | qName (elName element) == "Relationship"
     return $ Relationship relType relId target
 relElemToRelationship _ _ = Nothing
 
-filePathToRelationships :: Archive -> FilePath -> [Relationship]
-filePathToRelationships ar fp | Just relType <- filePathToRelType fp
-                              , Just entry <- findEntryByPath fp ar
-                              , Just relElems <- (parseXMLDoc . UTF8.toStringLazy . fromEntry) entry =
+filePathToRelationships :: Archive -> (Maybe FilePath) -> FilePath ->  [Relationship]
+filePathToRelationships ar mDocXmlPath fp
+  | Just docXmlPath <- mDocXmlPath
+  , Just relType <- filePathToRelType fp docXmlPath
+  , Just entry <- findEntryByPath fp ar
+  , Just relElems <- (parseXMLDoc . UTF8.toStringLazy . fromEntry) entry =
   mapMaybe (relElemToRelationship relType) $ elChildren relElems
-filePathToRelationships _ _ = []
+filePathToRelationships _ _ _ = []
 
 archiveToRelationships :: Archive -> [Relationship]
 archiveToRelationships archive =
-  concatMap (filePathToRelationships archive) $ filesInArchive archive
+  let mDocXmlPath = getDocumentPath archive
+  in
+    concatMap (filePathToRelationships archive mDocXmlPath) $ filesInArchive archive
 
 filePathIsMedia :: FilePath -> Bool
 filePathIsMedia fp =
-- 
cgit v1.2.3