summaryrefslogtreecommitdiff
path: root/src/Hakyll/Web/Template.hs
diff options
context:
space:
mode:
authorJasper Van der Jeugt <m@jaspervdj.be>2013-05-04 11:14:35 +0200
committerJasper Van der Jeugt <m@jaspervdj.be>2013-05-04 11:14:35 +0200
commit28bc3f1f3b98f3bf4c8601af8eb8fa7a9c226ed2 (patch)
treee39e82490c3ad607025a5f757e0183b0b8f71d4d /src/Hakyll/Web/Template.hs
parent35e2db23399d7604f5440230165fb670a97f568b (diff)
parent7d489f314d553019c04905a912bc27448b4ec241 (diff)
downloadhakyll-28bc3f1f3b98f3bf4c8601af8eb8fa7a9c226ed2.tar.gz
Merge remote-tracking branch 'sphynx/master'
Diffstat (limited to 'src/Hakyll/Web/Template.hs')
-rw-r--r--src/Hakyll/Web/Template.hs21
1 files changed, 14 insertions, 7 deletions
diff --git a/src/Hakyll/Web/Template.hs b/src/Hakyll/Web/Template.hs
index 07a8ff3..371ccef 100644
--- a/src/Hakyll/Web/Template.hs
+++ b/src/Hakyll/Web/Template.hs
@@ -44,7 +44,8 @@ module Hakyll.Web.Template
--------------------------------------------------------------------------------
-import Control.Monad (forM, liftM)
+import Control.Monad (liftM)
+import Control.Monad.Error (MonadError(..))
import Data.Monoid (mappend)
import Prelude hiding (id)
@@ -112,11 +113,17 @@ applyAsTemplate context item =
--------------------------------------------------------------------------------
-- | Overloaded apply template function to work in an arbitrary Monad.
-applyTemplateWith :: Monad m
+applyTemplateWith :: MonadError e m
=> (String -> a -> m String)
-> Template -> a -> m String
-applyTemplateWith context tpl x = liftM concat $
- forM (unTemplate tpl) $ \e -> case e of
- Chunk c -> return c
- Escaped -> return "$"
- Key k -> context k x
+applyTemplateWith context tpl x = go tpl where
+
+ go = liftM concat . mapM applyElem . unTemplate
+
+ applyElem (Chunk c) = return c
+ applyElem Escaped = return "$"
+ applyElem (Key k) = context k x
+ applyElem (If k t mf) = (context k x >> go t) `catchError` handler where
+ handler _ = case mf of
+ Nothing -> return ""
+ Just f -> go f