diff options
author | John MacFarlane <jgm@berkeley.edu> | 2017-07-14 17:28:13 +0200 |
---|---|---|
committer | John MacFarlane <jgm@berkeley.edu> | 2017-07-14 17:28:13 +0200 |
commit | df5a00990e6d7ee410544e1d6daabb0e102348d6 (patch) | |
tree | 4dcff57fae66f40be2542203f4b5b06f48232cd3 /src | |
parent | f8fef1ebb053c3e5a66c1a9a91f3e04940a6fdd6 (diff) | |
download | pandoc-df5a00990e6d7ee410544e1d6daabb0e102348d6.tar.gz |
Class: make addToFileTree handle directories recursively.
Diffstat (limited to 'src')
-rw-r--r-- | src/Text/Pandoc/Class.hs | 21 |
1 files changed, 15 insertions, 6 deletions
diff --git a/src/Text/Pandoc/Class.hs b/src/Text/Pandoc/Class.hs index 6906873d7..63b7419c4 100644 --- a/src/Text/Pandoc/Class.hs +++ b/src/Text/Pandoc/Class.hs @@ -102,7 +102,8 @@ import qualified Data.ByteString as B import qualified Data.ByteString.Lazy as BL import qualified System.Environment as IO (lookupEnv) import System.FilePath.Glob (match, compile) -import System.Directory (createDirectoryIfMissing) +import System.Directory (createDirectoryIfMissing, listDirectory, + doesDirectoryExist) import System.FilePath ((</>), (<.>), takeDirectory, takeExtension, dropExtension, isRelative, normalise) import qualified System.FilePath.Glob as IO (glob) @@ -478,13 +479,21 @@ newtype FileTree = FileTree {unFileTree :: M.Map FilePath FileInfo} getFileInfo :: FilePath -> FileTree -> Maybe FileInfo getFileInfo fp tree = M.lookup fp $ unFileTree tree +-- | Add the specified file to the FileTree. If file +-- is a directory, add its contents recursively. addToFileTree :: FileTree -> FilePath -> IO FileTree addToFileTree (FileTree treemap) fp = do - contents <- B.readFile fp - mtime <- IO.getModificationTime fp - return $ FileTree $ - M.insert fp FileInfo{ infoFileMTime = mtime - , infoFileContents = contents } treemap + isdir <- doesDirectoryExist fp + if isdir + then do -- recursively add contents of directories + fs <- map (fp </>) <$> listDirectory fp + foldM addToFileTree (FileTree treemap) fs + else do + contents <- B.readFile fp + mtime <- IO.getModificationTime fp + return $ FileTree $ + M.insert fp FileInfo{ infoFileMTime = mtime + , infoFileContents = contents } treemap newtype PandocPure a = PandocPure { unPandocPure :: ExceptT PandocError |