on master help sign in
Added `bindItem`
eff77caiko
lib/Hakyll/Web/Template/Context.hs+9
@@ -1,6 +1,7 @@
1--------------------------------------------------------------------------------
1--------------------------------------------------------------------------------
2{-# LANGUAGE CPP #-}
2{-# LANGUAGE CPP #-}
3{-# LANGUAGE ExistentialQuantification #-}
3{-# LANGUAGE ExistentialQuantification #-}
4+{-# LANGUAGE RankNTypes #-}
4module Hakyll.Web.Template.Context
5module Hakyll.Web.Template.Context
5 ( ContextField (..)
6 ( ContextField (..)
6 , Context (..)
7 , Context (..)
@@ -28,6 +29,7 @@ module Hakyll.Web.Template.Context
28 , teaserField
29 , teaserField
29 , teaserFieldWithSeparator
30 , teaserFieldWithSeparator
30 , missingField
31 , missingField
32+ , bindItem
31 ) where
33 ) where
32
34
33
35
@@ -396,3 +398,10 @@ parseTimeM = TF.parseTimeM
396#else
398#else
397parseTimeM _ = TF.parseTime
399parseTimeM _ = TF.parseTime
398#endif
400#endif
401+
402+--------------------------------------------------------------------------------
403+
404+-- | Binds an 'Item' to a given 'Context', allowing it to be combined with any
405+-- other 'Context' of any type.
406+bindItem :: Context a -> Item a -> forall b. Context b
407+bindItem (Context ctx) ia = Context $ \k args _ -> ctx k args ia
lib/Hakyll/Web/Template/Internal.hs+2−7
@@ -18,7 +18,7 @@ module Hakyll.Web.Template.Internal
18
18
19
19
20--------------------------------------------------------------------------------
20--------------------------------------------------------------------------------
21−import Control.Applicative ((<|>))
21+import Control.Monad (forM)
22import Control.Monad.Except (MonadError (..))
22import Control.Monad.Except (MonadError (..))
23import Data.Binary (Binary)
23import Data.Binary (Binary)
24import Data.List (intercalate)
24import Data.List (intercalate)
@@ -135,13 +135,8 @@ applyTemplate' tes context x = go tes
135 "got StringField for expr " ++ show e
135 "got StringField for expr " ++ show e
136 ListField c xs -> do
136 ListField c xs -> do
137 sep <- maybe (return "") go s
137 sep <- maybe (return "") go s
138− bs <- mapM (applyTemplate' b $ combineContexts context x c) xs
138+ bs <- forM xs $ applyTemplate' b $ c <> bindItem context x
139 return $ intercalate sep bs
139 return $ intercalate sep bs
140− where
141− combineContexts :: Context a -> Item a -> Context b -> Context b
142− combineContexts ca ia cb = Context $ \ k' args' ib' ->
143− unContext cb k' args' ib'
144− <|> unContext ca k' args' ia
145
140
146 applyElem (Partial e) = do
141 applyElem (Partial e) = do
147 p <- applyExpr e >>= getString e
142 p <- applyExpr e >>= getString e