on master help sign in
Add functionField again
commitfad9134fd4957c4c38e0dd7e1263ea5bfc8af32c authorJasper Van der Jeugt <m@jaspervdj.be> dateSat 10 Aug 2013, 16:18 (13 years ago) parentab0aebb
6 files changed+17−2
hakyll.cabal+1−1 src/Hakyll/Web/Template/Context.hs+10 src/Hakyll/Web/Template/Read.hs+1−1 tests/Hakyll/Web/Template/Tests.hs+1 tests/data/template.html+2 tests/data/template.html.out+2
hakyll.cabal+1−1
@@ -1,5 +1,5 @@
11Name: hakyll
2−Version: 4.3.2.0
2+Version: 4.3.3.0
33
44Synopsis: A static website compiler library
55Description:
src/Hakyll/Web/Template/Context.hs+10
@@ -6,6 +6,7 @@ module Hakyll.Web.Template.Context
66 , field
77 , constField
88 , listField
9+ , functionField
910 , mapContext
1011
1112 , defaultContext
@@ -86,6 +87,15 @@ listField :: String -> Context a -> Compiler [Item a] -> Context b
8687listField key c xs = field' key $ \_ -> fmap (ListField c) xs
8788
8889
90+--------------------------------------------------------------------------------
91+functionField :: String -> ([String] -> Item a -> Compiler String) -> Context a
92+functionField name value = Context $ \k i -> case words k of
93+ [] -> empty
94+ (n : args)
95+ | n == name -> StringField <$> value args i
96+ | otherwise -> empty
97+
98+
8999--------------------------------------------------------------------------------
90100mapContext :: (String -> String) -> Context a -> Context a
91101mapContext f (Context c) = Context $ \k i -> do
src/Hakyll/Web/Template/Read.hs+1−1
@@ -21,7 +21,7 @@ import Hakyll.Web.Template.Internal
2121readTemplate :: String -> Template
2222readTemplate input = case parse template "" input of
2323 Left err -> error $ "Cannot parse template: " ++ show err
24− Right t -> t
24+ Right t -> t
2525
2626
2727--------------------------------------------------------------------------------
tests/Hakyll/Web/Template/Tests.hs+1
@@ -58,6 +58,7 @@ testContext = mconcat
5858 n1 <- makeItem "Jan"
5959 n2 <- makeItem "Piet"
6060 return [n1, n2]
61+ , functionField "rev" $ \args _ -> return $ unwords $ map reverse args
6162 ]
6263 where
6364
tests/data/template.html+2
@@ -1,6 +1,8 @@
11<div>
22 I'm so rich I have $$3.
33
4+ $rev foo$
5+
46 $if(body)$
57 I have body
68 $else$
tests/data/template.html.out+2
@@ -1,6 +1,8 @@
11<div>
22 I'm so rich I have $3.
33
4+ oof
5+
46
57 I have body
68