@@ -71,6 +71,8 @@ module Text.Pandoc.Shared (
71 defaultWriterOptions,
71 defaultWriterOptions,
72 -- * File handling
72 -- * File handling
73 inDirectory,
73 inDirectory,
74 readDataFile
76 readDataFile
@@ -538,11 +540,28 @@ inDirectory path action = do
538 setCurrentDirectory oldDir
540 setCurrentDirectory oldDir
539 return result
541 return result
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)
551+ then return $ Just (dir </> f)
552+ else findFirstFile (Just dir) fs
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)
561+ then return (u </> f)
562+ else getDataFileName f
541-- | Read file from specified user data directory or, if not found there, from
564-- | Read file from specified user data directory or, if not found there, from
542-- Cabal data directory.
565-- Cabal data directory.
543readDataFile :: Maybe FilePath -> FilePath -> IO String
566readDataFile :: Maybe FilePath -> FilePath -> IO String
544−readDataFile userDir fname =
567+readDataFile userDir fname = findDataFile userDir fname >>= UTF8.readFile
546− Nothing -> getDataFileName fname >>= UTF8.readFile
547− Just u -> catch (UTF8.readFile $ u </> fname)
548− (\_ -> getDataFileName fname >>= UTF8.readFile)