@@ -105,6 +105,7 @@ readDocx opts bytes =
105 Left _ -> error $ "couldn't parse docx file"
105 Left _ -> error $ "couldn't parse docx file"
107data DState = DState { docxAnchorMap :: M.Map String String
107data DState = DState { docxAnchorMap :: M.Map String String
108+ , docxInHeaderBlock :: Bool
108 , docxInTexSubscript :: Bool }
109 , docxInTexSubscript :: Bool }
110data DEnv = DEnv { docxOptions :: ReaderOptions
111data DEnv = DEnv { docxOptions :: ReaderOptions
@@ -292,17 +293,22 @@ parPartToInlines (BookMark _ anchor) =
292 -- We record these, so we can make sure not to overwrite
293 -- We record these, so we can make sure not to overwrite
293 -- user-defined anchor links with header auto ids.
294 -- user-defined anchor links with header auto ids.
296+ -- get whether we're in a header.
297+ inHdrBool <- gets docxInHeaderBlock
295 -- Get the anchor map.
298 -- Get the anchor map.
296 anchorMap <- gets docxAnchorMap
299 anchorMap <- gets docxAnchorMap
300+ -- We don't want to rewrite if we're in a header, since we'll take
301+ -- care of that later, when we make the header anchor. If the
302+ -- bookmark were already in uniqueIdent form, this would lead to a
297− -- Check to see if the id is already in there. Rewrite if
303+ -- duplication. Otherwise, we check to see if the id is already in
298− -- necessary. This will have the possible effect of rewriting
304+ -- there. Rewrite if necessary. This will have the possible effect
299− -- user-defined anchor links. However, since these are not defined
305+ -- of rewriting user-defined anchor links. However, since these
300− -- in pandoc, it seems like a necessary evil to avoid an extra
306+ -- are not defined in pandoc, it seems like a necessary evil to
301− -- pass.
307+ -- avoid an extra pass.
302− let newAnchor = case anchor `elem` (M.elems anchorMap) of
309+ if not inHdrBool && anchor `elem` (M.elems anchorMap)
303− True -> uniqueIdent [Str anchor] (M.elems anchorMap)
310+ then uniqueIdent [Str anchor] (M.elems anchorMap)
304− False -> anchor
311+ else anchor
305− return [Span (anchor, ["anchor"], []) []]
306 modify $ \s -> s { docxAnchorMap = M.insert anchor newAnchor anchorMap}
312 modify $ \s -> s { docxAnchorMap = M.insert anchor newAnchor anchorMap}
307 return [Span (newAnchor, ["anchor"], []) []]
313 return [Span (newAnchor, ["anchor"], []) []]
308parPartToInlines (Drawing fp bs) = do
314parPartToInlines (Drawing fp bs) = do
@@ -493,7 +499,7 @@ makeHeaderAnchor (Header n (_, classes, kvs) ils)
494 hdrIDMap <- gets docxAnchorMap
500 hdrIDMap <- gets docxAnchorMap
495 let newIdent = uniqueIdent ils (M.elems hdrIDMap)
501 let newIdent = uniqueIdent ils (M.elems hdrIDMap)
496− updateDState $ \s -> s {docxAnchorMap = M.insert ident newIdent hdrIDMap}
502+ modify $ \s -> s {docxAnchorMap = M.insert ident newIdent hdrIDMap}
497 return $ Header n (newIdent, classes, kvs) (ils \\ (x:xs))
503 return $ Header n (newIdent, classes, kvs) (ils \\ (x:xs))
498-- Otherwise we just give it a name, and register that name (associate
504-- Otherwise we just give it a name, and register that name (associate
499-- it with itself.)
505-- it with itself.)
@@ -537,7 +543,8 @@ bodyPartToBlocks (Paragraph pPr parparts)
537 [CodeBlock ("", [], []) (concatMap parPartToString parparts)]
543 [CodeBlock ("", [], []) (concatMap parPartToString parparts)]
538bodyPartToBlocks (Paragraph pPr parparts)
544bodyPartToBlocks (Paragraph pPr parparts)
539 | any isHeaderContainer (parStyleToContainers pPr) = do
545 | any isHeaderContainer (parStyleToContainers pPr) = do
546+ ils <-withDState (\s -> s{docxInHeaderBlock = True}) $
540− ils <- parPartsToInlines parparts >>= (return . normalizeSpaces)
547+ parPartsToInlines parparts >>= (return . normalizeSpaces)
541 let (Container hdrFun) = head $ filter isHeaderContainer (parStyleToContainers pPr)
548 let (Container hdrFun) = head $ filter isHeaderContainer (parStyleToContainers pPr)
542 Header n attr _ = hdrFun []
549 Header n attr _ = hdrFun []
543 hdr <- makeHeaderAnchor $ Header n attr ils
550 hdr <- makeHeaderAnchor $ Header n attr ils
@@ -620,6 +627,7 @@ bodyToBlocks (Body bps) = do
620docxToBlocks :: ReaderOptions -> Docx -> [Block]
627docxToBlocks :: ReaderOptions -> Docx -> [Block]
621docxToBlocks opts d@(Docx (Document _ body)) =
628docxToBlocks opts d@(Docx (Document _ body)) =
622 let dState = DState { docxAnchorMap = M.empty
629 let dState = DState { docxAnchorMap = M.empty
630+ , docxInHeaderBlock = False
623 , docxInTexSubscript = False}
631 , docxInTexSubscript = False}
624 dEnv = DEnv { docxOptions = opts
632 dEnv = DEnv { docxOptions = opts
625 , docxDocument = d}
633 , docxDocument = d}