@@ -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
@@ -112,18 +113,13 @@ data DEnv = DEnv { docxOptions :: ReaderOptions
113type DocxContext = ReaderT DEnv (State DState)
114type DocxContext = ReaderT DEnv (State DState)
115−updateDState :: (DState -> DState) -> DocxContext ()
116+withDState :: (DState -> DState) -> DocxContext a -> DocxContext a
120−withDState :: DState -> DocxContext a -> DocxContext a
121−withDState ds dctx = do
117+withDState f dctx = do
122− ds' <- get
118+ ds <- get
123− updateDState (\_ -> ds)
119+ modify f
124− dctx' <- dctx
120+ ctx' <- dctx
126− return dctx'
122+ return ctx'
128evalDocxContext :: DocxContext a -> DEnv -> DState -> a
124evalDocxContext :: DocxContext a -> DEnv -> DState -> a
129evalDocxContext ctx env st = evalState (runReaderT ctx env) st
125evalDocxContext ctx env st = evalState (runReaderT ctx env) st
@@ -297,18 +293,24 @@ parPartToInlines (BookMark _ anchor) =
297 -- We record these, so we can make sure not to overwrite
293 -- We record these, so we can make sure not to overwrite
298 -- 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
300 -- Get the anchor map.
298 -- Get the anchor map.
301 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
302− -- 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
303− -- necessary. This will have the possible effect of rewriting
304+ -- there. Rewrite if necessary. This will have the possible effect
304− -- user-defined anchor links. However, since these are not defined
305+ -- of rewriting user-defined anchor links. However, since these
305− -- 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
306− -- pass.
307+ -- avoid an extra pass.
307− let newAnchor = case anchor `elem` (M.elems anchorMap) of
309+ if not inHdrBool && anchor `elem` (M.elems anchorMap)
308− True -> uniqueIdent [Str anchor] (M.elems anchorMap)
310+ then uniqueIdent [Str anchor] (M.elems anchorMap)
309− False -> anchor
311+ else anchor
310− updateDState $ \s -> s { docxAnchorMap = M.insert anchor newAnchor anchorMap}
312+ modify $ \s -> s { docxAnchorMap = M.insert anchor newAnchor anchorMap}
311− return [Span (anchor, ["anchor"], []) []]
313+ return [Span (newAnchor, ["anchor"], []) []]
312parPartToInlines (Drawing fp bs) = do
314parPartToInlines (Drawing fp bs) = do
313 return $ case True of -- TODO: add self-contained images
315 return $ case True of -- TODO: add self-contained images
314 True -> [Image [] (fp, "")]
316 True -> [Image [] (fp, "")]
@@ -427,8 +429,8 @@ oMathElemToTexString (Matrix bases) = do
427 s <- liftM (intercalate " \\\\\n")(mapM rowString bases)
429 s <- liftM (intercalate " \\\\\n")(mapM rowString bases)
428 return $ printf "\\begin{matrix}\n%s\n\\end{matrix}" s
430 return $ printf "\\begin{matrix}\n%s\n\\end{matrix}" s
429oMathElemToTexString (NAry style sub sup base) | Just c <- nAryChar style = do
431oMathElemToTexString (NAry style sub sup base) | Just c <- nAryChar style = do
430− ds <- gets (\s -> s{docxInTexSubscript = True})
432+ subString <- withDState (\s -> s{docxInTexSubscript = True}) $
431− subString <- withDState ds $ concatMapM oMathElemToTexString sub
433+ concatMapM oMathElemToTexString sub
432 supString <- concatMapM oMathElemToTexString sup
434 supString <- concatMapM oMathElemToTexString sup
433 baseString <- baseToTexString base
435 baseString <- baseToTexString base
434 return $ case M.lookup c uniconvMap of
436 return $ case M.lookup c uniconvMap of
@@ -497,7 +499,7 @@ makeHeaderAnchor (Header n (_, classes, kvs) ils)
498 hdrIDMap <- gets docxAnchorMap
500 hdrIDMap <- gets docxAnchorMap
499 let newIdent = uniqueIdent ils (M.elems hdrIDMap)
501 let newIdent = uniqueIdent ils (M.elems hdrIDMap)
500− updateDState $ \s -> s {docxAnchorMap = M.insert ident newIdent hdrIDMap}
502+ modify $ \s -> s {docxAnchorMap = M.insert ident newIdent hdrIDMap}
501 return $ Header n (newIdent, classes, kvs) (ils \\ (x:xs))
503 return $ Header n (newIdent, classes, kvs) (ils \\ (x:xs))
502-- 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
503-- it with itself.)
505-- it with itself.)
@@ -505,7 +507,7 @@ makeHeaderAnchor (Header n (_, classes, kvs) ils) =
506 hdrIDMap <- gets docxAnchorMap
508 hdrIDMap <- gets docxAnchorMap
507 let newIdent = uniqueIdent ils (M.elems hdrIDMap)
509 let newIdent = uniqueIdent ils (M.elems hdrIDMap)
508− updateDState $ \s -> s {docxAnchorMap = M.insert newIdent newIdent hdrIDMap}
510+ modify $ \s -> s {docxAnchorMap = M.insert newIdent newIdent hdrIDMap}
509 return $ Header n (newIdent, classes, kvs) ils
511 return $ Header n (newIdent, classes, kvs) ils
510makeHeaderAnchor blk = return blk
512makeHeaderAnchor blk = return blk
@@ -541,7 +543,8 @@ bodyPartToBlocks (Paragraph pPr parparts)
541 [CodeBlock ("", [], []) (concatMap parPartToString parparts)]
543 [CodeBlock ("", [], []) (concatMap parPartToString parparts)]
542bodyPartToBlocks (Paragraph pPr parparts)
544bodyPartToBlocks (Paragraph pPr parparts)
543 | any isHeaderContainer (parStyleToContainers pPr) = do
545 | any isHeaderContainer (parStyleToContainers pPr) = do
546+ ils <-withDState (\s -> s{docxInHeaderBlock = True}) $
544− ils <- parPartsToInlines parparts >>= (return . normalizeSpaces)
547+ parPartsToInlines parparts >>= (return . normalizeSpaces)
545 let (Container hdrFun) = head $ filter isHeaderContainer (parStyleToContainers pPr)
548 let (Container hdrFun) = head $ filter isHeaderContainer (parStyleToContainers pPr)
546 Header n attr _ = hdrFun []
549 Header n attr _ = hdrFun []
547 hdr <- makeHeaderAnchor $ Header n attr ils
550 hdr <- makeHeaderAnchor $ Header n attr ils
@@ -624,6 +627,7 @@ bodyToBlocks (Body bps) = do
624docxToBlocks :: ReaderOptions -> Docx -> [Block]
627docxToBlocks :: ReaderOptions -> Docx -> [Block]
625docxToBlocks opts d@(Docx (Document _ body)) =
628docxToBlocks opts d@(Docx (Document _ body)) =
626 let dState = DState { docxAnchorMap = M.empty
629 let dState = DState { docxAnchorMap = M.empty
630+ , docxInHeaderBlock = False
627 , docxInTexSubscript = False}
631 , docxInTexSubscript = False}
628 dEnv = DEnv { docxOptions = opts
632 dEnv = DEnv { docxOptions = opts
629 , docxDocument = d}
633 , docxDocument = d}