From 62882f976d2fb37c317dac7c35c6ce82b784b95b Mon Sep 17 00:00:00 2001
From: Jesse Rosenthal <jrosenthal@jhu.edu>
Date: Sun, 28 Aug 2016 18:03:09 -0400
Subject: Docx reader: Handle anchor spans with content in headers.

Previously, we would only be able to figure out internal links to a
header in a docx if the anchor span was empty. We change that to read
the inlines out of the first anchor span in a header.

This still leaves another problem: what to do if there are multiple
anchor spans in a header. That will be addressed in a future commit.
---
 src/Text/Pandoc/Readers/Docx.hs | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

(limited to 'src')

diff --git a/src/Text/Pandoc/Readers/Docx.hs b/src/Text/Pandoc/Readers/Docx.hs
index 2bc17c069..f74b2b6d6 100644
--- a/src/Text/Pandoc/Readers/Docx.hs
+++ b/src/Text/Pandoc/Readers/Docx.hs
@@ -83,7 +83,7 @@ import Text.Pandoc.Readers.Docx.Lists
 import Text.Pandoc.Readers.Docx.Combine
 import Text.Pandoc.Shared
 import Text.Pandoc.MediaBag (insertMedia, MediaBag)
-import Data.List (delete, (\\), intersect)
+import Data.List (delete, intersect)
 import Text.TeXMath (writeTeX)
 import Data.Default (Default)
 import qualified Data.ByteString.Lazy as B
@@ -412,10 +412,9 @@ parPartToInlines (PlainOMath exps) = do
   return $ math $ writeTeX exps
 
 isAnchorSpan :: Inline -> Bool
-isAnchorSpan (Span (_, classes, kvs) ils) =
+isAnchorSpan (Span (_, classes, kvs) _) =
   classes == ["anchor"] &&
-  null kvs &&
-  null ils
+  null kvs
 isAnchorSpan _ = False
 
 dummyAnchors :: [String]
@@ -433,12 +432,14 @@ 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)
-  | (c:cs) <- filter isAnchorSpan ils
-  , (Span (ident, ["anchor"], _) _) <- c = do
+  | (c:_) <- filter isAnchorSpan ils
+  , (Span (ident, ["anchor"], _) cIls) <- c = do
     hdrIDMap <- gets docxAnchorMap
     let newIdent = uniqueIdent ils (Set.fromList $ M.elems hdrIDMap)
+        newIls = concatMap f ils where f il | il == c   = cIls
+                                            | otherwise = [il]
     modify $ \s -> s {docxAnchorMap = M.insert ident newIdent hdrIDMap}
-    return $ Header n (newIdent, classes, kvs) (ils \\ (c:cs))
+    return $ Header n (newIdent, classes, kvs) newIls
 -- Otherwise we just give it a name, and register that name (associate
 -- it with itself.)
 makeHeaderAnchor' (Header n (_, classes, kvs) ils) =
-- 
cgit v1.2.3