summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJasper Van der Jeugt <m@jaspervdj.be>2013-08-10 16:18:59 +0200
committerJasper Van der Jeugt <m@jaspervdj.be>2013-08-10 16:19:23 +0200
commitfad9134fd4957c4c38e0dd7e1263ea5bfc8af32c (patch)
tree2fcfece7acd2e1de4771b2319a344853fbc732dd
parentab0aebb3c3b0ceec565af673b0de6bf141585b83 (diff)
downloadhakyll-fad9134fd4957c4c38e0dd7e1263ea5bfc8af32c.tar.gz
Add functionField again
-rw-r--r--hakyll.cabal2
-rw-r--r--src/Hakyll/Web/Template/Context.hs10
-rw-r--r--src/Hakyll/Web/Template/Read.hs2
-rw-r--r--tests/Hakyll/Web/Template/Tests.hs1
-rw-r--r--tests/data/template.html2
-rw-r--r--tests/data/template.html.out2
6 files changed, 17 insertions, 2 deletions
diff --git a/hakyll.cabal b/hakyll.cabal
index e552bf3..b23763a 100644
--- a/hakyll.cabal
+++ b/hakyll.cabal
@@ -1,5 +1,5 @@
Name: hakyll
-Version: 4.3.2.0
+Version: 4.3.3.0
Synopsis: A static website compiler library
Description:
diff --git a/src/Hakyll/Web/Template/Context.hs b/src/Hakyll/Web/Template/Context.hs
index fe63c38..428e105 100644
--- a/src/Hakyll/Web/Template/Context.hs
+++ b/src/Hakyll/Web/Template/Context.hs
@@ -6,6 +6,7 @@ module Hakyll.Web.Template.Context
, field
, constField
, listField
+ , functionField
, mapContext
, defaultContext
@@ -87,6 +88,15 @@ 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
fld <- c k i
diff --git a/src/Hakyll/Web/Template/Read.hs b/src/Hakyll/Web/Template/Read.hs
index bb5c8c2..2421b2d 100644
--- a/src/Hakyll/Web/Template/Read.hs
+++ b/src/Hakyll/Web/Template/Read.hs
@@ -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
--------------------------------------------------------------------------------
diff --git a/tests/Hakyll/Web/Template/Tests.hs b/tests/Hakyll/Web/Template/Tests.hs
index 1d80a06..8763147 100644
--- a/tests/Hakyll/Web/Template/Tests.hs
+++ b/tests/Hakyll/Web/Template/Tests.hs
@@ -58,6 +58,7 @@ testContext = mconcat
n1 <- makeItem "Jan"
n2 <- makeItem "Piet"
return [n1, n2]
+ , functionField "rev" $ \args _ -> return $ unwords $ map reverse args
]
where
diff --git a/tests/data/template.html b/tests/data/template.html
index 22e5ddd..26f9e8a 100644
--- a/tests/data/template.html
+++ b/tests/data/template.html
@@ -1,6 +1,8 @@
<div>
I'm so rich I have $$3.
+ $rev foo$
+
$if(body)$
I have body
$else$
diff --git a/tests/data/template.html.out b/tests/data/template.html.out
index 8047b0d..0b17d31 100644
--- a/tests/data/template.html.out
+++ b/tests/data/template.html.out
@@ -1,6 +1,8 @@
<div>
I'm so rich I have $3.
+ oof
+
I have body