From 378a795eaae7176426080c4164a66b33d511f87f Mon Sep 17 00:00:00 2001
From: Jesse Rosenthal <jrosenthal@jhu.edu>
Date: Tue, 12 Aug 2014 23:34:45 -0400
Subject: [PATCH] Docx: More robust handling of multiple bookmarks in header.

---
 src/Text/Pandoc/Readers/Docx.hs | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/src/Text/Pandoc/Readers/Docx.hs b/src/Text/Pandoc/Readers/Docx.hs
index 346d54bbe..28f49251c 100644
--- a/src/Text/Pandoc/Readers/Docx.hs
+++ b/src/Text/Pandoc/Readers/Docx.hs
@@ -402,7 +402,7 @@ parPartToInlines (PlainOMath exps) = do
 
 
 isAnchorSpan :: Inline -> Bool
-isAnchorSpan (Span (ident, classes, kvs) ils) =
+isAnchorSpan (Span (_, classes, kvs) ils) =
   classes == ["anchor"] &&
   null kvs &&
   null ils
@@ -415,14 +415,16 @@ makeHeaderAnchor :: Block -> DocxContext Block
 -- If there is an anchor already there (an anchor span in the header,
 -- to be exact), we rename and associate the new id with the old one.
 makeHeaderAnchor (Header n (_, classes, kvs) ils)
-  | (x : xs) <- filter isAnchorSpan ils
-  , (Span (ident, _, _) _) <- x
-  , notElem ident dummyAnchors =
+  | xs <- filter isAnchorSpan ils
+  , idents <- filter (\i -> notElem i dummyAnchors) $
+              map (\(Span (ident, _, _) _) -> ident) xs
+  , not $ null idents =
     do
       hdrIDMap <- gets docxAnchorMap
       let newIdent = uniqueIdent ils (M.elems hdrIDMap)
-      modify $ \s -> s {docxAnchorMap = M.insert ident newIdent hdrIDMap}
-      return $ Header n (newIdent, classes, kvs) (ils \\ (x:xs))
+          newMap   = M.fromList $ map (\i -> (i, newIdent)) idents
+      modify $ \s -> s {docxAnchorMap = M.union newMap hdrIDMap}
+      return $ Header n (newIdent, classes, kvs) (ils \\ xs)
 -- Otherwise we just give it a name, and register that name (associate
 -- it with itself.)
 makeHeaderAnchor (Header n (_, classes, kvs) ils) =