blob: 1d79b4aaba093e6d4c5bb46d0ffbe3fa2075d274 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
module Text.Hakyll.Util
( toDestination,
toCache,
makeDirectories,
getRecursiveContents,
trim,
split,
isCacheValid
) where
import System.Directory
import System.FilePath
import Control.Monad
import Data.Char
import Data.List
toDestination :: FilePath -> FilePath
toDestination path = "_site" </> path
toCache :: FilePath -> FilePath
toCache path = "_cache" </> path
-- | Given a path to a file, try to make the path writable by making
-- all directories on the path.
makeDirectories :: FilePath -> IO ()
makeDirectories path = createDirectoryIfMissing True dir
where dir = takeDirectory path
-- | Get all contents of a directory. Note that files starting with a dot (.)
-- will be ignored.
getRecursiveContents :: FilePath -> IO [FilePath]
getRecursiveContents topdir = do
names <- getDirectoryContents topdir
let properNames = filter isProper names
paths <- forM properNames $ \name -> do
let path = topdir </> name
isDirectory <- doesDirectoryExist path
if isDirectory
then getRecursiveContents path
else return [path]
return (concat paths)
where isProper = not . (== '.') . head
-- | Trim a string (drop spaces and tabs at both sides).
trim :: String -> String
trim = reverse . trim' . reverse . trim'
where trim' = dropWhile isSpace
-- | Split a list at a certain element.
split :: (Eq a) => a -> [a] -> [[a]]
split element = unfoldr splitOnce
where splitOnce l = let r = break (== element) l
in case r of ([], []) -> Nothing
(x, xs) -> if null xs
then Just (x, [])
else Just (x, tail xs)
-- | Check is a cache file is still valid.
isCacheValid :: FilePath -> [FilePath] -> IO Bool
isCacheValid cache depends = doesFileExist cache >>= \exists ->
if not exists then return False
else do dependsModified <- (mapM getModificationTime depends) >>= return . maximum
cacheModified <- getModificationTime cache
return (cacheModified >= dependsModified)
|