@@ -6,10 +6,11 @@ module Hakyll.Core.Compiler
6 , CompilerM
6 , CompilerM
7 , Compiler (..)
7 , Compiler (..)
8 , runCompiler
8 , runCompiler
9 , getIdentifier
10 , getIdentifier
10 , getResourceString
11 , getResourceString
12− -- , requireAll
13+ , requireAll
13 -- , compileFromString
14 -- , compileFromString
@@ -17,9 +18,9 @@ import Prelude hiding ((.), id)
17import Control.Arrow (second, (>>>))
18import Control.Arrow (second, (>>>))
18import Control.Applicative (Applicative, (<$>))
19import Control.Applicative (Applicative, (<$>))
19import Control.Monad.State (State, modify, runState)
20import Control.Monad.State (State, modify, runState)
20−import Control.Monad.Reader (ReaderT, ask, runReaderT)
21+import Control.Monad.Reader (ReaderT, Reader, ask, runReaderT, runReader)
21import Control.Monad.Trans (liftIO)
22import Control.Monad.Trans (liftIO)
22−import Control.Monad ((<=<))
23+import Control.Monad ((<=<), liftM2)
23import Data.Set (Set)
24import Data.Set (Set)
24import qualified Data.Set as S
25import qualified Data.Set as S
25import Control.Category (Category, (.), id)
26import Control.Category (Category, (.), id)
@@ -59,18 +60,17 @@ newtype CompilerM a = CompilerM
59-- | The compiler arrow
60-- | The compiler arrow
61data Compiler a b = Compiler
62data Compiler a b = Compiler
62− { -- TODO: Reader ResourceProvider Dependencies
63+ { compilerDependencies :: Reader ResourceProvider Dependencies
63− compilerDependencies :: Dependencies
64 , compilerJob :: a -> CompilerM b
64 , compilerJob :: a -> CompilerM b
67instance Category Compiler where
67instance Category Compiler where
68− id = Compiler S.empty return
68+ id = Compiler (return S.empty) return
69 (Compiler d1 j1) . (Compiler d2 j2) =
69 (Compiler d1 j1) . (Compiler d2 j2) =
70− Compiler (d1 `S.union` d2) (j1 <=< j2)
70+ Compiler (liftM2 S.union d1 d2) (j1 <=< j2)
72instance Arrow Compiler where
72instance Arrow Compiler where
73− arr f = Compiler S.empty (return . f)
73+ arr f = Compiler (return S.empty) (return . f)
74 first (Compiler d j) = Compiler d $ \(x, y) -> do
74 first (Compiler d j) = Compiler d $ \(x, y) -> do
76 return (x', y)
76 return (x', y)
@@ -91,13 +91,19 @@ runCompiler compiler identifier provider lookup' =
91 , compilerDependencyLookup = lookup'
91 , compilerDependencyLookup = lookup'
94−addDependency :: Identifier
94+getDependencies :: Compiler () a
97+getDependencies compiler provider =
98+ runReader (compilerDependencies compiler) provider
100+addDependencies :: (ResourceProvider -> [Identifier])
95− -> Compiler b b
101+ -> Compiler b b
96−addDependency id' = Compiler (S.singleton id') return
102+addDependencies deps = Compiler (S.fromList . deps <$> ask) return
98fromCompilerM :: (a -> CompilerM b)
104fromCompilerM :: (a -> CompilerM b)
99 -> Compiler a b
105 -> Compiler a b
100−fromCompilerM = Compiler S.empty
106+fromCompilerM = Compiler (return S.empty)
102getIdentifier :: Compiler () Identifier
108getIdentifier :: Compiler () Identifier
103getIdentifier = fromCompilerM $ const $ CompilerM $
109getIdentifier = fromCompilerM $ const $ CompilerM $
@@ -115,26 +121,32 @@ getResourceString = getIdentifier >>> getResourceString'
116require :: (Binary a, Typeable a, Writable a)
122require :: (Binary a, Typeable a, Writable a)
117 => Identifier
123 => Identifier
118− -> (a -> b -> c)
124+ -> (b -> a -> c)
119 -> Compiler b c
125 -> Compiler b c
126+require identifier f =
120−require identifier f = addDependency identifier >>> fromCompilerM require'
127+ addDependencies (const [identifier]) >>> fromCompilerM require'
122 require' x = CompilerM $ do
129 require' x = CompilerM $ do
123 lookup' <- compilerDependencyLookup <$> ask
130 lookup' <- compilerDependencyLookup <$> ask
124− return $ f (unCompiledItem $ lookup' identifier) x
131+ return $ f x $ unCompiledItem $ lookup' identifier
127-- | Require a number of targets. Using this function ensures automatic handling
133-- | Require a number of targets. Using this function ensures automatic handling
128-- of dependencies
134-- of dependencies
130requireAll :: (Binary a, Typeable a, Writable a)
136requireAll :: (Binary a, Typeable a, Writable a)
131 => Pattern
137 => Pattern
132− -> Compiler [a]
138+ -> (b -> [a] -> c)
133−requireAll pattern = CompilerM $ do
140+requireAll pattern f =
141+ addDependencies getDeps >>> fromCompilerM requireAll'
143+ getDeps = matches pattern . resourceList
144+ requireAll' x = CompilerM $ do
134− provider <- compilerResourceProvider <$> ask
145+ deps <- getDeps . compilerResourceProvider <$> ask
135− r <- unCompilerM $ mapM require $ matches pattern $ resourceList provider
146+ lookup' <- compilerDependencyLookup <$> ask
136− return $ sequence r
147+ return $ f x $ map (unCompiledItem . lookup') deps
138-- | Construct a target from a string, this string being the content of the
150-- | Construct a target from a string, this string being the content of the
139-- resource.
151-- resource.