summaryrefslogtreecommitdiff
path: root/src/Hakyll/Web
diff options
context:
space:
mode:
authorJasper Van der Jeugt <m@jaspervdj.be>2011-11-23 15:24:20 +0100
committerJasper Van der Jeugt <m@jaspervdj.be>2011-11-23 15:24:20 +0100
commitd61d7f19fc636f97e60f76b1ab0bd7a249cd96c3 (patch)
treecf2541ca23fd98852bd7d4d075eb53bef4dd95fe /src/Hakyll/Web
parente1687cbb300323e5975413caaf813e07939e89dc (diff)
downloadhakyll-d61d7f19fc636f97e60f76b1ab0bd7a249cd96c3.tar.gz
"Allow" missing keys in templates
Diffstat (limited to 'src/Hakyll/Web')
-rw-r--r--src/Hakyll/Web/Template.hs35
1 files changed, 29 insertions, 6 deletions
diff --git a/src/Hakyll/Web/Template.hs b/src/Hakyll/Web/Template.hs
index 222ab23..c5c7ff8 100644
--- a/src/Hakyll/Web/Template.hs
+++ b/src/Hakyll/Web/Template.hs
@@ -44,10 +44,12 @@
module Hakyll.Web.Template
( Template
, applyTemplate
+ , applyTemplateWith
, applySelf
, templateCompiler
, templateCompilerWith
, applyTemplateCompiler
+ , applyTemplateCompilerWith
) where
import Control.Arrow
@@ -65,17 +67,29 @@ import Hakyll.Web.Template.Read
import Hakyll.Web.Page.Internal
-- | Substitutes @$identifiers@ in the given @Template@ by values from the given
--- "Page". When a key is not found, it is left as it is. You can specify
--- the characters used to replace escaped dollars (@$$@) here.
+-- "Page". When a key is not found, it is left as it is.
--
applyTemplate :: Template -> Page String -> Page String
-applyTemplate template page =
+applyTemplate = applyTemplateWith defaultMissingHandler
+
+-- | Default solution if a key is missing: render it again
+defaultMissingHandler :: String -> String
+defaultMissingHandler k = "$" ++ k ++ "$"
+
+-- | A version of 'applyTemplate' which allows you to give a fallback option,
+-- which can produce the value for a key if it is missing.
+--
+applyTemplateWith :: (String -> String) -- ^ Fallback if key missing
+ -> Template -- ^ Template to apply
+ -> Page String -- ^ Input page
+ -> Page String -- ^ Resulting page
+applyTemplateWith missing template page =
fmap (const $ substitute =<< unTemplate template) page
where
map' = toMap page
substitute (Chunk chunk) = chunk
- substitute (Key key) = fromMaybe ("$" ++ key ++ "$") $ M.lookup key map'
- substitute (Escaped) = "$"
+ substitute (Key key) = fromMaybe (missing key) $ M.lookup key map'
+ substitute (Escaped) = "$"
-- | Apply a page as it's own template. This is often very useful to fill in
-- certain keys like @$root@ and @$url@.
@@ -106,4 +120,13 @@ templateCompilerWith settings =
applyTemplateCompiler :: Identifier Template -- ^ Template
-> Compiler (Page String) (Page String) -- ^ Compiler
-applyTemplateCompiler identifier = require identifier (flip applyTemplate)
+applyTemplateCompiler = applyTemplateCompilerWith defaultMissingHandler
+
+-- | A version of 'applyTemplateCompiler' which allows you to pass a function
+-- which is called for a key when it is missing.
+--
+applyTemplateCompilerWith :: (String -> String)
+ -> Identifier Template
+ -> Compiler (Page String) (Page String)
+applyTemplateCompilerWith missing identifier =
+ require identifier (flip $ applyTemplateWith missing)