@@ -1,10 +1,11 @@
1--------------------------------------------------------------------------------
1--------------------------------------------------------------------------------
2+{-# LANGUAGE ExistentialQuantification #-}
2module Hakyll.Web.Template.Context
3module Hakyll.Web.Template.Context
3− ( Context (..)
4+ ( ContextField (..)
4− , mapContext
5+ , Context (..)
6 , constField
7 , constField
7− , functionField
8+ , listField
9 , defaultContext
10 , defaultContext
10 , teaserContext
11 , teaserContext
@@ -45,9 +46,16 @@ import Hakyll.Core.Util.String (splitAll, needlePrefix)
45import Hakyll.Web.Html
46import Hakyll.Web.Html
49+--------------------------------------------------------------------------------
50+-- | Mostly for internal usage
53+ | forall a. ListField (Context a) [Item a]
48--------------------------------------------------------------------------------
56--------------------------------------------------------------------------------
49newtype Context a = Context
57newtype Context a = Context
50− { unContext :: String -> Item a -> Compiler String
58+ { unContext :: String -> Item a -> Compiler ContextField
@@ -58,13 +66,13 @@ instance Monoid (Context a) where
60--------------------------------------------------------------------------------
68--------------------------------------------------------------------------------
61−mapContext :: (String -> String) -> Context a -> Context a
69+field' :: String -> (Item a -> Compiler ContextField) -> Context a
62−mapContext f (Context g) = Context $ \k i -> f <$> g k i
70+field' key value = Context $ \k i -> if k == key then value i else empty
65--------------------------------------------------------------------------------
73--------------------------------------------------------------------------------
66field :: String -> (Item a -> Compiler String) -> Context a
74field :: String -> (Item a -> Compiler String) -> Context a
67−field key value = Context $ \k i -> if k == key then value i else empty
75+field key value = field' key (fmap StringField . value)
70--------------------------------------------------------------------------------
78--------------------------------------------------------------------------------
@@ -73,12 +81,8 @@ constField key = field key . const . return
75--------------------------------------------------------------------------------
83--------------------------------------------------------------------------------
76−functionField :: String -> ([String] -> Item a -> Compiler String) -> Context a
84+listField :: String -> Context a -> Compiler [Item a] -> Context b
77−functionField name value = Context $ \k i -> case words k of
85+listField key c xs = field' key $ \_ -> fmap (ListField c) xs
80− | n == name -> value args i
84--------------------------------------------------------------------------------
88--------------------------------------------------------------------------------
@@ -91,6 +95,7 @@ defaultContext =
91 titleField "title" `mappend`
95 titleField "title" `mappend`
92 missingField
96 missingField
94--------------------------------------------------------------------------------
99--------------------------------------------------------------------------------
95-- | A context with "teaser" key which contain a teaser of the item.
100-- | A context with "teaser" key which contain a teaser of the item.
96-- The item is loaded from the given snapshot (which should be saved
101-- The item is loaded from the given snapshot (which should be saved
@@ -116,7 +121,7 @@ bodyField key = field key $ return . itemBody
116metadataField :: Context String
121metadataField :: Context String
117metadataField = Context $ \k i -> do
122metadataField = Context $ \k i -> do
118 value <- getMetadataField (itemIdentifier i) k
123 value <- getMetadataField (itemIdentifier i) k
119− maybe empty return value
124+ maybe empty (return . StringField) value
122--------------------------------------------------------------------------------
127--------------------------------------------------------------------------------
@@ -135,7 +140,7 @@ pathField key = field key $ return . toFilePath . itemIdentifier
135--------------------------------------------------------------------------------
140--------------------------------------------------------------------------------
136-- | This title field takes the basename of the underlying file by default
141-- | This title field takes the basename of the underlying file by default
137titleField :: String -> Context a
142titleField :: String -> Context a
138−titleField key = mapContext takeBaseName $ pathField key
143+titleField key = field key $ return . takeBaseName . toFilePath . itemIdentifier
141--------------------------------------------------------------------------------
146--------------------------------------------------------------------------------