@@ -1,5 +1,6 @@
1-- | Module describing the Hakyll monad stack.
1-- | Module describing the Hakyll monad stack.
2+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
2−module Text.Hakyll.HakyllMonad
3+module Text.Hakyll.Monad
3 ( HakyllConfiguration (..)
4 ( HakyllConfiguration (..)
4 , PreviewMode (..)
5 , PreviewMode (..)
@@ -11,6 +12,7 @@ module Text.Hakyll.HakyllMonad
13import Control.Monad.Trans (liftIO)
14import Control.Monad.Trans (liftIO)
15+import Control.Monad.IO.Class (MonadIO, liftIO)
14import Control.Concurrent.MVar (MVar, putMVar, newEmptyMVar, readMVar)
16import Control.Concurrent.MVar (MVar, putMVar, newEmptyMVar, readMVar)
15import Control.Monad.Reader (ReaderT, ask, runReaderT)
17import Control.Monad.Reader (ReaderT, ask, runReaderT)
16import Control.Monad (liftM, forM, forM_)
18import Control.Monad (liftM, forM, forM_)
@@ -24,7 +26,16 @@ import Text.Hakyll.Context (Context (..))
25-- | Our custom monad stack.
27-- | Our custom monad stack.
27−type Hakyll = ReaderT HakyllConfiguration IO
29+newtype Hakyll a = Hakyll (ReaderT HakyllConfiguration IO a)
30+ deriving (Monad, Functor)
32+instance MonadIO Hakyll where
33+ liftIO = Hakyll . liftIO
35+-- | Run a hakyll stack
37+runHakyll :: Hakyll a -> HakyllConfiguration -> IO a
38+runHakyll (Hakyll h) = runReaderT h
29-- | Preview mode.
40-- | Preview mode.
@@ -56,6 +67,11 @@ data HakyllConfiguration = HakyllConfiguration
56 hamletSettings :: HamletSettings
67 hamletSettings :: HamletSettings
70+-- | Get the hakyll configuration
72+getHakyllConfiguration :: Hakyll HakyllConfiguration
73+getHakyllConfiguration = Hakyll ask
59-- | Simplified @ask@ function for the Hakyll monad stack.
75-- | Simplified @ask@ function for the Hakyll monad stack.
61-- Usage would typically be something like:
77-- Usage would typically be something like:
@@ -66,7 +82,7 @@ data HakyllConfiguration = HakyllConfiguration
68askHakyll :: (HakyllConfiguration -> a) -> Hakyll a
84askHakyll :: (HakyllConfiguration -> a) -> Hakyll a
69−askHakyll = flip liftM ask
85+askHakyll = flip liftM getHakyllConfiguration
71-- | Obtain the globally available, additional context.
87-- | Obtain the globally available, additional context.
@@ -78,16 +94,16 @@ getAdditionalContext configuration =
78-- | Write some log information.
94-- | Write some log information.
80logHakyll :: String -> Hakyll ()
96logHakyll :: String -> Hakyll ()
81−logHakyll = liftIO . hPutStrLn stderr
97+logHakyll = Hakyll . liftIO . hPutStrLn stderr
83-- | Perform a concurrent hakyll action. Returns an MVar you can wait on
99-- | Perform a concurrent hakyll action. Returns an MVar you can wait on
85forkHakyllWait :: Hakyll () -> Hakyll (MVar ())
101forkHakyllWait :: Hakyll () -> Hakyll (MVar ())
86forkHakyllWait action = do
102forkHakyllWait action = do
87 mvar <- liftIO newEmptyMVar
103 mvar <- liftIO newEmptyMVar
88− config <- ask
104+ config <- getHakyllConfiguration
89 liftIO $ do
105 liftIO $ do
90− runReaderT action config
106+ runHakyll action config
91 putMVar mvar ()
107 putMVar mvar ()
92 return mvar
108 return mvar