Add functionField again

This commit is contained in:
Jasper Van der Jeugt 2013-08-10 16:18:59 +02:00
parent ab0aebb3c3
commit fad9134fd4
6 changed files with 17 additions and 2 deletions

View file

@ -1,5 +1,5 @@
Name: hakyll
Version: 4.3.2.0
Version: 4.3.3.0
Synopsis: A static website compiler library
Description:

View file

@ -6,6 +6,7 @@ module Hakyll.Web.Template.Context
, field
, constField
, listField
, functionField
, mapContext
, defaultContext
@ -86,6 +87,15 @@ listField :: String -> Context a -> Compiler [Item a] -> Context b
listField key c xs = field' key $ \_ -> fmap (ListField c) xs
--------------------------------------------------------------------------------
functionField :: String -> ([String] -> Item a -> Compiler String) -> Context a
functionField name value = Context $ \k i -> case words k of
[] -> empty
(n : args)
| n == name -> StringField <$> value args i
| otherwise -> empty
--------------------------------------------------------------------------------
mapContext :: (String -> String) -> Context a -> Context a
mapContext f (Context c) = Context $ \k i -> do

View file

@ -21,7 +21,7 @@ import Hakyll.Web.Template.Internal
readTemplate :: String -> Template
readTemplate input = case parse template "" input of
Left err -> error $ "Cannot parse template: " ++ show err
Right t -> t
Right t -> t
--------------------------------------------------------------------------------

View file

@ -58,6 +58,7 @@ testContext = mconcat
n1 <- makeItem "Jan"
n2 <- makeItem "Piet"
return [n1, n2]
, functionField "rev" $ \args _ -> return $ unwords $ map reverse args
]
where

View file

@ -1,6 +1,8 @@
<div>
I'm so rich I have $$3.
$rev foo$
$if(body)$
I have body
$else$

View file

@ -1,6 +1,8 @@
<div>
I'm so rich I have $3.
oof
I have body