summaryrefslogtreecommitdiff
path: root/src/Text/Hakyll/RenderAction.hs
blob: a84ce46b4e70882978cb0fbd8c8ecf02b8f32e22 (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
module Text.Hakyll.RenderAction
    ( RenderAction (..)
    , fromRenderable
    ) where

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

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

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

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

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

fromRenderable :: (Renderable a)
               => a
               -> RenderAction () Context
fromRenderable renderable = RenderAction
    { actionDependencies = getDependencies renderable
    , actionDestination  = Just $ getUrl renderable
    , actionFunction     = const $ toContext renderable
    }