@@ -1,27 +1,6 @@
1−-- | This module provides 'Context's which are used to expand expressions in
2−-- templates and allow for arbitrary customisation.
4−-- 'Template's define a small expression DSL which consists of strings,
5−-- identifiers and function application. There is no type system, every value is
6−-- a string and on the top level they get substituted verbatim into the page.
8−-- For example, you can build a context that contains
10−-- > … <> functionField "concat" (const . concat) <> …
12−-- which will allow you to use the @concat@ identifier as a function that takes
13−-- arbitrarily many stings and concatenates them to a new string:
15−-- > $partial(concat("templates/categories/", category))$
17−-- This will evaluate the @category@ field in the context, then prepend he path,
18−-- and include the referenced file as a template.
21--------------------------------------------------------------------------------
1--------------------------------------------------------------------------------
22{-# LANGUAGE CPP #-}
2{-# LANGUAGE CPP #-}
23{-# LANGUAGE ExistentialQuantification #-}
3{-# LANGUAGE ExistentialQuantification #-}
24−{-# LANGUAGE RankNTypes #-}
25module Hakyll.Web.Template.Context
4module Hakyll.Web.Template.Context
26 ( ContextField (..)
5 ( ContextField (..)
27 , Context (..)
6 , Context (..)
@@ -49,7 +28,6 @@ module Hakyll.Web.Template.Context
49 , teaserField
28 , teaserField
50 , teaserFieldWithSeparator
29 , teaserFieldWithSeparator
51 , missingField
30 , missingField
@@ -72,16 +50,13 @@ import Hakyll.Core.Metadata
72import Hakyll.Core.Provider
50import Hakyll.Core.Provider
73import Hakyll.Core.Util.String (needlePrefix, splitAll)
51import Hakyll.Core.Util.String (needlePrefix, splitAll)
74import Hakyll.Web.Html
52import Hakyll.Web.Html
75−import Prelude hiding (id)
76−import System.FilePath (dropExtension, splitDirectories,
53+import System.FilePath (splitDirectories, takeBaseName, dropExtension)
80--------------------------------------------------------------------------------
56--------------------------------------------------------------------------------
81-- | Mostly for internal usage
57-- | Mostly for internal usage
82data ContextField
58data ContextField
84− | StringField String
59+ = StringField String
85 | forall a. ListField (Context a) [Item a]
60 | forall a. ListField (Context a) [Item a]
@@ -106,8 +81,6 @@ newtype Context a = Context
108--------------------------------------------------------------------------------
83--------------------------------------------------------------------------------
109−-- | Tries to find a key in the left context,
110−-- or when that fails in the right context.
111#if MIN_VERSION_base(4,9,0)
84#if MIN_VERSION_base(4,9,0)
112instance Semigroup (Context a) where
85instance Semigroup (Context a) where
113 (<>) (Context f) (Context g) = Context $ \k a i -> f k a i <|> g k a i
86 (<>) (Context f) (Context g) = Context $ \k a i -> f k a i <|> g k a i
@@ -124,105 +97,64 @@ instance Monoid (Context a) where
125--------------------------------------------------------------------------------
98--------------------------------------------------------------------------------
126field' :: String -> (Item a -> Compiler ContextField) -> Context a
99field' :: String -> (Item a -> Compiler ContextField) -> Context a
127−field' key value = Context $ \k _ i ->
100+field' key value = Context $ \k _ i -> if k == key then value i else empty
130− else noResult $ "Tried field " ++ key
133--------------------------------------------------------------------------------
103--------------------------------------------------------------------------------
134−-- | Constructs a new field for a 'Context'.
104+-- | Constructs a new field in the 'Context.'
135−-- If the key matches, the compiler is run and its result is substituted in the
138−-- If the compiler fails, the field will be considered non-existent
139−-- in an @$if()$@ macro or ultimately break the template application
140−-- (unless the key is found in another context when using '<>').
141−-- Use 'empty' or 'noResult' for intentional failures of fields used in
142−-- @$if()$@, to distinguish them from exceptions thrown with 'fail'.
144 :: String -- ^ Key
106 :: String -- ^ Key
145 -> (Item a -> Compiler String) -- ^ Function that constructs a value based
107 -> (Item a -> Compiler String) -- ^ Function that constructs a value based
146− -- on the item (e.g. accessing metadata)
108+ -- on the item
147 -> Context a
109 -> Context a
148field key value = field' key (fmap StringField . value)
110field key value = field' key (fmap StringField . value)
151--------------------------------------------------------------------------------
113--------------------------------------------------------------------------------
152-- | Creates a 'field' to use with the @$if()$@ template macro.
114-- | Creates a 'field' to use with the @$if()$@ template macro.
153−-- Attempting to substitute the field into the template will cause an error.
155 :: String
116 :: String
156 -> (Item a -> Bool)
117 -> (Item a -> Bool)
157 -> Context a
118 -> Context a
158−boolField name f = field' name (\i -> if f i
119+boolField name f = field name (\i -> if f i
159− then return EmptyField
120+ then pure (error $ unwords ["no string value for bool field:",name])
160− else noResult $ "Field " ++ name ++ " is false")
121+ else empty)
163--------------------------------------------------------------------------------
124--------------------------------------------------------------------------------
164−-- | Creates a 'field' that does not depend on the 'Item' but always yields
125+-- | Creates a 'field' that does not depend on the 'Item'
166−constField :: String -- ^ Key
126+constField :: String -> String -> Context a
167− -> String -- ^ Value
169constField key = field key . const . return
127constField key = field key . const . return
172--------------------------------------------------------------------------------
130--------------------------------------------------------------------------------
173−-- | Creates a list field to be consumed by a @$for(…)$@ expression.
174−-- The compiler returns multiple items which are rendered in the loop body
175−-- with the supplied context.
176listField :: String -> Context a -> Compiler [Item a] -> Context b
131listField :: String -> Context a -> Compiler [Item a] -> Context b
177listField key c xs = listFieldWith key c (const xs)
132listField key c xs = listFieldWith key c (const xs)
180--------------------------------------------------------------------------------
135--------------------------------------------------------------------------------
181−-- | Produces a new 'Context' which has list field 'key'. All fields from
182−-- 'c' are also accessible from the produced context.
183−-- Be careful when doing @listFieldWith k ca f <> cb@ as any fields in @ca@
184−-- will override fields in @cb@ with the same name.
185−-- Creates a list field like 'listField', but supplies the current page
187listFieldWith
136listFieldWith
188 :: String -> Context a -> (Item b -> Compiler [Item a]) -> Context b
137 :: String -> Context a -> (Item b -> Compiler [Item a]) -> Context b
189listFieldWith key c f = field' key $ fmap (ListField c) . f
138listFieldWith key c f = field' key $ fmap (ListField c) . f
192--------------------------------------------------------------------------------
141--------------------------------------------------------------------------------
193−-- | Creates a variadic function field.
195−-- The function will be called with the dynamically evaluated string arguments
196−-- from the template as well as the page that is currently rendered.
197−functionField :: String -- ^ Key
198− -> ([String] -> Item a -> Compiler String) -- ^ Function
142+functionField :: String -> ([String] -> Item a -> Compiler String) -> Context a
200functionField name value = Context $ \k args i ->
143functionField name value = Context $ \k args i ->
201 if k == name
144 if k == name
202 then StringField <$> value args i
145 then StringField <$> value args i
203− else noResult $ "Tried function field " ++ name
146+ else empty
206--------------------------------------------------------------------------------
149--------------------------------------------------------------------------------
207−-- | Transform the respective string results of all fields in a context.
210−-- > mapContext (++"c") (constField "x" "a" <> constField "y" "b")
214−-- > constField "x" "ac" <> constField "y" "bc"
216mapContext :: (String -> String) -> Context a -> Context a
150mapContext :: (String -> String) -> Context a -> Context a
217mapContext f (Context c) = Context $ \k a i -> do
151mapContext f (Context c) = Context $ \k a i -> do
218 fld <- c k a i
152 fld <- c k a i
219 case fld of
153 case fld of
220− EmptyField -> wrongType "boolField"
221 StringField str -> return $ StringField (f str)
154 StringField str -> return $ StringField (f str)
222− _ -> wrongType "ListField"
155+ ListField _ _ -> fail $
224− wrongType typ = fail $ "Hakyll.Web.Template.Context.mapContext: " ++
156+ "Hakyll.Web.Template.Context.mapContext: " ++
225− "can't map over a " ++ typ ++ "!"
157+ "can't map over a ListField!"
227--------------------------------------------------------------------------------
159--------------------------------------------------------------------------------
228-- | A context that allows snippet inclusion. In processed file, use as:
160-- | A context that allows snippet inclusion. In processed file, use as:
@@ -231,15 +163,15 @@ mapContext f (Context c) = Context $ \k a i -> do
231-- > $snippet("path/to/snippet/")$
163-- > $snippet("path/to/snippet/")$
234−-- The contents of the included file will not be interpolated like @partial@
166+-- The contents of the included file will not be interpolated.
237snippetField :: Context String
168snippetField :: Context String
238snippetField = functionField "snippet" f
169snippetField = functionField "snippet" f
240 f [contentsPath] _ = loadBody (fromFilePath contentsPath)
171 f [contentsPath] _ = loadBody (fromFilePath contentsPath)
241− f [] _ = fail "No argument to function 'snippet()'"
172+ f _ i = error $
242− f _ _ = fail "Too many arguments to function 'snippet()'"
173+ "Too many arguments to function 'snippet()' in item " ++
174+ show (itemIdentifier i)
244--------------------------------------------------------------------------------
176--------------------------------------------------------------------------------
245-- | A context that contains (in that order)
177-- | A context that contains (in that order)
@@ -259,7 +191,8 @@ defaultContext =
259 metadataField `mappend`
191 metadataField `mappend`
260 urlField "url" `mappend`
192 urlField "url" `mappend`
261 pathField "path" `mappend`
193 pathField "path" `mappend`
262− titleField "title"
194+ titleField "title" `mappend`
265--------------------------------------------------------------------------------
198--------------------------------------------------------------------------------
@@ -277,20 +210,15 @@ bodyField key = field key $ return . itemBody
277-- | Map any field to its metadata value, if present
210-- | Map any field to its metadata value, if present
278metadataField :: Context a
211metadataField :: Context a
279metadataField = Context $ \k _ i -> do
212metadataField = Context $ \k _ i -> do
280− let id = itemIdentifier i
281− empty' = noResult $ "No '" ++ k ++ "' field in metadata " ++
282− "of item " ++ show id
283− value <- getMetadataField id k
213+ value <- getMetadataField (itemIdentifier i) k
284− maybe empty' (return . StringField) value
214+ maybe empty (return . StringField) value
287--------------------------------------------------------------------------------
217--------------------------------------------------------------------------------
288-- | Absolute url to the resulting item
218-- | Absolute url to the resulting item
289urlField :: String -> Context a
219urlField :: String -> Context a
290−urlField key = field key $ \i -> do
220+urlField key = field key $
291− let id = itemIdentifier i
292− empty' = fail $ "No route url found for item " ++ show id
293− fmap (maybe empty' toUrl) $ getRoute id
221+ fmap (maybe empty toUrl) . getRoute . itemIdentifier
296--------------------------------------------------------------------------------
224--------------------------------------------------------------------------------
@@ -344,8 +272,8 @@ titleField = mapContext takeBaseName . pathField
345-- As another alternative, if none of the above matches, and the file has a
273-- As another alternative, if none of the above matches, and the file has a
346-- path which contains nested directories specifying a date, then that date
274-- path which contains nested directories specifying a date, then that date
347−-- will be used. In other words, if the path is of the form
275+-- will be used. In other words, if the path is of the form
348−-- @**//yyyy//mm//dd//**//main.extension@ .
276+-- @**//yyyy//mm//dd//**//main.extension@ .
349-- As above, in case of multiple matches, the rightmost one is used.
277-- As above, in case of multiple matches, the rightmost one is used.
351dateField :: String -- ^ Key in which the rendered date should be placed
279dateField :: String -- ^ Key in which the rendered date should be placed
@@ -357,7 +285,7 @@ dateField = dateFieldWith defaultTimeLocale
357--------------------------------------------------------------------------------
285--------------------------------------------------------------------------------
358-- | This is an extended version of 'dateField' that allows you to
286-- | This is an extended version of 'dateField' that allows you to
359-- specify a time locale that is used for outputting the date. For more
287-- specify a time locale that is used for outputting the date. For more
360−-- details, see 'dateField' and 'formatTime'.
288+-- details, see 'dateField'.
361dateFieldWith :: TimeLocale -- ^ Output time locale
289dateFieldWith :: TimeLocale -- ^ Output time locale
362 -> String -- ^ Destination key
290 -> String -- ^ Destination key
363 -> String -- ^ Format to use on the date
291 -> String -- ^ Format to use on the date
@@ -412,7 +340,6 @@ getItemModificationTime identifier = do
414--------------------------------------------------------------------------------
342--------------------------------------------------------------------------------
415−-- | Creates a field with the last modification date of the underlying item.
416modificationTimeField :: String -- ^ Key
343modificationTimeField :: String -- ^ Key
417 -> String -- ^ Format
344 -> String -- ^ Format
418 -> Context a -- ^ Resulting context
345 -> Context a -- ^ Resulting context
@@ -420,8 +347,6 @@ modificationTimeField = modificationTimeFieldWith defaultTimeLocale
422--------------------------------------------------------------------------------
349--------------------------------------------------------------------------------
423−-- | Creates a field with the last modification date of the underlying item
424−-- in a custom localisation format (see 'formatTime').
425modificationTimeFieldWith :: TimeLocale -- ^ Time output locale
350modificationTimeFieldWith :: TimeLocale -- ^ Time output locale
426 -> String -- ^ Key
351 -> String -- ^ Key
427 -> String -- ^ Format
352 -> String -- ^ Format
@@ -460,11 +385,10 @@ teaserFieldWithSeparator separator key snapshot = field key $ \item -> do
462--------------------------------------------------------------------------------
387--------------------------------------------------------------------------------
463−-- | Constantly reports any field as missing. Mostly for internal usage,
464−-- it is the last choice in every context used in a template application.
465missingField :: Context a
388missingField :: Context a
466−missingField = Context $ \k _ _ -> noResult $
389+missingField = Context $ \k _ i -> fail $
467− "Missing field '" ++ k ++ "' in context"
390+ "Missing field $" ++ k ++ "$ in context for item " ++
391+ show (itemIdentifier i)
469parseTimeM :: Bool -> TimeLocale -> String -> String -> Maybe UTCTime
393parseTimeM :: Bool -> TimeLocale -> String -> String -> Maybe UTCTime
470#if MIN_VERSION_time(1,5,0)
394#if MIN_VERSION_time(1,5,0)
@@ -472,10 +396,3 @@ parseTimeM = TF.parseTimeM
473parseTimeM _ = TF.parseTime
397parseTimeM _ = TF.parseTime
476−--------------------------------------------------------------------------------
478−-- | Binds an 'Item' to a given 'Context', allowing it to be combined with any
479−-- other 'Context' of any type.
480−bindItem :: Context a -> Item a -> forall b. Context b
481−bindItem (Context ctx) ia = Context $ \k args _ -> ctx k args ia