diff --git a/src/Text/Hakyll/Internal/Render.hs b/src/Text/Hakyll/Internal/Render.hs index 739db7a..5432f4d 100644 --- a/src/Text/Hakyll/Internal/Render.hs +++ b/src/Text/Hakyll/Internal/Render.hs @@ -10,7 +10,6 @@ module Text.Hakyll.Internal.Render import qualified Data.Map as M import Control.Monad.Reader (liftIO) import Data.Maybe (fromMaybe) -import System.IO (hPutStrLn, stderr) import Text.Hakyll.Context (Context, ContextManipulation) import Text.Hakyll.File @@ -42,6 +41,4 @@ writePage = createRenderAction $ \initialContext -> do destination <- toDestination url makeDirectories destination     -- Substitute $root here, just before writing. - liftIO $ do - writeFile destination $ finalSubstitute (fromString body) context - hPutStrLn stderr $ "Writing " ++ destination + liftIO $ writeFile destination $ finalSubstitute (fromString body) context diff --git a/src/Text/Hakyll/Render.hs b/src/Text/Hakyll/Render.hs index ef7493c..3c62fe2 100644 --- a/src/Text/Hakyll/Render.hs +++ b/src/Text/Hakyll/Render.hs @@ -1,8 +1,7 @@ -- | Module containing rendering functions. All these functions are used to -- render files to the @_site@ directory. module Text.Hakyll.Render - ( depends - , render + ( render , renderWith , renderAndConcat , renderAndConcatWith @@ -12,7 +11,6 @@ module Text.Hakyll.Render , css ) where -import Control.Monad (unless) import Control.Arrow ((>>>)) import Control.Monad.Reader (liftIO) import System.Directory (copyFile) @@ -27,16 +25,6 @@ import Text.Hakyll.Internal.CompressCss import Text.Hakyll.Internal.Render import Text.Hakyll.Internal.Template (readTemplate) --- | Execute an IO action only when the cache is invalid. -depends :: FilePath -- ^ File to be rendered or created. - -> [FilePath] -- ^ Files the render depends on. - -> Hakyll () -- ^ Action to execute when the file is out of date. - -> Hakyll () -depends file dependencies action = do - destination <- toDestination file - valid <- isFileMoreRecent destination dependencies - unless valid action - -- | Render to a Page. render :: FilePath -- ^ Template to use for rendering. -> RenderAction Context Context -- ^ The render computation. @@ -113,7 +101,7 @@ renderChainWith :: ContextManipulation -> RenderAction () Context -> Hakyll () renderChainWith manipulation templatePaths initial = - runRenderAction renderChainWith' + runRenderActionIfNeeded renderChainWith' where renderChainWith' :: RenderAction () () renderChainWith' = initial >>> manipulationAction >>> chain' >>> writePage @@ -125,30 +113,18 @@ renderChainWith manipulation templatePaths initial = -- | Mark a certain file as static, so it will just be copied when the site is -- generated. static :: FilePath -> Hakyll () -static source = runRenderAction static' +static source = runRenderActionIfNeeded static' where - static' = RenderAction - { actionDependencies = [source] - , actionUrl = Just $ return source - , actionFunction = actionFunction' - } - - actionFunction' _ = do + static' = createFileRenderAction source $ do destination <- toDestination source makeDirectories destination liftIO $ copyFile source destination -- | Render a css file, compressing it. css :: FilePath -> Hakyll () -css source = runRenderAction css' +css source = runRenderActionIfNeeded css' where - css' = RenderAction - { actionDependencies = [source] - , actionUrl = Just $ return source - , actionFunction = actionFunction' - } - - actionFunction' _ = do + css' = createFileRenderAction source $ do contents <- liftIO $ readFile source destination <- toDestination source makeDirectories destination diff --git a/src/Text/Hakyll/RenderAction.hs b/src/Text/Hakyll/RenderAction.hs index 9fb9477..b84a3c5 100644 --- a/src/Text/Hakyll/RenderAction.hs +++ b/src/Text/Hakyll/RenderAction.hs @@ -2,17 +2,22 @@ module Text.Hakyll.RenderAction ( RenderAction (..) , createRenderAction , createSimpleRenderAction + , createFileRenderAction , createManipulationAction , chain , runRenderAction + , runRenderActionIfNeeded ) where -import Prelude hiding ((.), id) import Control.Category -import Control.Monad ((<=<), mplus) +import Control.Monad ((<=<), mplus, unless) +import Control.Monad.Reader (liftIO) +import Prelude hiding ((.), id) +import System.IO (hPutStrLn, stderr) -import Text.Hakyll.Hakyll import Text.Hakyll.Context +import Text.Hakyll.File (toDestination, isFileMoreRecent) +import Text.Hakyll.Hakyll data RenderAction a b = RenderAction { actionDependencies :: [FilePath] @@ -26,6 +31,13 @@ createRenderAction f = id { actionFunction = f } createSimpleRenderAction :: Hakyll b -> RenderAction () b createSimpleRenderAction = createRenderAction . const +createFileRenderAction :: FilePath -> Hakyll b -> RenderAction () b +createFileRenderAction path action = RenderAction + { actionDependencies = [path] + , actionUrl = Just $ return path + , actionFunction = const action + } + instance Category RenderAction where id = RenderAction { actionDependencies = [] @@ -47,3 +59,13 @@ chain = foldl1 (>>>) runRenderAction :: RenderAction () a -> Hakyll a runRenderAction action = actionFunction action () + +runRenderActionIfNeeded :: RenderAction () () -> Hakyll () +runRenderActionIfNeeded action = do + url <- case actionUrl action of + (Just u) -> u + Nothing -> error "No url when checking dependencies." + destination <- toDestination url + valid <- isFileMoreRecent destination $ actionDependencies action + unless valid $ do liftIO $ hPutStrLn stderr $ "Rendering " ++ destination + runRenderAction action