on master help sign in
Moved some more functions from the IO monad to the Hakyll monad stack.
commit62330ceae51ea28f75768196ad5e3f93eb88b8a3 authorJasper Van der Jeugt <jaspervdj@gmail.com> dateFri 15 Jan 2010, 09:47 (17 years ago) parenta180016
4 files changed+38−30
src/Text/Hakyll/File.hs+18−14 src/Text/Hakyll/Page.hs+13−9 src/Text/Hakyll/Render.hs+6−6 src/Text/Hakyll/Render/Internal.hs+1−1
src/Text/Hakyll/File.hs+18−14
@@ -63,20 +63,20 @@ removeSpaces = map swap
6363
6464-- | Given a path to a file, try to make the path writable by making
6565-- all directories on the path.
66−makeDirectories :: FilePath -> IO ()
67−makeDirectories path = createDirectoryIfMissing True dir
66+makeDirectories :: FilePath -> Hakyll ()
67+makeDirectories path = liftIO $ createDirectoryIfMissing True dir
6868 where
6969 dir = takeDirectory path
7070
7171-- | Get all contents of a directory. Note that files starting with a dot (.)
7272-- will be ignored.
73−getRecursiveContents :: FilePath -> IO [FilePath]
73+getRecursiveContents :: FilePath -> Hakyll [FilePath]
7474getRecursiveContents topdir = do
75− names <- getDirectoryContents topdir
75+ names <- liftIO $ getDirectoryContents topdir
7676 let properNames = filter isProper names
7777 paths <- forM properNames $ \name -> do
7878 let path = topdir </> name
79− isDirectory <- doesDirectoryExist path
79+ isDirectory <- liftIO $ doesDirectoryExist path
8080 if isDirectory
8181 then getRecursiveContents path
8282 else return [path]
@@ -87,20 +87,24 @@ getRecursiveContents topdir = do
8787-- | A filter that takes all file names with a given extension. Prefix the
8888-- extension with a dot:
8989--
90−-- > havingExtension ".markdown" ["index.markdown", "style.css"] == ["index.markdown"]
90+-- > havingExtension ".markdown" [ "index.markdown"
91+-- > , "style.css"
92+-- > ] == ["index.markdown"]
9193havingExtension :: String -> [FilePath] -> [FilePath]
9294havingExtension extension = filter ((==) extension . takeExtension)
9395
94−-- | Perform an IO action on every file in a given directory.
96+-- | Perform a Hakyll action on every file in a given directory.
9597directory :: (FilePath -> Hakyll ()) -> FilePath -> Hakyll ()
9698directory action dir = do
97− contents <- liftIO $ getRecursiveContents dir
99+ contents <- getRecursiveContents dir
98100 mapM_ action contents
99101
100102-- | Check if a cache file is still valid.
101−isCacheValid :: FilePath -> [FilePath] -> IO Bool
102−isCacheValid cache depends = doesFileExist cache >>= \exists ->
103− if not exists then return False
104− else do dependsModified <- (mapM getModificationTime depends) >>= return . maximum
105− cacheModified <- getModificationTime cache
106− return (cacheModified >= dependsModified)
103+isCacheValid :: FilePath -> [FilePath] -> Hakyll Bool
104+isCacheValid cache depends = do
105+ exists <- liftIO $ doesFileExist cache
106+ if not exists
107+ then return False
108+ else do dependsModified <- liftIO $ mapM getModificationTime depends
109+ cacheModified <- liftIO $ getModificationTime cache
110+ return (cacheModified >= maximum dependsModified)
src/Text/Hakyll/Page.hs+13−9
@@ -84,26 +84,30 @@ isDelimiter = L.isPrefixOf "---"
8484
8585-- | Used for caching of files.
8686cachePage :: Page -> Hakyll ()
87−cachePage page@(Page mapping) = liftIO $ do
88− let destination = toCache $ getURL page
87+cachePage page@(Page mapping) = do
8988 makeDirectories destination
90− handle <- openFile destination WriteMode
91− hPutStrLn handle "---"
92− mapM_ (writePair handle) $ M.toList $ M.delete "body" mapping
93− hPutStrLn handle "---"
94− hPutStr handle $ getBody page
95− hClose handle
89+ liftIO writePageToCache
9690 where
91+ writePageToCache = do
92+ handle <- openFile destination WriteMode
93+ hPutStrLn handle "---"
94+ mapM_ (writePair handle) $ M.toList $ M.delete "body" mapping
95+ hPutStrLn handle "---"
96+ hPutStr handle $ getBody page
97+ hClose handle
98+
9799 writePair h (k, v) = do hPutStr h $ k ++ ": " ++ v
98100 hPutStrLn h ""
99101
102+ destination = toCache $ getURL page
103+
100104-- | Read a page from a file. Metadata is supported, and if the filename
101105-- has a .markdown extension, it will be rendered using pandoc. Note that
102106-- pages are not templates, so they should not contain $identifiers.
103107readPage :: FilePath -> Hakyll Page
104108readPage pagePath = do
105109 -- Check cache.
106− getFromCache <- liftIO $ isCacheValid cacheFile [pagePath]
110+ getFromCache <- isCacheValid cacheFile [pagePath]
107111 let path = if getFromCache then cacheFile else pagePath
108112
109113 -- Read file.
src/Text/Hakyll/Render.hs+6−6
@@ -31,7 +31,7 @@ depends :: FilePath -- ^ File to be rendered or created.
3131 -> Hakyll () -- ^ IO action to execute when the file is out of date.
3232 -> Hakyll ()
3333depends file dependencies action = do
34− valid <- liftIO $ isCacheValid (toDestination file) dependencies
34+ valid <- isCacheValid (toDestination file) dependencies
3535 unless valid action
3636
3737-- | Render to a Page.
@@ -93,17 +93,17 @@ renderChainWith manipulation templatePaths renderable =
9393-- | Mark a certain file as static, so it will just be copied when the site is
9494-- generated.
9595static :: FilePath -> Hakyll ()
96−static source = depends destination [source] (liftIO action)
96+static source = depends destination [source] action
9797 where
9898 destination = toDestination source
9999 action = do makeDirectories destination
100− copyFile source destination
100+ liftIO $ copyFile source destination
101101
102102-- | Render a css file, compressing it.
103103css :: FilePath -> Hakyll ()
104−css source = depends destination [source] (liftIO css')
104+css source = depends destination [source] css'
105105 where
106106 destination = toDestination source
107− css' = do contents <- readFile source
107+ css' = do contents <- liftIO $ readFile source
108108 makeDirectories destination
109− writeFile destination (compressCSS contents)
109+ liftIO $ writeFile destination (compressCSS contents)
src/Text/Hakyll/Render/Internal.hs+1−1
@@ -87,7 +87,7 @@ writePage page = do
8787 globalContext <- liftM hakyllGlobalContext ask
8888 let destination = toDestination url
8989 context = (M.singleton "root" $ toRoot url) `M.union` globalContext
90− liftIO $ makeDirectories destination
90+ makeDirectories destination
9191    -- Substitute $root here, just before writing.
9292 liftIO $ writeFile destination $ finalSubstitute (getBody page) context
9393 where