@@ -5,6 +5,7 @@ module Text.Hakyll.File
55 , toCache
66 , toUrl
77 , toRoot
8+ , inDirectory
89 , inHakyllDirectory
910 , removeSpaces
1011 , makeDirectories
@@ -87,6 +88,15 @@ toRoot = emptyException . joinPath . map parent . splitPath
8788 emptyException [] = "."
8889 emptyException x = x
8990
91+-- | Check if a file is in a given directory.
92+--
93+inDirectory :: FilePath -- ^ File path
94+ -> FilePath -- ^ Directory
95+ -> Bool -- ^ Result
96+inDirectory path dir = case splitDirectories path of
97+ [] -> False
98+ (x : _) -> x == dir
99+
90100-- | Check if a file is in a Hakyll directory. With a Hakyll directory, we mean
91101-- a directory that should be "ignored" such as the @_site@ or @_cache@
92102-- directory.
@@ -101,11 +111,8 @@ toRoot = emptyException . joinPath . map parent . splitPath
101111--
102112inHakyllDirectory :: FilePath -> Hakyll Bool
103113inHakyllDirectory path =
104− or <$> mapM (liftM inDirectory . askHakyll) [siteDirectory, cacheDirectory]
105− where
106− inDirectory dir = case splitDirectories path of
107− [] -> False
108− (x : _) -> x == dir
114+ or <$> mapM (liftM (inDirectory path) . askHakyll)
115+ [siteDirectory, cacheDirectory]
109116
110117-- | Swaps spaces for '-'.
111118removeSpaces :: FilePath -> FilePath
@@ -123,17 +130,21 @@ makeDirectories path = liftIO $ createDirectoryIfMissing True dir
123130
124131-- | Get all contents of a directory. Note that files starting with a dot (.)
125132-- will be ignored.
133+--
126134getRecursiveContents :: FilePath -> Hakyll [FilePath]
127135getRecursiveContents topdir = do
128− names <- liftIO $ getDirectoryContents topdir
129− let properNames = filter isProper names
130− paths <- forM properNames $ \name -> do
131− let path = topdir </> name
132− isDirectory <- liftIO $ doesDirectoryExist path
133− if isDirectory
134− then getRecursiveContents path
135− else return [path]
136− return (concat paths)
136+ topdirExists <- liftIO $ doesDirectoryExist topdir
137+ if topdirExists
138+ then do names <- liftIO $ getDirectoryContents topdir
139+ let properNames = filter isProper names
140+ paths <- forM properNames $ \name -> do
141+ let path = topdir </> name
142+ isDirectory <- liftIO $ doesDirectoryExist path
143+ if isDirectory
144+ then getRecursiveContents path
145+ else return [normalise path]
146+ return (concat paths)
147+ else return []
137148 where
138149 isProper = not . (== '.') . head
139150