@@ -1,6 +1,5 @@
11module Text.Hakyll.Renderables
2− ( CustomPage
3− , createCustomPage
2+ ( createCustomPage
43 , createListing
54 , createListingWith
65 , PagePath
@@ -12,7 +11,8 @@ module Text.Hakyll.Renderables
1211
1312import qualified Data.Map as M
1413import Control.Arrow (second)
15−import Control.Monad (liftM)
14+import Control.Monad (liftM, liftM2, mplus)
15+import Control.Applicative ((<$>))
1616
1717import Data.Binary
1818
@@ -22,13 +22,7 @@ import Text.Hakyll.Renderable
2222import Text.Hakyll.File
2323import Text.Hakyll.Context
2424import Text.Hakyll.Render
25−
26−-- | A custom page.
27−data CustomPage = CustomPage
28− { customPageUrl :: String,
29− customPageDependencies :: [FilePath],
30− customPageContext :: [(String, Either String (Hakyll String))]
31− }
25+import Text.Hakyll.RenderAction
3226
3327-- | Create a custom page.
3428--
@@ -37,11 +31,18 @@ data CustomPage = CustomPage
3731-- A @Hakyll String@ is preferred for more complex data, since it allows
3832-- dependency checking. A @String@ is obviously more simple to use in some
3933-- cases.
40−createCustomPage :: String -- ^ Destination of the page, relative to _site.
41− -> [FilePath] -- ^ Dependencies of the page.
42− -> [(String, Either String (Hakyll String))] -- ^ Mapping.
43− -> CustomPage
44−createCustomPage = CustomPage
34+createCustomPage :: String
35+ -> [FilePath]
36+ -> [(String, Either String (Hakyll String))]
37+ -> RenderAction () Context
38+createCustomPage url dependencies association = RenderAction
39+ { actionDependencies = dependencies
40+ , actionDestination = Just $ return url
41+ , actionFunction = actionFunction'
42+ }
43+ where
44+ mtuple (a, b) = b >>= \b' -> return (a, b')
45+ actionFunction' () = M.fromList <$> mapM (mtuple . second (either return id)) association
4546
4647-- | A @createCustomPage@ function specialized in creating listings.
4748--
@@ -61,7 +62,7 @@ createListing :: (Renderable a)
6162 -> FilePath -- ^ Template to render all items with.
6263 -> [a] -- ^ Renderables in the list.
6364 -> [(String, String)] -- ^ Additional context.
64− -> CustomPage
65+ -> RenderAction () Context
6566createListing = createListingWith id
6667
6768-- | A @createCustomPage@ function specialized in creating listings.
@@ -74,7 +75,7 @@ createListingWith :: (Renderable a)
7475 -> FilePath -- ^ Template to render all items with.
7576 -> [a] -- ^ Renderables in the list.
7677 -> [(String, String)] -- ^ Additional context.
77− -> CustomPage
78+ -> RenderAction () Context
7879createListingWith manipulation url template renderables additional =
7980 createCustomPage url dependencies context
8081 where
@@ -83,14 +84,6 @@ createListingWith manipulation url template renderables additional =
8384 concatenation = renderAndConcatWith manipulation [template] renderables
8485 additional' = map (second Left) additional
8586
86−instance Renderable CustomPage where
87− getDependencies = customPageDependencies
88− getUrl = return . customPageUrl
89− toContext page = do
90− values <- mapM (either return id . snd) (customPageContext page)
91− let pairs = zip (map fst $ customPageContext page) values
92− return $ M.fromList $ ("url", customPageUrl page) : pairs
93−
9487-- | PagePath is a class that wraps a FilePath. This is used to render Pages
9588-- without reading them first through use of caching.
9689newtype PagePath = PagePath FilePath
@@ -122,8 +115,13 @@ data CombinedRenderable a b = CombinedRenderable a b
122115--
123116-- Since renderables are always more or less key-value maps, you can see
124117-- this as a @union@ between two maps.
125−combine :: (Renderable a, Renderable b) => a -> b -> CombinedRenderable a b
126−combine = CombinedRenderable
118+combine :: RenderAction () Context -> RenderAction () Context
119+ -> RenderAction () Context
120+combine x y = RenderAction
121+ { actionDependencies = actionDependencies x ++ actionDependencies y
122+ , actionDestination = actionDestination x `mplus` actionDestination y
123+ , actionFunction = \_ -> liftM2 (M.union) (actionFunction x ()) (actionFunction y ())
124+ }
127125
128126-- | Combine two renderables and set a custom URL. This behaves like @combine@,
129127-- except that for the @url@ field, the given URL is always chosen.