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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
--------------------------------------------------------------------------------
-- | Internally used compiler module
{-# LANGUAGE GADTs #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module Hakyll.Core.Compiler.Internal
( CompilerRead (..)
, Compiler (..)
, compilerTell
, compilerAsk
, compilerThrow
, compilerCatch
) where
--------------------------------------------------------------------------------
import Control.Applicative (Alternative (..),
Applicative (..))
import Data.Monoid (mappend, mempty)
--------------------------------------------------------------------------------
import Hakyll.Core.Dependencies
import Hakyll.Core.Identifier
import Hakyll.Core.Logger
import Hakyll.Core.ResourceProvider
import Hakyll.Core.Routes
import Hakyll.Core.Store
--------------------------------------------------------------------------------
-- | Environment in which a compiler runs
data CompilerRead = CompilerRead
{ -- | Target identifier
compilerIdentifier :: Identifier ()
, -- | Resource provider
compilerResourceProvider :: ResourceProvider
, -- | List of all known identifiers
compilerUniverse :: [Identifier ()]
, -- | Site routes
compilerRoutes :: Routes
, -- | Compiler store
compilerStore :: Store
, -- | Flag indicating if the underlying resource was modified
compilerResourceModified :: Bool
, -- | Logger
compilerLogger :: Logger
}
--------------------------------------------------------------------------------
type CompilerWrite = [Dependency]
--------------------------------------------------------------------------------
data CompilerResult a where
CompilerDone :: a -> CompilerWrite -> CompilerResult a
CompilerError :: String -> CompilerResult a
CompilerRequire :: Identifier b -> (b -> Compiler a) -> CompilerResult a
--------------------------------------------------------------------------------
newtype Compiler a = Compiler
{ unCompiler :: CompilerRead -> IO (CompilerResult a)
}
--------------------------------------------------------------------------------
instance Functor Compiler where
fmap f (Compiler c) = Compiler $ \r -> do
res <- c r
return $ case res of
CompilerDone x w -> CompilerDone (f x) w
CompilerError e -> CompilerError e
CompilerRequire i g -> CompilerRequire i (\x -> fmap f (g x))
{-# INLINE fmap #-}
--------------------------------------------------------------------------------
instance Monad Compiler where
return x = Compiler $ \_ -> return $ CompilerDone x mempty
{-# INLINE return #-}
Compiler c >>= f = Compiler $ \r -> do
res <- c r
case res of
CompilerDone x w -> do
res' <- unCompiler (f x) r
return $ case res' of
CompilerDone y w' -> CompilerDone y (w `mappend` w')
CompilerError e -> CompilerError e
CompilerRequire i g -> CompilerRequire i $ \z -> do
compilerTell w -- Save dependencies!
g z
CompilerError e -> return $ CompilerError e
CompilerRequire i g -> return $ CompilerRequire i $ \z -> g z >>= f
{-# INLINE (>>=) #-}
--------------------------------------------------------------------------------
instance Applicative Compiler where
pure x = return x
{-# INLINE pure #-}
f <*> x = f >>= \f' -> fmap f' x
{-# INLINE (<*>) #-}
--------------------------------------------------------------------------------
instance Alternative Compiler where
empty = compilerThrow "Hakyll.Core.Compiler.Internal: empty alternative"
x <|> y = compilerCatch x (\_ -> y)
{-# INLINE (<|>) #-}
--------------------------------------------------------------------------------
compilerAsk :: Compiler CompilerRead
compilerAsk = Compiler $ \r -> return $ CompilerDone r mempty
{-# INLINE compilerAsk #-}
--------------------------------------------------------------------------------
compilerTell :: [Dependency] -> Compiler ()
compilerTell deps = Compiler $ \_ -> return $ CompilerDone () deps
{-# INLINE compilerTell #-}
--------------------------------------------------------------------------------
compilerThrow :: String -> Compiler a
compilerThrow e = Compiler $ \_ -> return $ CompilerError e
{-# INLINE compilerThrow #-}
--------------------------------------------------------------------------------
compilerCatch :: Compiler a -> (String -> Compiler a) -> Compiler a
compilerCatch (Compiler x) f = Compiler $ \r -> do
res <- x r
case res of
CompilerError e -> unCompiler (f e) r
_ -> return res
{-# INLINE compilerCatch #-}
{-
--------------------------------------------------------------------------------
-- | The compiler monad
newtype CompilerM a = CompilerM
{ unCompilerM :: ErrorT String (ReaderT CompilerEnvironment IO) a
} deriving (Monad, Functor, Applicative)
--------------------------------------------------------------------------------
-- | The compiler arrow
data Compiler a = Compiler
{ compilerDependencies :: Reader DependencyEnvironment Dependencies
, compilerJob :: CompilerM a
}
--------------------------------------------------------------------------------
instance Functor Compiler where
fmap f (Compiler d j) = Compiler d $ fmap f j
{-# INLINE fmap #-}
--------------------------------------------------------------------------------
instance Applicative Compiler where
pure = fromJob . return
{-# INLINE pure #-}
Compiler d1 j1 <*> Compiler d2 j2 =
Compiler (liftM2 S.union d1 d2) $ j1 <*> j2
{-# INLINE (<*>) #-}
-}
|