on 333c19d help sign in
69 lines 2.4 kB history raw
1module Text.Hakyll.Util
2 ( toDestination,
3 toCache,
4 toURL,
5 makeDirectories,
6 getRecursiveContents,
7 trim,
8 split,
9 isCacheValid
10 ) where
12import System.Directory
13import System.FilePath
14import Control.Monad
15import Data.Char
16import Data.List
18toDestination :: FilePath -> FilePath
19toDestination path = "_site" </> path
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-- | Trim a string (drop spaces and tabs at both sides).
50trim :: String -> String
51trim = reverse . trim' . reverse . trim'
52 where trim' = dropWhile isSpace
54-- | Split a list at a certain element.
55split :: (Eq a) => a -> [a] -> [[a]]
56split element = unfoldr splitOnce
57 where splitOnce l = let r = break (== element) l
58 in case r of ([], []) -> Nothing
59 (x, xs) -> if null xs
60 then Just (x, [])
61 else Just (x, tail xs)
63-- | Check is a cache file is still valid.
64isCacheValid :: FilePath -> [FilePath] -> IO Bool
65isCacheValid cache depends = doesFileExist cache >>= \exists ->
66 if not exists then return False
67 else do dependsModified <- (mapM getModificationTime depends) >>= return . maximum
68 cacheModified <- getModificationTime cache
69 return (cacheModified >= dependsModified)