on c1d93eb help sign in
55 lines 2.0 kB history raw
1-- | A module containing various function for manipulating and examinating
2-- files and directories.
3module Text.Hakyll.File
4 ( toDestination,
5 toCache,
6 toURL,
7 makeDirectories,
8 getRecursiveContents,
9 isCacheValid
10 ) where
12import System.Directory
13import System.FilePath
14import Control.Monad
16-- | Convert a relative filepath to a filepath in the destination (_site).
17toDestination :: FilePath -> FilePath
18toDestination path = "_site" </> path
20-- | Convert a relative filepath to a filepath in the cache (_cache).
21toCache :: FilePath -> FilePath
22toCache path = "_cache" </> path
24-- | Get the url for a given page.
25toURL :: FilePath -> FilePath
26toURL = flip addExtension ".html" . dropExtension
28-- | Given a path to a file, try to make the path writable by making
29-- all directories on the path.
30makeDirectories :: FilePath -> IO ()
31makeDirectories path = createDirectoryIfMissing True dir
32 where dir = takeDirectory path
34-- | Get all contents of a directory. Note that files starting with a dot (.)
35-- will be ignored.
36getRecursiveContents :: FilePath -> IO [FilePath]
37getRecursiveContents topdir = do
38 names <- getDirectoryContents topdir
39 let properNames = filter isProper names
40 paths <- forM properNames $ \name -> do
41 let path = topdir </> name
42 isDirectory <- doesDirectoryExist path
43 if isDirectory
44 then getRecursiveContents path
45 else return [path]
46 return (concat paths)
47 where isProper = not . (== '.') . head
49-- | Check is a cache file is still valid.
50isCacheValid :: FilePath -> [FilePath] -> IO Bool
51isCacheValid cache depends = doesFileExist cache >>= \exists ->
52 if not exists then return False
53 else do dependsModified <- (mapM getModificationTime depends) >>= return . maximum
54 cacheModified <- getModificationTime cache
55 return (cacheModified >= dependsModified)