@@ -5,12 +5,13 @@ module Text.Hakyll.CreateContext
55 ( createPage
66 , createCustomPage
77 , createListing
8+ , addField
89 , combine
910 , combineWithUrl
1011 ) where
1112
1213import qualified Data.Map as M
13−import Control.Arrow (second)
14+import Control.Arrow (second, arr, (&&&))
1415import Control.Monad (liftM2)
1516import Control.Applicative ((<$>))
1617import Control.Arrow ((>>>))
@@ -27,13 +28,14 @@ import Text.Hakyll.Internal.Cache
2728createPage :: FilePath -> HakyllAction () Context
2829createPage path = cacheAction "pages" $ readPageAction path >>> renderAction
2930
30−-- | Create a "custom page" @Context@.
31+-- | Create a custom page @Context@.
3132--
3233-- The association list given maps keys to values for substitution. Note
3334-- that as value, you can either give a @String@ or a
3435-- @HakyllAction () String@. The latter is preferred for more complex data,
3536-- since it allows dependency checking. A @String@ is obviously more simple
3637-- to use in some cases.
38+--
3739createCustomPage :: FilePath
3840 -> [(String, Either String (HakyllAction () String))]
3941 -> HakyllAction () Context
@@ -67,6 +69,17 @@ createListing url templates renderables additional =
6769 context = ("body", Right concatenation) : additional
6870 concatenation = renderAndConcat templates renderables
6971
72+-- | Add a field to a 'Context'.
73+--
74+addField :: String -- ^ Key
75+ -> Either String (HakyllAction () String) -- ^ Value
76+ -> HakyllAction a Context -- ^ Target
77+ -> HakyllAction a Context -- ^ Result
78+addField key value target = value' &&& target >>> arr (uncurry insert)
79+ where
80+ value' = arr (const ()) >>> either (arr . const) id value
81+ insert v = Context . M.insert key v . unContext
82+
7083-- | Combine two @Context@s. The url will always be taken from the first
7184-- @Renderable@. Also, if a `$key` is present in both renderables, the
7285-- value from the first @Context@ will be taken as well.