@@ -115,9 +115,7 @@ class (Functor m, Applicative m, Monad m, MonadError PandocError m)
115 openURL :: String -> m (B.ByteString, Maybe MimeType)
115 openURL :: String -> m (B.ByteString, Maybe MimeType)
116 readFileLazy :: FilePath -> m BL.ByteString
116 readFileLazy :: FilePath -> m BL.ByteString
117 readFileStrict :: FilePath -> m B.ByteString
117 readFileStrict :: FilePath -> m B.ByteString
118− readDataFile :: Maybe FilePath
118+ readDataFile :: Maybe FilePath -> FilePath -> m B.ByteString
121 glob :: String -> m [FilePath]
119 glob :: String -> m [FilePath]
122 getModificationTime :: FilePath -> m UTCTime
120 getModificationTime :: FilePath -> m UTCTime
123 getCommonState :: m CommonState
121 getCommonState :: m CommonState
@@ -218,40 +216,25 @@ newtype PandocIO a = PandocIO {
218 , MonadError PandocError
216 , MonadError PandocError
219+liftIOError :: (String -> IO a) -> String -> PandocIO a
221+ res <- liftIO $ tryIOError $ f u
223+ Left e -> throwError $ PandocIOError u e
221instance PandocMonad PandocIO where
226instance PandocMonad PandocIO where
222 lookupEnv = liftIO . IO.lookupEnv
227 lookupEnv = liftIO . IO.lookupEnv
223 getCurrentTime = liftIO IO.getCurrentTime
228 getCurrentTime = liftIO IO.getCurrentTime
224 getCurrentTimeZone = liftIO IO.getCurrentTimeZone
229 getCurrentTimeZone = liftIO IO.getCurrentTimeZone
225 newStdGen = liftIO IO.newStdGen
230 newStdGen = liftIO IO.newStdGen
226 newUniqueHash = hashUnique <$> (liftIO IO.newUnique)
231 newUniqueHash = hashUnique <$> (liftIO IO.newUnique)
227− openURL u = do
232+ openURL u = liftIOError IO.openURL u
228− eitherRes <- liftIO $ (tryIOError $ IO.openURL u)
230− Right (Right res) -> return res
231− Right (Left _) -> throwError $ PandocFileReadError u
232− Left _ -> throwError $ PandocFileReadError u
233− readFileLazy s = do
233+ readFileLazy s = liftIOError BL.readFile s
234− eitherBS <- liftIO (tryIOError $ BL.readFile s)
236− Right bs -> return bs
237− Left _ -> throwError $ PandocFileReadError s
238− readFileStrict s = do
234+ readFileStrict s = liftIOError B.readFile s
239− eitherBS <- liftIO (tryIOError $ B.readFile s)
241− Right bs -> return bs
242− Left _ -> throwError $ PandocFileReadError s
243− -- TODO: Make this more sensitive to the different sorts of failure
244− readDataFile mfp fname = do
235+ readDataFile mfp fname = liftIOError (IO.readDataFile mfp) fname
245− eitherBS <- liftIO (tryIOError $ IO.readDataFile mfp fname)
247− Right bs -> return bs
248− Left _ -> throwError $ PandocFileReadError fname
249 glob = liftIO . IO.glob
236 glob = liftIO . IO.glob
250− getModificationTime fp = do
237+ getModificationTime fp = liftIOError IO.getModificationTime fp
251− eitherMtime <- liftIO (tryIOError $ IO.getModificationTime fp)
253− Right mtime -> return mtime
254− Left _ -> throwError $ PandocFileReadError fp
255 getCommonState = PandocIO $ lift get
238 getCommonState = PandocIO $ lift get
256 putCommonState x = PandocIO $ lift $ put x
239 putCommonState x = PandocIO $ lift $ put x
257 logOutput msg =
240 logOutput msg =
@@ -304,8 +287,8 @@ downloadOrRead sourceURL s = do
304 readLocalFile $ dropWhile (=='/') (uriPath u')
287 readLocalFile $ dropWhile (=='/') (uriPath u')
305 _ -> readLocalFile fp -- get from local file system
288 _ -> readLocalFile fp -- get from local file system
306 where readLocalFile f = do
289 where readLocalFile f = do
307− cont <- readFileStrict f
290+ cont <- readFileStrict f
308− return (cont, mime)
291+ return (cont, mime)
309 httpcolon = URI{ uriScheme = "http:",
292 httpcolon = URI{ uriScheme = "http:",
310 uriAuthority = Nothing,
293 uriAuthority = Nothing,
311 uriPath = "",
294 uriPath = "",
@@ -418,17 +401,20 @@ instance PandocMonad PandocPure where
418 modifyPureState $ \st -> st { stUniqStore = us }
401 modifyPureState $ \st -> st { stUniqStore = us }
420 _ -> M.fail "uniq store ran out of elements"
403 _ -> M.fail "uniq store ran out of elements"
404+ openURL u = throwError $ PandocIOError u $
421− openURL _ = throwError $ PandocSomeError "Cannot open URL in PandocPure"
405+ userError "Cannot open URL in PandocPure"
422 readFileLazy fp = do
406 readFileLazy fp = do
423 fps <- getsPureState stFiles
407 fps <- getsPureState stFiles
424 case infoFileContents <$> getFileInfo fp fps of
408 case infoFileContents <$> getFileInfo fp fps of
425 Just bs -> return (BL.fromStrict bs)
409 Just bs -> return (BL.fromStrict bs)
426− Nothing -> throwError $ PandocFileReadError fp
410+ Nothing -> throwError $ PandocIOError fp
411+ (userError "File not found in PureState")
427 readFileStrict fp = do
412 readFileStrict fp = do
428 fps <- getsPureState stFiles
413 fps <- getsPureState stFiles
429 case infoFileContents <$> getFileInfo fp fps of
414 case infoFileContents <$> getFileInfo fp fps of
430 Just bs -> return bs
415 Just bs -> return bs
431− Nothing -> throwError $ PandocFileReadError fp
416+ Nothing -> throwError $ PandocIOError fp
417+ (userError "File not found in PureState")
432 readDataFile Nothing "reference.docx" = do
418 readDataFile Nothing "reference.docx" = do
433 (B.concat . BL.toChunks . fromArchive) <$> getsPureState stReferenceDocx
419 (B.concat . BL.toChunks . fromArchive) <$> getsPureState stReferenceDocx
434 readDataFile Nothing "reference.odt" = do
420 readDataFile Nothing "reference.odt" = do
@@ -450,7 +436,8 @@ instance PandocMonad PandocPure where
450 fps <- getsPureState stFiles
436 fps <- getsPureState stFiles
451 case infoFileMTime <$> (getFileInfo fp fps) of
437 case infoFileMTime <$> (getFileInfo fp fps) of
452 Just tm -> return tm
438 Just tm -> return tm
453− Nothing -> throwError $ PandocFileReadError fp
439+ Nothing -> throwError $ PandocIOError fp
440+ (userError "Can't get modification time")
455 getCommonState = PandocPure $ lift $ get
442 getCommonState = PandocPure $ lift $ get
456 putCommonState x = PandocPure $ lift $ put x
443 putCommonState x = PandocPure $ lift $ put x