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