aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2010-11-19 22:13:30 -0800
committerJohn MacFarlane <jgm@berkeley.edu>2010-11-19 22:13:30 -0800
commit9cb0581de6b485ddfbd37e66414990339cd44b72 (patch)
treea7046e5569e6cf5ce2c6394f82684a86a1729c2d
parent6390103509caba5930c8ac45d31364b244607547 (diff)
downloadpandoc-9cb0581de6b485ddfbd37e66414990339cd44b72.tar.gz
Shared: Added findFirstFile, findDataFile, refactored readDataFile.
-rw-r--r--src/Text/Pandoc/Shared.hs29
1 files changed, 24 insertions, 5 deletions
diff --git a/src/Text/Pandoc/Shared.hs b/src/Text/Pandoc/Shared.hs
index 0fdaf42f3..67c5153c7 100644
--- a/src/Text/Pandoc/Shared.hs
+++ b/src/Text/Pandoc/Shared.hs
@@ -71,6 +71,8 @@ module Text.Pandoc.Shared (
defaultWriterOptions,
-- * File handling
inDirectory,
+ findFirstFile,
+ findDataFile,
readDataFile
) where
@@ -538,11 +540,28 @@ inDirectory path action = do
setCurrentDirectory oldDir
return result
+-- | Get full file path for the first of a list of files found in the
+-- specified directory.
+findFirstFile :: (Maybe FilePath) -> [FilePath] -> IO (Maybe FilePath)
+findFirstFile Nothing _ = return Nothing
+findFirstFile (Just _) [] = return Nothing
+findFirstFile (Just dir) (f:fs) = do
+ ex <- doesFileExist (dir </> f)
+ if ex
+ then return $ Just (dir </> f)
+ else findFirstFile (Just dir) fs
+
+-- | Get file path for data file, either from specified user data directory,
+-- or, if not found there, from Cabal data directory.
+findDataFile :: Maybe FilePath -> FilePath -> IO FilePath
+findDataFile Nothing f = getDataFileName f
+findDataFile (Just u) f = do
+ ex <- doesFileExist (u </> f)
+ if ex
+ then return (u </> f)
+ else getDataFileName f
+
-- | Read file from specified user data directory or, if not found there, from
-- Cabal data directory.
readDataFile :: Maybe FilePath -> FilePath -> IO String
-readDataFile userDir fname =
- case userDir of
- Nothing -> getDataFileName fname >>= UTF8.readFile
- Just u -> catch (UTF8.readFile $ u </> fname)
- (\_ -> getDataFileName fname >>= UTF8.readFile)
+readDataFile userDir fname = findDataFile userDir fname >>= UTF8.readFile