summaryrefslogtreecommitdiff
path: root/src/Text/Hakyll/HakyllMonad.hs
blob: fbfe5aeb79ae20d7a5ddb094980b41a637ad5638 (plain)
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
-- | Module describing the Hakyll monad stack.
module Text.Hakyll.HakyllMonad
    ( HakyllConfiguration (..)
    , Hakyll
    , askHakyll
    , getAdditionalContext
    ) where

import Control.Monad.Reader (ReaderT, ask)
import Control.Monad (liftM)
import qualified Data.Map as M

import Text.Pandoc (ParserState, WriterOptions)

import Text.Hakyll.Context (Context)

-- | Our custom monad stack.
type Hakyll = ReaderT HakyllConfiguration IO

-- | Hakyll global configuration type.
data HakyllConfiguration = HakyllConfiguration
    { -- | Absolute URL of the site.
      absoluteUrl         :: String
    , -- | An additional context to use when rendering. This additional context
      --   is used globally.
      additionalContext   :: Context
    , -- | Directory where the site is placed.
      siteDirectory       :: FilePath
    , -- | Directory for cache files.
      cacheDirectory      :: FilePath
    , -- | Enable index links.
      enableIndexUrl      :: Bool
    , -- | Delay between polls in preview mode.
      previewPollDelay    :: Int
    , -- | Pandoc parsing options
      pandocParserState   :: ParserState
    , -- | Pandoc writer options
      pandocWriterOptions :: WriterOptions
    }

-- | Simplified @ask@ function for the Hakyll monad stack.
--
--   Usage would typically be something like:
--
--   > doSomething :: a -> b -> Hakyll c
--   > doSomething arg1 arg2 = do
--   >     siteDirectory' <- askHakyll siteDirectory
--   >     ...
--
askHakyll :: (HakyllConfiguration -> a) -> Hakyll a
askHakyll = flip liftM ask

getAdditionalContext :: HakyllConfiguration -> Context
getAdditionalContext configuration =
    M.insert "absolute" (absoluteUrl configuration)
             (additionalContext configuration)