git.iko.soy/typeable/pandoc
on master help sign in
Class: Add stateful IO warnings, and function to get warndings.

Right now, the io warnings both print to stderr and write to the state. That can be easily modified. We also add a getWarnings function which pulls warnings out of the state for instances of PandocMonad.

commitcc7191b3b17ce7c7010a021bf685753ed2019aa6 authorJesse Rosenthal <jrosenthal@jhu.edu> dateSun 27 Nov 2016, 13:17 (10 years ago) parent18e85f8
1 file changed+9−3
src/Text/Pandoc/Class.hs+9−3
src/Text/Pandoc/Class.hs+9−3
@@ -97,6 +97,7 @@ class (Functor m, Applicative m, Monad m, MonadError PandocExecutionError m) =>
9797 -> String
9898 -> m (Either E.SomeException (B.ByteString, Maybe MimeType))
9999 warn :: String -> m ()
100+ getWarnings :: m [String]
100101 fail :: String -> m b
101102 glob :: String -> m [FilePath]
102103
@@ -113,11 +114,11 @@ data PandocExecutionError = PandocFileReadError FilePath
113114 deriving (Show, Typeable)
114115
115116-- Nothing in this for now, but let's put it there anyway.
116−data PandocStateIO = PandocStateIO
117+data PandocStateIO = PandocStateIO { ioStWarnings :: [String] }
117118 deriving Show
118119
119120instance Default PandocStateIO where
120− def = PandocStateIO
121+ def = PandocStateIO { ioStWarnings = [] }
121122
122123runIO :: PandocIO a -> IO (Either PandocExecutionError a)
123124runIO ma = flip evalStateT def $ runExceptT $ unPandocIO ma
@@ -156,7 +157,10 @@ instance PandocMonad PandocIO where
156157 fail = M.fail
157158 fetchItem ms s = liftIO $ IO.fetchItem ms s
158159 fetchItem' mb ms s = liftIO $ IO.fetchItem' mb ms s
159− warn = liftIO . IO.warn
160+ warn msg = do
161+ modify $ \st -> st{ioStWarnings = msg : ioStWarnings st}
162+ liftIO $ IO.warn msg
163+ getWarnings = gets ioStWarnings
160164 glob = liftIO . IO.glob
161165
162166data TestState = TestState { stStdGen :: StdGen
@@ -266,6 +270,8 @@ instance PandocMonad PandocPure where
266270
267271 warn s = modify $ \st -> st { stWarnings = s : stWarnings st }
268272
273+ getWarnings = gets stWarnings
274+
269275 glob s = do
270276 fontFiles <- asks envFontFiles
271277 return (filter (match (compile s)) fontFiles)