@@ -71,6 +71,8 @@ module Text.Pandoc.Shared (
7171 defaultWriterOptions,
7272 -- * File handling
7373 inDirectory,
74+ findFirstFile,
75+ findDataFile,
7476 readDataFile
7577 ) where
7678
@@ -538,11 +540,28 @@ inDirectory path action = do
538540 setCurrentDirectory oldDir
539541 return result
540542
543+-- | Get full file path for the first of a list of files found in the
544+-- specified directory.
545+findFirstFile :: (Maybe FilePath) -> [FilePath] -> IO (Maybe FilePath)
546+findFirstFile Nothing _ = return Nothing
547+findFirstFile (Just _) [] = return Nothing
548+findFirstFile (Just dir) (f:fs) = do
549+ ex <- doesFileExist (dir </> f)
550+ if ex
551+ then return $ Just (dir </> f)
552+ else findFirstFile (Just dir) fs
553+
554+-- | Get file path for data file, either from specified user data directory,
555+-- or, if not found there, from Cabal data directory.
556+findDataFile :: Maybe FilePath -> FilePath -> IO FilePath
557+findDataFile Nothing f = getDataFileName f
558+findDataFile (Just u) f = do
559+ ex <- doesFileExist (u </> f)
560+ if ex
561+ then return (u </> f)
562+ else getDataFileName f
563+
541564-- | Read file from specified user data directory or, if not found there, from
542565-- Cabal data directory.
543566readDataFile :: Maybe FilePath -> FilePath -> IO String
544−readDataFile userDir fname =
545− case userDir of
546− Nothing -> getDataFileName fname >>= UTF8.readFile
547− Just u -> catch (UTF8.readFile $ u </> fname)
548− (\_ -> getDataFileName fname >>= UTF8.readFile)
567+readDataFile userDir fname = findDataFile userDir fname >>= UTF8.readFile