on ca2e161 help sign in
60 lines 2.2 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 directory
11 ) where
13import System.Directory
14import System.FilePath
15import Control.Monad
17-- | Convert a relative filepath to a filepath in the destination (_site).
18toDestination :: FilePath -> FilePath
19toDestination path = "_site" </> path
21-- | Convert a relative filepath to a filepath in the cache (_cache).
22toCache :: FilePath -> FilePath
23toCache path = "_cache" </> path
25-- | Get the url for a given page.
26toURL :: FilePath -> FilePath
27toURL = flip addExtension ".html" . dropExtension
29-- | Given a path to a file, try to make the path writable by making
30-- all directories on the path.
31makeDirectories :: FilePath -> IO ()
32makeDirectories path = createDirectoryIfMissing True dir
33 where dir = takeDirectory path
35-- | Get all contents of a directory. Note that files starting with a dot (.)
36-- will be ignored.
37getRecursiveContents :: FilePath -> IO [FilePath]
38getRecursiveContents topdir = do
39 names <- getDirectoryContents topdir
40 let properNames = filter isProper names
41 paths <- forM properNames $ \name -> do
42 let path = topdir </> name
43 isDirectory <- doesDirectoryExist path
44 if isDirectory
45 then getRecursiveContents path
46 else return [path]
47 return (concat paths)
48 where isProper = not . (== '.') . head
50-- | Perform an IO action on every file in a given directory.
51directory :: (FilePath -> IO ()) -> FilePath -> IO ()
52directory action dir = getRecursiveContents dir >>= mapM_ action
54-- | Check is a cache file is still valid.
55isCacheValid :: FilePath -> [FilePath] -> IO Bool
56isCacheValid cache depends = doesFileExist cache >>= \exists ->
57 if not exists then return False
58 else do dependsModified <- (mapM getModificationTime depends) >>= return . maximum
59 cacheModified <- getModificationTime cache
60 return (cacheModified >= dependsModified)