@@ -5,12 +5,13 @@ module Text.Hakyll.CreateContext
5 ( createPage
5 ( createPage
6 , createCustomPage
6 , createCustomPage
7 , createListing
7 , createListing
9 , combineWithUrl
10 , combineWithUrl
12import qualified Data.Map as M
13import qualified Data.Map as M
13−import Control.Arrow (second)
14+import Control.Arrow (second, arr, (&&&))
14import Control.Monad (liftM2)
15import Control.Monad (liftM2)
15import Control.Applicative ((<$>))
16import Control.Applicative ((<$>))
16import Control.Arrow ((>>>))
17import Control.Arrow ((>>>))
@@ -27,13 +28,14 @@ import Text.Hakyll.Internal.Cache
27createPage :: FilePath -> HakyllAction () Context
28createPage :: FilePath -> HakyllAction () Context
28createPage path = cacheAction "pages" $ readPageAction path >>> renderAction
29createPage path = cacheAction "pages" $ readPageAction path >>> renderAction
30−-- | Create a "custom page" @Context@.
31+-- | Create a custom page @Context@.
32-- The association list given maps keys to values for substitution. Note
33-- The association list given maps keys to values for substitution. Note
33-- that as value, you can either give a @String@ or a
34-- that as value, you can either give a @String@ or a
34-- @HakyllAction () String@. The latter is preferred for more complex data,
35-- @HakyllAction () String@. The latter is preferred for more complex data,
35-- since it allows dependency checking. A @String@ is obviously more simple
36-- since it allows dependency checking. A @String@ is obviously more simple
36-- to use in some cases.
37-- to use in some cases.
37createCustomPage :: FilePath
39createCustomPage :: FilePath
38 -> [(String, Either String (HakyllAction () String))]
40 -> [(String, Either String (HakyllAction () String))]
39 -> HakyllAction () Context
41 -> HakyllAction () Context
@@ -67,6 +69,17 @@ createListing url templates renderables additional =
67 context = ("body", Right concatenation) : additional
69 context = ("body", Right concatenation) : additional
68 concatenation = renderAndConcat templates renderables
70 concatenation = renderAndConcat templates renderables
72+-- | Add a field to a 'Context'.
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)
80+ value' = arr (const ()) >>> either (arr . const) id value
81+ insert v = Context . M.insert key v . unContext
70-- | Combine two @Context@s. The url will always be taken from the first
83-- | Combine two @Context@s. The url will always be taken from the first
71-- @Renderable@. Also, if a `$key` is present in both renderables, the
84-- @Renderable@. Also, if a `$key` is present in both renderables, the
72-- value from the first @Context@ will be taken as well.
85-- value from the first @Context@ will be taken as well.