summaryrefslogtreecommitdiff
path: root/src/Text/Hakyll/RenderAction.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Text/Hakyll/RenderAction.hs')
-rw-r--r--src/Text/Hakyll/RenderAction.hs28
1 files changed, 25 insertions, 3 deletions
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