git.iko.soy/typeable/pandoc
on master help sign in
Add input and output filepaths to PandocMonad.
6a9a38cJesse Rosenthal
src/Text/Pandoc/Class.hs+48−12
@@ -110,9 +110,15 @@ class (Functor m, Applicative m, Monad m, MonadError PandocExecutionError m) =>
110 getWarnings :: m [String]
110 getWarnings :: m [String]
111 fail :: String -> m b
111 fail :: String -> m b
112 glob :: String -> m [FilePath]
112 glob :: String -> m [FilePath]
113+ getModificationTime :: FilePath -> m UTCTime
114+ -- The following are common to all instantiations of the monad, up
115+ -- to the record names, so I'd like to work out a better way to deal
116+ -- with it.
113 setMediaBag :: MediaBag -> m ()
117 setMediaBag :: MediaBag -> m ()
114 insertMedia :: FilePath -> Maybe MimeType -> BL.ByteString -> m ()
118 insertMedia :: FilePath -> Maybe MimeType -> BL.ByteString -> m ()
115− getModificationTime :: FilePath -> m UTCTime
119+ getInputFiles :: m (Maybe [FilePath])
120+ getOutputFile :: m (Maybe FilePath)
121+
116
122
117
123
118--Some functions derived from Primitives:
124--Some functions derived from Primitives:
@@ -152,8 +158,16 @@ instance Default PandocStateIO where
152 , ioStMediaBag = mempty
158 , ioStMediaBag = mempty
153 }
159 }
154
160
161+data PandocEnvIO = PandocEnvIO { ioEnvInputFiles :: Maybe [FilePath]
162+ , ioEnvOutputFile :: Maybe FilePath
163+ }
164+instance Default PandocEnvIO where
165+ def = PandocEnvIO { ioEnvInputFiles = Nothing -- stdin
166+ , ioEnvOutputFile = Nothing -- stdout
167+ }
168+
155runIO :: PandocIO a -> IO (Either PandocExecutionError a)
169runIO :: PandocIO a -> IO (Either PandocExecutionError a)
156−runIO ma = flip evalStateT def $ runExceptT $ unPandocIO ma
170+runIO ma = flip evalStateT def $ flip runReaderT def $ runExceptT $ unPandocIO ma
157
171
158runIOorExplode :: PandocIO a -> IO a
172runIOorExplode :: PandocIO a -> IO a
159runIOorExplode ma = do
173runIOorExplode ma = do
@@ -166,11 +180,12 @@ runIOorExplode ma = do
166 Left (PandocSomeError s) -> error s
180 Left (PandocSomeError s) -> error s
167
181
168newtype PandocIO a = PandocIO {
182newtype PandocIO a = PandocIO {
169− unPandocIO :: ExceptT PandocExecutionError (StateT PandocStateIO IO) a
183+ unPandocIO :: ExceptT PandocExecutionError (ReaderT PandocEnvIO (StateT PandocStateIO IO)) a
170 } deriving ( MonadIO
184 } deriving ( MonadIO
171 , Functor
185 , Functor
172 , Applicative
186 , Applicative
173 , Monad
187 , Monad
188+ , MonadReader PandocEnvIO
174 , MonadState PandocStateIO
189 , MonadState PandocStateIO
175 , MonadError PandocExecutionError
190 , MonadError PandocExecutionError
176 )
191 )
@@ -202,15 +217,20 @@ instance PandocMonad PandocIO where
202 liftIO $ IO.warn msg
217 liftIO $ IO.warn msg
203 getWarnings = gets ioStWarnings
218 getWarnings = gets ioStWarnings
204 glob = liftIO . IO.glob
219 glob = liftIO . IO.glob
205− setMediaBag mb =
206− modify $ \st -> st{ioStMediaBag = mb}
207− insertMedia fp mime bs =
208− modify $ \st -> st{ioStMediaBag = MB.insertMedia fp mime bs (ioStMediaBag st) }
209 getModificationTime fp = do
220 getModificationTime fp = do
210 eitherMtime <- liftIO (tryIOError $ IO.getModificationTime fp)
221 eitherMtime <- liftIO (tryIOError $ IO.getModificationTime fp)
211 case eitherMtime of
222 case eitherMtime of
212 Right mtime -> return mtime
223 Right mtime -> return mtime
213 Left _ -> throwError $ PandocFileReadError fp
224 Left _ -> throwError $ PandocFileReadError fp
225+ -- Common functions
226+ setMediaBag mb =
227+ modify $ \st -> st{ioStMediaBag = mb}
228+ insertMedia fp mime bs =
229+ modify $ \st -> st{ioStMediaBag = MB.insertMedia fp mime bs (ioStMediaBag st) }
230+ getInputFiles = asks ioEnvInputFiles
231+ getOutputFile = asks ioEnvOutputFile
232+
233+
214
234
215data PureState = PureState { stStdGen :: StdGen
235data PureState = PureState { stStdGen :: StdGen
216 , stWord8Store :: [Word8] -- should be
236 , stWord8Store :: [Word8] -- should be
@@ -253,6 +273,8 @@ data PureEnv = PureEnv { envEnv :: [(String, String)]
253 , envUserDataDir :: FileTree
273 , envUserDataDir :: FileTree
254 , envCabalDataDir :: FileTree
274 , envCabalDataDir :: FileTree
255 , envFontFiles :: [FilePath]
275 , envFontFiles :: [FilePath]
276+ , envInputFiles :: Maybe [FilePath]
277+ , envOutputFile :: Maybe FilePath
256 }
278 }
257
279
258-- We have to figure this out a bit more. But let's put some empty
280-- We have to figure this out a bit more. But let's put some empty
@@ -267,6 +289,8 @@ instance Default PureEnv where
267 , envUserDataDir = mempty
289 , envUserDataDir = mempty
268 , envCabalDataDir = mempty
290 , envCabalDataDir = mempty
269 , envFontFiles = []
291 , envFontFiles = []
292+ , envInputFiles = Nothing
293+ , envOutputFile = Nothing
270 }
294 }
271
295
272instance E.Exception PandocExecutionError
296instance E.Exception PandocExecutionError
@@ -348,14 +372,26 @@ instance PandocMonad PandocPure where
348 fontFiles <- asks envFontFiles
372 fontFiles <- asks envFontFiles
349 return (filter (match (compile s)) fontFiles)
373 return (filter (match (compile s)) fontFiles)
350
374
375+ getModificationTime fp = do
376+ fps <- asks envFiles
377+ case infoFileMTime <$> (getFileInfo fp fps) of
378+ Just tm -> return tm
379+ Nothing -> throwError $ PandocFileReadError fp
380+
381+ -- Common files
382+
351 setMediaBag mb =
383 setMediaBag mb =
352 modify $ \st -> st{stMediaBag = mb}
384 modify $ \st -> st{stMediaBag = mb}
353
385
354 insertMedia fp mime bs =
386 insertMedia fp mime bs =
355 modify $ \st -> st{stMediaBag = MB.insertMedia fp mime bs (stMediaBag st) }
387 modify $ \st -> st{stMediaBag = MB.insertMedia fp mime bs (stMediaBag st) }
356
388
357− getModificationTime fp = do
389+ getInputFiles = asks envInputFiles
358− fps <- asks envFiles
390+
359− case infoFileMTime <$> (getFileInfo fp fps) of
391+ getOutputFile = asks envOutputFile
360− Just tm -> return tm
392+
361− Nothing -> throwError $ PandocFileReadError fp
393+
394+
395+
396+
397+