summaryrefslogtreecommitdiff
path: root/src/Text/Hakyll/RenderAction.hs
blob: 28c8e091d474ed200573a4c5677670b1c43052d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
module Text.Hakyll.RenderAction
    ( RenderAction (..)
    , createRenderAction
    , createSimpleRenderAction
    , createManipulationAction
    , chain
    , runRenderAction
    ) where

import Prelude hiding ((.), id)
import Control.Category
import Control.Monad ((<=<), mplus)

import Text.Hakyll.Hakyll
import Text.Hakyll.Context

data RenderAction a b = RenderAction
    { actionDependencies :: [FilePath]
    , actionUrl          :: Maybe (Hakyll FilePath)
    , actionFunction     :: a -> Hakyll b
    }

createRenderAction :: (a -> Hakyll b) -> RenderAction a b
createRenderAction f = RenderAction
    { actionDependencies = []
    , actionUrl          = Nothing
    , actionFunction     = f
    }

createSimpleRenderAction :: Hakyll b -> RenderAction () b
createSimpleRenderAction = createRenderAction . const

instance Category RenderAction where
    id = RenderAction
        { actionDependencies = []
        , actionUrl          = Nothing
        , actionFunction     = return
        }

    x . y = RenderAction
        { actionDependencies = actionDependencies x ++ actionDependencies y
        , actionUrl          = actionUrl y `mplus` actionUrl x
        , actionFunction     = actionFunction x <=< actionFunction y
        }

createManipulationAction :: ContextManipulation -> RenderAction Context Context
createManipulationAction = createRenderAction . (return .)

chain :: [RenderAction a a] -> RenderAction a a
chain = foldl1 (>>>)

runRenderAction :: RenderAction () a -> Hakyll a
runRenderAction action = actionFunction action ()