on master help sign in
Added `bindItem`
commiteff77cae4036cf0cb2a9340c18b5ecb5b33b6997 authoriko <ilyakooo0@gmail.com> dateThu 29 Aug 2019, 14:14 (7 years ago) parent8ae8f52
2 files changed+11−7
lib/Hakyll/Web/Template/Context.hs+9 lib/Hakyll/Web/Template/Internal.hs+2−7
lib/Hakyll/Web/Template/Context.hs+9
@@ -1,6 +1,7 @@
11--------------------------------------------------------------------------------
22{-# LANGUAGE CPP #-}
33{-# LANGUAGE ExistentialQuantification #-}
4+{-# LANGUAGE RankNTypes #-}
45module Hakyll.Web.Template.Context
56 ( ContextField (..)
67 , Context (..)
@@ -28,6 +29,7 @@ module Hakyll.Web.Template.Context
2829 , teaserField
2930 , teaserFieldWithSeparator
3031 , missingField
32+ , bindItem
3133 ) where
3234
3335
@@ -396,3 +398,10 @@ parseTimeM = TF.parseTimeM
396398#else
397399parseTimeM _ = TF.parseTime
398400#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
1818
1919
2020--------------------------------------------------------------------------------
21−import Control.Applicative ((<|>))
21+import Control.Monad (forM)
2222import Control.Monad.Except (MonadError (..))
2323import Data.Binary (Binary)
2424import Data.List (intercalate)
@@ -135,13 +135,8 @@ applyTemplate' tes context x = go tes
135135 "got StringField for expr " ++ show e
136136 ListField c xs -> do
137137 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
139139 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
145140
146141 applyElem (Partial e) = do
147142 p <- applyExpr e >>= getString e