git.iko.soy/typeable/pandoc
on master help sign in
Class: Functions for dealing with PureState

There are two states in PandocPure, but it is only easy to deal with CommonState. In the past, to do state monad operations on PureState (the state specific to PandocPure) you had to add (lift . lift) to the monadic operation and then rewrap in the newtype. This adds four functions ({get,gets,put,modify}PureState) corresponding to normal state monad operations. This allows the user to modify PureState in PandocPure without worrying about where it sits in the monad stack or rewrapping the newtype.

commit57cff4b8ae75a2bbca86f5e3123cb890b629944e authorJesse Rosenthal <jrosenthal@jhu.edu> dateSat 3 Dec 2016, 23:39 (10 years ago) parent15708f0
1 file changed+42−23
src/Text/Pandoc/Class.hs+42−23
src/Text/Pandoc/Class.hs+42−23
@@ -34,6 +34,10 @@ Typeclass for pandoc readers and writers, allowing both IO and pure instances.
3434module Text.Pandoc.Class ( PandocMonad(..)
3535 , CommonState(..)
3636 , PureState(..)
37+ , getPureState
38+ , getsPureState
39+ , putPureState
40+ , modifyPureState
3741 , getPOSIXTime
3842 , getZonedTime
3943 , warning
@@ -266,6 +270,21 @@ instance Default PureState where
266270 , stCabalDataDir = mempty
267271 , stFontFiles = []
268272 }
273+
274+
275+getPureState :: PandocPure PureState
276+getPureState = PandocPure $ lift $ lift $ get
277+
278+getsPureState :: (PureState -> a) -> PandocPure a
279+getsPureState f = f <$> getPureState
280+
281+putPureState :: PureState -> PandocPure ()
282+putPureState ps= PandocPure $ lift $ lift $ put ps
283+
284+modifyPureState :: (PureState -> PureState) -> PandocPure ()
285+modifyPureState f = PandocPure $ lift $ lift $ modify f
286+
287+
269288data FileInfo = FileInfo { infoFileMTime :: UTCTime
270289 , infoFileContents :: B.ByteString
271290 }
@@ -294,33 +313,33 @@ runPure x = flip evalState def $
294313 unPandocPure x
295314
296315instance PandocMonad PandocPure where
297− lookupEnv s = PandocPure $ do
298− env <- lift $ lift $ gets stEnv
316+ lookupEnv s = do
317+ env <- getsPureState stEnv
299318 return (lookup s env)
300319
301− getCurrentTime = PandocPure $ lift $ lift $ gets stTime
320+ getCurrentTime = getsPureState stTime
302321
303− getCurrentTimeZone = PandocPure $ lift $ lift $ gets stTimeZone
322+ getCurrentTimeZone = getsPureState stTimeZone
304323
305− getDefaultReferenceDocx _ = PandocPure $ lift $ lift $ gets stReferenceDocx
324+ getDefaultReferenceDocx _ = getsPureState stReferenceDocx
306325
307− getDefaultReferenceODT _ = PandocPure $ lift $ lift $ gets stReferenceODT
326+ getDefaultReferenceODT _ = getsPureState stReferenceODT
308327
309− newStdGen = PandocPure $ do
310− g <- lift $ lift $ gets stStdGen
328+ newStdGen = do
329+ g <- getsPureState stStdGen
311330 let (_, nxtGen) = next g
312− lift $ lift $ modify $ \st -> st { stStdGen = nxtGen }
331+ modifyPureState $ \st -> st { stStdGen = nxtGen }
313332 return g
314333
315− newUniqueHash = PandocPure $ do
316− uniqs <- lift $ lift $ gets stUniqStore
334+ newUniqueHash = do
335+ uniqs <- getsPureState stUniqStore
317336 case uniqs of
318337 u : us -> do
319− lift $ lift $ modify $ \st -> st { stUniqStore = us }
338+ modifyPureState $ \st -> st { stUniqStore = us }
320339 return u
321340 _ -> M.fail "uniq store ran out of elements"
322− readFileLazy fp = PandocPure $ do
323− fps <- lift $ lift $ gets stFiles
341+ readFileLazy fp = do
342+ fps <- getsPureState stFiles
324343 case infoFileContents <$> getFileInfo fp fps of
325344 Just bs -> return (BL.fromStrict bs)
326345 Nothing -> throwError $ PandocFileReadError fp
@@ -331,14 +350,14 @@ instance PandocMonad PandocPure where
331350 readDataFile Nothing fname = do
332351 let fname' = if fname == "MANUAL.txt" then fname else "data" </> fname
333352 BL.toStrict <$> (readFileLazy fname')
334− readDataFile (Just userDir) fname = PandocPure $ do
335− userDirFiles <- lift $ lift $ gets stUserDataDir
353+ readDataFile (Just userDir) fname = do
354+ userDirFiles <- getsPureState stUserDataDir
336355 case infoFileContents <$> (getFileInfo (userDir </> fname) userDirFiles) of
337356 Just bs -> return bs
338− Nothing -> unPandocPure $ readDataFile Nothing fname
357+ Nothing -> readDataFile Nothing fname
339358 fail = M.fail
340− fetchItem _ fp = PandocPure $ do
341− fps <- lift $ lift $ gets stFiles
359+ fetchItem _ fp = do
360+ fps <- getsPureState stFiles
342361 case infoFileContents <$> (getFileInfo fp fps) of
343362 Just bs -> return (Right (bs, getMimeType fp))
344363 Nothing -> return (Left $ E.toException $ PandocFileReadError fp)
@@ -348,12 +367,12 @@ instance PandocMonad PandocPure where
348367 Nothing -> fetchItem sourceUrl nm
349368 Just (mime, bs) -> return (Right (B.concat $ BL.toChunks bs, Just mime))
350369
351− glob s = PandocPure $ do
352− fontFiles <- lift $ lift $ gets stFontFiles
370+ glob s = do
371+ fontFiles <- getsPureState stFontFiles
353372 return (filter (match (compile s)) fontFiles)
354373
355− getModificationTime fp = PandocPure $ do
356− fps <- lift $ lift $ gets stFiles
374+ getModificationTime fp = do
375+ fps <- getsPureState stFiles
357376 case infoFileMTime <$> (getFileInfo fp fps) of
358377 Just tm -> return tm
359378 Nothing -> throwError $ PandocFileReadError fp