@@ -1,6 +1,8 @@
11module Text.Hakyll.Renderables
22 ( CustomPage
33 , createCustomPage
4+ , createListing
5+ , createListingWith
46 , PagePath
57 , createPagePath
68 , CombinedRenderable
@@ -9,11 +11,14 @@ module Text.Hakyll.Renderables
911 ) where
1012
1113import qualified Data.Map as M
14+import Control.Arrow (second)
1215
1316import Text.Hakyll.Hakyll (Hakyll)
1417import Text.Hakyll.Page
1518import Text.Hakyll.Renderable
1619import Text.Hakyll.File
20+import Text.Hakyll.Context
21+import Text.Hakyll.Render
1722
1823-- | A custom page.
1924data CustomPage = CustomPage
@@ -35,6 +40,39 @@ createCustomPage :: String -- ^ Destination of the page, relative to _site.
3540 -> CustomPage
3641createCustomPage = CustomPage
3742
43+-- | A @createCustomPage@ function specialized in creating listings.
44+--
45+-- This function creates a listing of a certain list of @Renderable@s. Every
46+-- item in the list is created by applying the given template to every
47+-- renderable. You can also specify additional context to be included in the
48+-- @CustomPage@.
49+createListing :: (Renderable a)
50+ => String -- ^ Destination of the page.
51+ -> FilePath -- ^ Template to render all items with.
52+ -> [a] -- ^ Renderables in the list.
53+ -> [(String, String)] -- ^ Additional context.
54+ -> CustomPage
55+createListing = createListingWith id
56+
57+-- | A @createCustomPage@ function specialized in creating listings.
58+--
59+-- In addition to @createListing@, this function allows you to specify an
60+-- extra @ContextManipulation@ for all @Renderable@s given.
61+createListingWith :: (Renderable a)
62+ => ContextManipulation -- ^ Manipulation for the renderables.
63+ -> String -- ^ Destination of the page.
64+ -> FilePath -- ^ Template to render all items with.
65+ -> [a] -- ^ Renderables in the list.
66+ -> [(String, String)] -- ^ Additional context.
67+ -> CustomPage
68+createListingWith manipulation url template renderables additional =
69+ createCustomPage url dependencies context
70+ where
71+ dependencies = template : concatMap getDependencies renderables
72+ context = ("body", Right concatenation) : additional'
73+ concatenation = renderAndConcatWith manipulation [template] renderables
74+ additional' = map (second Left) additional
75+
3876instance Renderable CustomPage where
3977 getDependencies = customPageDependencies
4078 getURL = customPageURL
@@ -62,15 +100,15 @@ data CombinedRenderable a b = CombinedRenderable a b
62100 | CombinedRenderableWithURL FilePath a b
63101
64102-- | Combine two renderables. The url will always be taken from the first
65−-- "Renderable". Also, if a `$key` is present in both renderables, the
66−-- value from the first "Renderable" will be taken as well.
103+-- @Renderable@. Also, if a `$key` is present in both renderables, the
104+-- value from the first @Renderable@ will be taken as well.
67105--
68106-- Since renderables are always more or less key-value maps, you can see
69107-- this as a @union@ between two maps.
70108combine :: (Renderable a, Renderable b) => a -> b -> CombinedRenderable a b
71109combine = CombinedRenderable
72110
73−-- | Combine two renderables and set a custom URL. This behaves like "combine",
111+-- | Combine two renderables and set a custom URL. This behaves like @combine@,
74112-- except that for the @url@ field, the given URL is always chosen.
75113combineWithURL :: (Renderable a, Renderable b)
76114 => FilePath