on master help sign in
Add target module
commit12c446785c76130a65c46cc603e767893b4818b5 authorJasper Van der Jeugt <jaspervdj@gmail.com> dateFri 24 Dec 2010, 16:55 (16 years ago) parent4b7c42d
3 files changed+59−1
src/Hakyll/Core/DirectedGraph/Internal.hs+2−1 src/Hakyll/Core/Target.hs+11 src/Hakyll/Core/Target/Internal.hs+46
src/Hakyll/Core/DirectedGraph/Internal.hs+2−1
@@ -1,4 +1,5 @@
1−-- | Internal structure of the DirectedGraph type. Not exported in the library.
1+-- | Internal structure of the DirectedGraph type. Not exported outside of the
2+-- library.
23--
34module Hakyll.Core.DirectedGraph.Internal
45 ( Node (..)
src/Hakyll/Core/Target.hsnew file+11
@@ -0,0 +1,11 @@
1+-- | A target represents one compilation unit, e.g. a blog post, a CSS file...
2+--
3+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
4+module Hakyll.Core.Target
5+ ( DependencyLookup
6+ , TargetM
7+ , Target
8+ , runTarget
9+ ) where
10+
11+import Hakyll.Core.Target.Internal
src/Hakyll/Core/Target/Internal.hsnew file+46
@@ -0,0 +1,46 @@
1+-- | Internal structure of a Target, not exported outside of the library
2+--
3+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
4+module Hakyll.Core.Target.Internal
5+ ( DependencyLookup
6+ , TargetM (..)
7+ , Target
8+ , runTarget
9+ ) where
10+
11+import Control.Monad.Trans (MonadIO)
12+import Control.Monad.Reader (ReaderT, runReaderT)
13+
14+import Hakyll.Core.Identifier
15+
16+-- | A lookup with which we can get dependencies
17+--
18+type DependencyLookup a = Identifier -> a
19+
20+-- | Environment for the target monad
21+--
22+data TargetEnvironment a = TargetEnvironment
23+ { targetIdentifier :: Identifier
24+ , targetDependencyLookup :: DependencyLookup a -- ^ Dependency lookup
25+ }
26+
27+-- | Monad for targets. In this monad, the user can compose targets and describe
28+-- how they should be created.
29+--
30+newtype TargetM a b = TargetM {unTargetM :: ReaderT (TargetEnvironment a) IO b}
31+ deriving (Monad, Functor, MonadIO)
32+
33+-- | Simplification of the 'TargetM' type for concrete cases: the type of the
34+-- returned item should equal the type of the dependencies.
35+--
36+type Target a = TargetM a a
37+
38+-- | Run a target, yielding an actual result.
39+--
40+runTarget :: Target a -> Identifier -> DependencyLookup a -> IO a
41+runTarget target id' lookup' = runReaderT (unTargetM target) env
42+ where
43+ env = TargetEnvironment
44+ { targetIdentifier = id'
45+ , targetDependencyLookup = lookup'
46+ }