on master help sign in
Added caching again. But now the more sexy, stable and fast version.
commit892cae9da263d9f2fad4d93dd6d6c50fb12add16 authorJasper Van der Jeugt <jaspervdj@gmail.com> dateWed 20 Jan 2010, 17:10 (17 years ago) parent8602f23
2 files changed+27−5
src/Text/Hakyll/Internal/Cache.hs+14−2 src/Text/Hakyll/Page.hs+13−3
src/Text/Hakyll/Internal/Cache.hs+14−2
@@ -3,10 +3,22 @@ module Text.Hakyll.Internal.Cache
33 , getFromCache
44 ) where
55
6+import Control.Monad.Reader (liftIO)
67import Text.Hakyll.Hakyll (Hakyll)
8+import Text.Hakyll.File
79
810storeInCache :: (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)
1015
1116getFromCache :: (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
1818
1919import Text.Pandoc
2020
21+import Text.Hakyll.Internal.Cache
2122import Text.Hakyll.Hakyll (Hakyll)
2223import Text.Hakyll.File
2324import Text.Hakyll.Util (trim)
@@ -28,6 +29,7 @@ import Text.Hakyll.Regex (substituteRegex, matchesRegex)
2829-- | A Page is basically key-value mapping. Certain keys have special
2930-- meanings, like for example url, body and title.
3031data Page = Page Context
32+ deriving (Show, Read)
3133
3234-- | Create a Page from a key-value mapping.
3335fromContext :: Context -> Page
@@ -110,8 +112,8 @@ readSection renderFunction isFirst ls
110112
111113-- | Read a page from a file. Metadata is supported, and if the filename
112114-- has a @.markdown@ extension, it will be rendered using pandoc.
113−readPage :: FilePath -> Hakyll Page
114−readPage path = do
115+readPageFromFile :: FilePath -> Hakyll Page
116+readPageFromFile path = do
115117 let renderFunction = getRenderFunction $ takeExtension path
116118 sectionFunctions = map (readSection renderFunction)
117119 (True : repeat False)
@@ -128,11 +130,19 @@ readPage path = do
128130 ] ++ context
129131
130132 seq (($|) id rdeepseq context) $ liftIO $ hClose handle
131−
132133 return page
133134 where
134135 url = toURL path
135136
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+
136146-- Make pages renderable.
137147instance Renderable Page where
138148 getDependencies = (:[]) . getPagePath