on da12825 help sign in
hakyll/src/Hakyll/Core/Compiler.hs
68 lines 2.1 kB history raw
1-- | A Compiler manages targets and dependencies between targets.
2--
3{-# LANGUAGE GeneralizedNewtypeDeriving #-}
4module Hakyll.Core.Compiler
5 ( Compiler
6 , getIdentifier
7 , getResourceString
8 , require
9 , requireAll
10 ) where
12import Prelude hiding ((.), id)
13import Control.Arrow ((>>>))
14import Control.Applicative ((<$>))
15import Control.Monad.Reader (ask)
16import Control.Monad.Trans (liftIO)
17import Control.Category (Category, (.))
19import Data.Binary (Binary)
20import Data.Typeable (Typeable)
22import Hakyll.Core.Identifier
23import Hakyll.Core.Identifier.Pattern
24import Hakyll.Core.CompiledItem
25import Hakyll.Core.Writable
26import Hakyll.Core.ResourceProvider
27import Hakyll.Core.Compiler.Internal
29getIdentifier :: Compiler a Identifier
30getIdentifier = fromJob $ const $ CompilerM $
31 compilerIdentifier <$> ask
33getResourceString :: Compiler a String
34getResourceString = getIdentifier >>> getResourceString'
35 where
36 getResourceString' = fromJob $ \id' -> CompilerM $ do
37 provider <- compilerResourceProvider <$> ask
38 liftIO $ resourceString provider id'
40-- | Require another target. Using this function ensures automatic handling of
41-- dependencies
42--
43require :: (Binary a, Typeable a, Writable a)
44 => Identifier
45 -> (b -> a -> c)
46 -> Compiler b c
47require identifier f =
48 fromDependencies (const [identifier]) >>> fromJob require'
49 where
50 require' x = CompilerM $ do
51 lookup' <- compilerDependencyLookup <$> ask
52 return $ f x $ unCompiledItem $ lookup' identifier
54-- | Require a number of targets. Using this function ensures automatic handling
55-- of dependencies
56--
57requireAll :: (Binary a, Typeable a, Writable a)
58 => Pattern
59 -> (b -> [a] -> c)
60 -> Compiler b c
61requireAll pattern f =
62 fromDependencies getDeps >>> fromJob requireAll'
63 where
64 getDeps = matches pattern . resourceList
65 requireAll' x = CompilerM $ do
66 deps <- getDeps . compilerResourceProvider <$> ask
67 lookup' <- compilerDependencyLookup <$> ask
68 return $ f x $ map (unCompiledItem . lookup') deps