diff options
author | Jasper Van der Jeugt <m@jaspervdj.be> | 2013-05-04 11:14:35 +0200 |
---|---|---|
committer | Jasper Van der Jeugt <m@jaspervdj.be> | 2013-05-04 11:14:35 +0200 |
commit | 28bc3f1f3b98f3bf4c8601af8eb8fa7a9c226ed2 (patch) | |
tree | e39e82490c3ad607025a5f757e0183b0b8f71d4d /src/Hakyll/Web/Template.hs | |
parent | 35e2db23399d7604f5440230165fb670a97f568b (diff) | |
parent | 7d489f314d553019c04905a912bc27448b4ec241 (diff) | |
download | hakyll-28bc3f1f3b98f3bf4c8601af8eb8fa7a9c226ed2.tar.gz |
Merge remote-tracking branch 'sphynx/master'
Diffstat (limited to 'src/Hakyll/Web/Template.hs')
-rw-r--r-- | src/Hakyll/Web/Template.hs | 21 |
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 |