on master help sign in
Made site and cache directory configurable. Caching stubs.
commit8602f23f7bcdcc3bec65ec98c70ee3f295482856 authorJasper Van der Jeugt <jaspervdj@gmail.com> dateWed 20 Jan 2010, 16:46 (17 years ago) parentd79022f
8 files changed+71−59
hakyll.cabal+1 src/Text/Hakyll.hs+21−24 src/Text/Hakyll/File.hs+20−11 src/Text/Hakyll/Hakyll.hs+4 src/Text/Hakyll/Internal/Cache.hs+12 src/Text/Hakyll/Render.hs+11−10 src/Text/Hakyll/Render/Internal.hs+2−2 tests/Tests.hs−12
hakyll.cabal+1
@@ -49,3 +49,4 @@ library
4949 Text.Hakyll.Regex
5050 Network.Hakyll.SimpleServer
5151 other-modules: Text.Hakyll.Render.Internal
52+ Text.Hakyll.Internal.Cache
src/Text/Hakyll.hs+21−24
@@ -4,7 +4,7 @@ module Text.Hakyll
44 , hakyllWithConfiguration
55 ) where
66
7−import Control.Monad.Reader (runReaderT)
7+import Control.Monad.Reader (runReaderT, liftIO)
88import Control.Monad (when)
99import qualified Data.Map as M
1010import System.Environment (getArgs, getProgName)
@@ -17,6 +17,8 @@ import Text.Hakyll.Hakyll
1717defaultHakyllConfiguration :: HakyllConfiguration
1818defaultHakyllConfiguration = HakyllConfiguration
1919 { additionalContext = M.empty
20+ , siteDirectory = "_site"
21+ , cacheDirectory = "_cache"
2022 }
2123
2224-- | Hakyll with a default configuration.
@@ -27,32 +29,27 @@ hakyll = hakyllWithConfiguration defaultHakyllConfiguration
2729hakyllWithConfiguration :: HakyllConfiguration -> Hakyll () -> IO ()
2830hakyllWithConfiguration configuration buildFunction = do
2931 args <- getArgs
30− case args of ["build"] -> build'
31− ["clean"] -> clean
32− ["preview", p] -> build' >> server (read p)
33− ["preview"] -> build' >> server 8000
34− ["server", p] -> server (read p)
35− ["server"] -> server 8000
36− _ -> help
37− where
38− build' = build configuration buildFunction
39−
40−-- | Build the site.
41−build :: HakyllConfiguration -> Hakyll () -> IO ()
42−build configuration buildFunction = do putStrLn "Generating..."
43− runReaderT buildFunction configuration
32+ let f = case args of ["build"] -> buildFunction
33+ ["clean"] -> clean
34+ ["preview", p] -> buildFunction >> server (read p)
35+ ["preview"] -> buildFunction >> server 8000
36+ ["server", p] -> server (read p)
37+ ["server"] -> server 8000
38+ _ -> help
39+ runReaderT f configuration
4440
4541-- | Clean up directories.
46−clean :: IO ()
47−clean = remove' "_site"
42+clean :: Hakyll ()
43+clean = do askHakyll siteDirectory >>= remove'
44+ askHakyll cacheDirectory >>= remove'
4845 where
49− remove' dir = do putStrLn $ "Removing " ++ dir ++ "..."
50− exists <- doesDirectoryExist dir
51− when exists $ removeDirectoryRecursive dir
46+ remove' dir = liftIO $ do putStrLn $ "Removing " ++ dir ++ "..."
47+ exists <- doesDirectoryExist dir
48+ when exists $ removeDirectoryRecursive dir
5249
5350-- | Show usage information.
54−help :: IO ()
55−help = do
51+help :: Hakyll ()
52+help = liftIO $ do
5653 name <- getProgName
5754 putStrLn $ "This is a Hakyll site generator program. You should always\n"
5855 ++ "run it from the project root directory.\n"
@@ -64,5 +61,5 @@ help = do
6461 ++ name ++ " preview [port] Generate site, then start a server.\n"
6562 ++ name ++ " server [port] Run a local test server.\n"
6663
67−server :: Integer -> IO ()
68−server p = simpleServer (fromIntegral p) "_site"
64+server :: Integer -> Hakyll ()
65+server p = askHakyll siteDirectory >>= liftIO . simpleServer (fromIntegral p)
src/Text/Hakyll/File.hs+20−11
@@ -2,13 +2,14 @@
22-- files and directories.
33module Text.Hakyll.File
44 ( toDestination
5+ , toCache
56 , toURL
67 , toRoot
78 , removeSpaces
89 , makeDirectories
910 , getRecursiveContents
1011 , havingExtension
11− , isCacheValid
12+ , isMoreRecent
1213 , directory
1314 ) where
1415
@@ -18,7 +19,7 @@ import Control.Monad
1819import Data.List (isPrefixOf)
1920import Control.Monad.Reader (liftIO)
2021
21−import Text.Hakyll.Hakyll (Hakyll)
22+import Text.Hakyll.Hakyll
2223
2324-- | Auxiliary function to remove pathSeparators form the start. We don't deal
2425-- with absolute paths here. We also remove $root from the start.
@@ -31,9 +32,17 @@ removeLeadingSeparator path
3132 path' = if "$root" `isPrefixOf` path then drop 5 path
3233 else path
3334
34−-- | Convert a relative filepath to a filepath in the destination (@_site@).
35−toDestination :: FilePath -> FilePath
36−toDestination path = "_site" </> removeLeadingSeparator path
35+-- | Convert a relative filepath to a filepath in the destination
36+-- (default: @_site@).
37+toDestination :: FilePath -> Hakyll FilePath
38+toDestination path = do dir <- askHakyll siteDirectory
39+ return $ dir </> removeLeadingSeparator path
40+
41+-- | Convert a relative filepath to a filepath in the cache
42+-- (default: @_cache@).
43+toCache :: FilePath -> Hakyll FilePath
44+toCache path = do dir <- askHakyll cacheDirectory
45+ return $ dir </> removeLeadingSeparator path
3746
3847-- | Get the url for a given page.
3948toURL :: FilePath -> FilePath
@@ -103,14 +112,14 @@ havingExtension extension = filter ((==) extension . takeExtension)
103112directory :: (FilePath -> Hakyll ()) -> FilePath -> Hakyll ()
104113directory action dir = getRecursiveContents dir >>= mapM_ action
105114
106−-- | Check if a cache file is still valid.
107−isCacheValid :: FilePath -- ^ The cached file.
115+-- | Check if a file is newer then a number of given files.
116+isMoreRecent :: FilePath -- ^ The cached file.
108117 -> [FilePath] -- ^ Dependencies of the cached file.
109118 -> Hakyll Bool
110−isCacheValid cache depends = do
111− exists <- liftIO $ doesFileExist cache
119+isMoreRecent file depends = do
120+ exists <- liftIO $ doesFileExist file
112121 if not exists
113122 then return False
114123 else do dependsModified <- liftIO $ mapM getModificationTime depends
115− cacheModified <- liftIO $ getModificationTime cache
116− return (cacheModified >= maximum dependsModified)
124+ fileModified <- liftIO $ getModificationTime file
125+ return (fileModified >= maximum dependsModified)
src/Text/Hakyll/Hakyll.hs+4
@@ -15,6 +15,10 @@ data HakyllConfiguration = HakyllConfiguration
1515 { -- | An additional context to use when rendering. This additional context
1616 -- is used globally.
1717 additionalContext :: Context
18+ , -- | Directory where the site is placed.
19+ siteDirectory :: FilePath
20+ , -- | Directory for cache files.
21+ cacheDirectory :: FilePath
1822 }
1923
2024-- | Our custom monad stack.
src/Text/Hakyll/Internal/Cache.hsnew file+12
@@ -0,0 +1,12 @@
1+module Text.Hakyll.Internal.Cache
2+ ( storeInCache
3+ , getFromCache
4+ ) where
5+
6+import Text.Hakyll.Hakyll (Hakyll)
7+
8+storeInCache :: (Show a) => a -> FilePath -> Hakyll ()
9+storeInCache = undefined
10+
11+getFromCache :: (Read a) => FilePath -> Hakyll (Maybe a)
12+getFromCache = undefined
src/Text/Hakyll/Render.hs+11−10
@@ -28,7 +28,8 @@ depends :: FilePath -- ^ File to be rendered or created.
2828 -> Hakyll () -- ^ IO action to execute when the file is out of date.
2929 -> Hakyll ()
3030depends file dependencies action = do
31− valid <- isCacheValid (toDestination file) dependencies
31+ destination <- toDestination file
32+ valid <- isMoreRecent destination dependencies
3233 unless valid action
3334
3435-- | Render to a Page.
@@ -108,17 +109,17 @@ renderChainWith manipulation templatePaths renderable =
108109-- | Mark a certain file as static, so it will just be copied when the site is
109110-- generated.
110111static :: FilePath -> Hakyll ()
111−static source = depends destination [source] action
112+static source = do destination <- toDestination source
113+ depends destination [source] (action destination)
112114 where
113− destination = toDestination source
114− action = do makeDirectories destination
115− liftIO $ copyFile source destination
115+ action destination = do makeDirectories destination
116+ liftIO $ copyFile source destination
116117
117118-- | Render a css file, compressing it.
118119css :: FilePath -> Hakyll ()
119−css source = depends destination [source] css'
120+css source = do destination <- toDestination source
121+ depends destination [source] (css' destination)
120122 where
121− destination = toDestination source
122− css' = do contents <- liftIO $ readFile source
123− makeDirectories destination
124− liftIO $ writeFile destination (compressCSS contents)
123+ css' destination = do contents <- liftIO $ readFile source
124+ makeDirectories destination
125+ liftIO $ writeFile destination (compressCSS contents)
src/Text/Hakyll/Render/Internal.hs+2−2
@@ -85,8 +85,8 @@ pureRenderChainWith manipulation templates context =
8585writePage :: Page -> Hakyll ()
8686writePage page = do
8787 additionalContext' <- askHakyll additionalContext
88− let destination = toDestination url
89− context = additionalContext' `M.union` M.singleton "root" (toRoot url)
88+ destination <- toDestination url
89+ let context = additionalContext' `M.union` M.singleton "root" (toRoot url)
9090 makeDirectories destination
9191    -- Substitute $root here, just before writing.
9292 liftIO $ writeFile destination $ finalSubstitute (getBody page) context
tests/Tests.hs−12
@@ -45,9 +45,6 @@ tests = [ testGroup "Util group"
4545 ]
4646
4747 , testGroup "File group"
48− [ testCase "toDestination 1" test_to_destination1
49− , testCase "toDestination 2" test_to_destination2
50− , testCase "toDestination 3" test_to_destination3
5148 , testCase "toRoot 1" test_to_root1
5249 , testCase "toRoot 2" test_to_root2
5350 , testCase "toRoot 3" test_to_root3
@@ -112,15 +109,6 @@ test_render_date2 = M.lookup "date" rendered @?= Just "Unknown date"
112109 rendered = renderDate "date" "%B %e, %Y" "Unknown date" $
113110 M.singleton "path" "2009-badness-30-a-title.markdown"
114111
115−-- toDestination test cases
116−test_to_destination1 = toDestination "/posts/foo.html"
117− @?= "_site/posts/foo.html"
118−
119−test_to_destination2 = toDestination "$root/posts/foo.html"
120− @?= "_site/posts/foo.html"
121−
122−test_to_destination3 = toDestination "foo.html" @?= "_site/foo.html"
123−
124112-- toRoot test cases
125113test_to_root1 = toRoot "/posts/foo.html" @?= ".."
126114test_to_root2 = toRoot "posts/foo.html" @?= ".."