git.iko.soy/typeable/pandoc
on master help sign in
Docx Reader: Change state handling.

We don't need `updateDState` -- the built-in `modify` works just fine. And we redefine `withDState` to use modify.

commita4671afd64b4195be67f7dccd2ca947144608a7f authorJesse Rosenthal <jrosenthal@jhu.edu> dateWed 16 Jul 2014, 03:43 (12 years ago) parent643435f
1 file changed+12−16
src/Text/Pandoc/Readers/Docx.hs+12−16
src/Text/Pandoc/Readers/Docx.hs+12−16
@@ -112,18 +112,13 @@ data DEnv = DEnv { docxOptions :: ReaderOptions
112112
113113type DocxContext = ReaderT DEnv (State DState)
114114
115−updateDState :: (DState -> DState) -> DocxContext ()
116−updateDState f = do
117− st <- get
118− put $ f st
119−
120−withDState :: DState -> DocxContext a -> DocxContext a
121−withDState ds dctx = do
122− ds' <- get
123− updateDState (\_ -> ds)
124− dctx' <- dctx
125− put ds'
126− return dctx'
115+withDState :: (DState -> DState) -> DocxContext a -> DocxContext a
116+withDState f dctx = do
117+ ds <- get
118+ modify f
119+ ctx' <- dctx
120+ put ds
121+ return ctx'
127122
128123evalDocxContext :: DocxContext a -> DEnv -> DState -> a
129124evalDocxContext ctx env st = evalState (runReaderT ctx env) st
@@ -307,8 +302,9 @@ parPartToInlines (BookMark _ anchor) =
307302 let newAnchor = case anchor `elem` (M.elems anchorMap) of
308303 True -> uniqueIdent [Str anchor] (M.elems anchorMap)
309304 False -> anchor
310− updateDState $ \s -> s { docxAnchorMap = M.insert anchor newAnchor anchorMap}
311305 return [Span (anchor, ["anchor"], []) []]
306+ modify $ \s -> s { docxAnchorMap = M.insert anchor newAnchor anchorMap}
307+ return [Span (newAnchor, ["anchor"], []) []]
312308parPartToInlines (Drawing fp bs) = do
313309 return $ case True of -- TODO: add self-contained images
314310 True -> [Image [] (fp, "")]
@@ -427,8 +423,8 @@ oMathElemToTexString (Matrix bases) = do
427423 s <- liftM (intercalate " \\\\\n")(mapM rowString bases)
428424 return $ printf "\\begin{matrix}\n%s\n\\end{matrix}" s
429425oMathElemToTexString (NAry style sub sup base) | Just c <- nAryChar style = do
430− ds <- gets (\s -> s{docxInTexSubscript = True})
431− subString <- withDState ds $ concatMapM oMathElemToTexString sub
426+ subString <- withDState (\s -> s{docxInTexSubscript = True}) $
427+ concatMapM oMathElemToTexString sub
432428 supString <- concatMapM oMathElemToTexString sup
433429 baseString <- baseToTexString base
434430 return $ case M.lookup c uniconvMap of
@@ -505,7 +501,7 @@ makeHeaderAnchor (Header n (_, classes, kvs) ils) =
505501 do
506502 hdrIDMap <- gets docxAnchorMap
507503 let newIdent = uniqueIdent ils (M.elems hdrIDMap)
508− updateDState $ \s -> s {docxAnchorMap = M.insert newIdent newIdent hdrIDMap}
504+ modify $ \s -> s {docxAnchorMap = M.insert newIdent newIdent hdrIDMap}
509505 return $ Header n (newIdent, classes, kvs) ils
510506makeHeaderAnchor blk = return blk
511507