on master help sign in
Added caching again. But now the more sexy, stable and fast version.
892cae9Jasper Van der Jeugt
src/Text/Hakyll/Internal/Cache.hs+14−2
@@ -3,10 +3,22 @@ module Text.Hakyll.Internal.Cache
3 , getFromCache
3 , getFromCache
4 ) where
4 ) where
5
5
6+import Control.Monad.Reader (liftIO)
6import Text.Hakyll.Hakyll (Hakyll)
7import Text.Hakyll.Hakyll (Hakyll)
8+import Text.Hakyll.File
7
9
8storeInCache :: (Show a) => a -> FilePath -> Hakyll ()
10storeInCache :: (Show a) => a -> FilePath -> Hakyll ()
9−storeInCache = undefined
11+storeInCache value path = do
12+ cachePath <- toCache path
13+ makeDirectories cachePath
14+ liftIO $ writeFile cachePath (show value)
10
15
11getFromCache :: (Read a) => FilePath -> Hakyll (Maybe a)
16getFromCache :: (Read a) => FilePath -> Hakyll (Maybe a)
12−getFromCache = undefined
17+getFromCache path = do
18+ cachePath <- toCache path
19+ valid <- isMoreRecent cachePath [path]
20+ if valid then liftIO (getFromCache' cachePath) >>= return . Just
21+ else return Nothing
22+ where
23+ getFromCache' cachePath = do c <- readFile cachePath
24+ return (read c)
src/Text/Hakyll/Page.hs+13−3
@@ -18,6 +18,7 @@ import System.IO
18
18
19import Text.Pandoc
19import Text.Pandoc
20
20
21+import Text.Hakyll.Internal.Cache
21import Text.Hakyll.Hakyll (Hakyll)
22import Text.Hakyll.Hakyll (Hakyll)
22import Text.Hakyll.File
23import Text.Hakyll.File
23import Text.Hakyll.Util (trim)
24import Text.Hakyll.Util (trim)
@@ -28,6 +29,7 @@ import Text.Hakyll.Regex (substituteRegex, matchesRegex)
28-- | A Page is basically key-value mapping. Certain keys have special
29-- | A Page is basically key-value mapping. Certain keys have special
29-- meanings, like for example url, body and title.
30-- meanings, like for example url, body and title.
30data Page = Page Context
31data Page = Page Context
32+ deriving (Show, Read)
31
33
32-- | Create a Page from a key-value mapping.
34-- | Create a Page from a key-value mapping.
33fromContext :: Context -> Page
35fromContext :: Context -> Page
@@ -110,8 +112,8 @@ readSection renderFunction isFirst ls
110
112
111-- | Read a page from a file. Metadata is supported, and if the filename
113-- | Read a page from a file. Metadata is supported, and if the filename
112-- has a @.markdown@ extension, it will be rendered using pandoc.
114-- has a @.markdown@ extension, it will be rendered using pandoc.
113−readPage :: FilePath -> Hakyll Page
115+readPageFromFile :: FilePath -> Hakyll Page
114−readPage path = do
116+readPageFromFile path = do
115 let renderFunction = getRenderFunction $ takeExtension path
117 let renderFunction = getRenderFunction $ takeExtension path
116 sectionFunctions = map (readSection renderFunction)
118 sectionFunctions = map (readSection renderFunction)
117 (True : repeat False)
119 (True : repeat False)
@@ -128,11 +130,19 @@ readPage path = do
128 ] ++ context
130 ] ++ context
129
131
130 seq (($|) id rdeepseq context) $ liftIO $ hClose handle
132 seq (($|) id rdeepseq context) $ liftIO $ hClose handle
131−
132 return page
133 return page
133 where
134 where
134 url = toURL path
135 url = toURL path
135
136
137+-- | Read a page. Might fetch it from the cache if available.
138+readPage :: FilePath -> Hakyll Page
139+readPage path = do
140+ cacheResult <- getFromCache path
141+ case cacheResult of (Just page) -> return page
142+ Nothing -> do page <- readPageFromFile path
143+ storeInCache page path
144+ return page
145+
136-- Make pages renderable.
146-- Make pages renderable.
137instance Renderable Page where
147instance Renderable Page where
138 getDependencies = (:[]) . getPagePath
148 getDependencies = (:[]) . getPagePath