on master help sign in
Added basic dependency checking.
commit453e81f746c3a0e254457881cc0ac8609874cf32 authorJasper Van der Jeugt <jaspervdj@gmail.com> dateMon 7 Dec 2009, 20:34 (17 years ago) parent35e6572
3 files changed+36−12
src/Text/Hakyll/Page.hs+7−2 src/Text/Hakyll/Render.hs+23−5 src/Text/Hakyll/Util.hs+6−5
src/Text/Hakyll/Page.hs+7−2
@@ -2,6 +2,7 @@ module Text.Hakyll.Page
22 ( Page,
33 PageValue,
44 addContext,
5+ toURL,
56 getURL,
67 getBody,
78 readPage,
@@ -33,6 +34,10 @@ type PageValue = B.ByteString
3334addContext :: String -> String -> Page -> Page
3435addContext key value = M.insert key (B.pack value)
3536
37+-- | Get the url for a given page.
38+toURL :: FilePath -> FilePath
39+toURL = flip addExtension ".html" . dropExtension
40+
3641-- | Get the URL for a certain page. This should always be defined. If
3742-- not, it will return trash.html.
3843getURL :: Page -> String
@@ -88,7 +93,7 @@ cachePage page = do
8893readPage :: FilePath -> IO Page
8994readPage pagePath = do
9095 -- Check cache.
91− getFromCache <- isCacheFileValid cacheFile pagePath
96+ getFromCache <- isCacheValid cacheFile [pagePath]
9297 let path = if getFromCache then cacheFile else pagePath
9398
9499 -- Read file.
@@ -108,7 +113,7 @@ readPage pagePath = do
108113 -- Cache if needed
109114 if getFromCache then return () else cachePage page
110115 return page
111− where url = addExtension (dropExtension pagePath) ".html"
116+ where url = toURL pagePath
112117 cacheFile = toCache url
113118
114119-- | Create a key-value mapping page from an association list.
src/Text/Hakyll/Render.hs+23−5
@@ -1,7 +1,10 @@
11module Text.Hakyll.Render
2− ( renderPage,
2+ ( depends,
3+ renderPage,
34 renderAndWrite,
5+ writePage,
46 renderAndConcat,
7+ renderChain,
58 static,
69 staticDirectory
710 ) where
@@ -17,6 +20,12 @@ import System.IO
1720import Text.Hakyll.Page
1821import Text.Hakyll.Util
1922
23+depends :: FilePath -> [FilePath] -> IO () -> IO ()
24+depends file dependencies action = do
25+ valid <- isCacheValid (toDestination file) dependencies
26+ if valid then return ()
27+ else action
28+
2029createContext :: Page -> Context
2130createContext = M.fromList . map packPair . M.toList
2231 where packPair (a, b) = (B.pack a, b)
@@ -30,11 +39,14 @@ renderPage templatePath page = do
3039 return $ M.insert "body" body page
3140
3241renderAndWrite :: FilePath -> Page -> IO ()
33−renderAndWrite templatePath page = do
34− rendered <- renderPage templatePath page
35− let destination = toDestination $ getURL rendered
42+renderAndWrite templatePath page =
43+ renderPage templatePath page >>= writePage
44+
45+writePage :: Page -> IO ()
46+writePage page = do
47+ let destination = toDestination $ getURL page
3648 makeDirectories destination
37− B.writeFile destination (getBody rendered)
49+ B.writeFile destination (getBody page)
3850
3951renderAndConcat :: FilePath -> [FilePath] -> IO B.ByteString
4052renderAndConcat templatePath paths = foldM concatRender' B.empty paths
@@ -45,6 +57,12 @@ renderAndConcat templatePath paths = foldM concatRender' B.empty paths
4557 let body = getBody rendered
4658 return $ B.append chunk $ body
4759
60+renderChain :: FilePath -> [FilePath] -> IO ()
61+renderChain pagePath templates = depends (toURL pagePath) (pagePath : templates) $
62+ do page <- readPage pagePath
63+ result <- foldM (flip renderPage) page templates
64+ writePage result
65+
4866static :: FilePath -> IO ()
4967static source = do
5068 makeDirectories destination
src/Text/Hakyll/Util.hs+6−5
@@ -5,7 +5,7 @@ module Text.Hakyll.Util
55 getRecursiveContents,
66 trim,
77 split,
8− isCacheFileValid
8+ isCacheValid
99 ) where
1010
1111import System.Directory
@@ -56,8 +56,9 @@ split element = unfoldr splitOnce
5656 else Just (x, tail xs)
5757
5858-- | Check is a cache file is still valid.
59−isCacheFileValid :: FilePath -> FilePath -> IO Bool
60−isCacheFileValid cache file = doesFileExist cache >>= \exists ->
59+isCacheValid :: FilePath -> [FilePath] -> IO Bool
60+isCacheValid cache depends = doesFileExist cache >>= \exists ->
6161 if not exists then return False
62− else liftM2 (<=) (getModificationTime file)
63− (getModificationTime cache)
62+ else do dependsModified <- (mapM getModificationTime depends) >>= return . maximum
63+ cacheModified <- getModificationTime cache
64+ return (cacheModified >= dependsModified)