@@ -1,7 +1,12 @@
11-- | This is the module which binds it all together
22--
3+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
34module Hakyll.Core.Run where
45
6+import Control.Applicative
7+import Control.Monad.Reader
8+import Control.Monad.State
9+import Control.Monad.Trans
510import Control.Arrow ((&&&))
611import Control.Monad (foldM, forM_, forM, filterM)
712import Data.Map (Map)
@@ -34,10 +39,26 @@ hakyll :: Rules -> IO ()
3439hakyll rules = do
3540 store <- makeStore "_store"
3641 provider <- fileResourceProvider
37− hakyllWith rules provider store
38−
39−hakyllWith :: Rules -> ResourceProvider -> Store -> IO ()
40−hakyllWith rules provider store = do
42+ evalStateT
43+ (runReaderT
44+ (unHakyll (hakyllWith rules provider store)) undefined) undefined
45+
46+data HakyllState = HakyllState
47+ { hakyllCompilers :: [(Identifier, Compiler () CompileRule)]
48+ }
49+
50+data HakyllEnvironment = HakyllEnvironment
51+ { hakyllRoute :: Route
52+ , hakyllResourceProvider :: ResourceProvider
53+ , hakyllStore :: Store
54+ }
55+
56+newtype Hakyll a = Hakyll
57+ { unHakyll :: ReaderT HakyllEnvironment (StateT HakyllState IO) a
58+ } deriving (Functor, Applicative, Monad)
59+
60+hakyllWith :: Rules -> ResourceProvider -> Store -> Hakyll ()
61+hakyllWith rules provider store = Hakyll $ do
4162 let -- Get the rule set
4263 ruleSet = runRules rules provider
4364
@@ -55,17 +76,19 @@ hakyllWith rules provider store = do
5576 -- Create the graph
5677 graph = fromList dependencies
5778
58− putStrLn "Writing dependency graph to dependencies.dot..."
59− writeDot "dependencies.dot" show graph
79+ liftIO $ do
80+ putStrLn "Writing dependency graph to dependencies.dot..."
81+ writeDot "dependencies.dot" show graph
6082
6183 -- Check which items are up-to-date
62− modified' <- modified provider store $ map fst compilers
84+ modified' <- liftIO $ modified provider store $ map fst compilers
6385
6486 let -- Try to reduce the graph
6587 reducedGraph = filterObsolete modified' graph
6688
67− putStrLn "Writing reduced graph to reduced.dot..."
68− writeDot "reduced.dot" show reducedGraph
89+ liftIO $ do
90+ putStrLn "Writing reduced graph to reduced.dot..."
91+ writeDot "reduced.dot" show reducedGraph
6992
7093 let -- Solve the graph
7194 ordered = solveDependencies reducedGraph
@@ -76,13 +99,10 @@ hakyllWith rules provider store = do
7699 -- Fetch the routes
77100 route' = rulesRoute ruleSet
78101
79− putStrLn $ show reducedGraph
80− putStrLn $ show ordered
81−
82102 -- Generate all the targets in order
83103 _ <- mapM (addTarget route' modified') orderedCompilers
84104
85− putStrLn "DONE."
105+ liftIO $ putStrLn "DONE."
86106 where
87107 addTarget route' modified' (id', comp) = do
88108 let url = runRoute route' id'
@@ -91,18 +111,19 @@ hakyllWith rules provider store = do
91111 let isModified = id' `S.member` modified'
92112
93113 -- Run the compiler
94− ItemRule compiled <- runCompiler comp id' provider url store isModified
95− putStrLn $ "Generated target: " ++ show id'
114+ ItemRule compiled <- liftIO $
115+ runCompiler comp id' provider url store isModified
116+ liftIO $ putStrLn $ "Generated target: " ++ show id'
96117
97118 case url of
98119 Nothing -> return ()
99− Just r -> do
120+ Just r -> liftIO $ do
100121 putStrLn $ "Routing " ++ show id' ++ " to " ++ r
101122 let path = "_site" </> r
102123 makeDirectories path
103124 write path compiled
104125
105− putStrLn ""
126+ liftIO $ putStrLn ""
106127
107128-- | Return a set of modified identifiers
108129--