on master help sign in
Added createListing, createListingWith functions.
5928459Jasper Van der Jeugt
src/Text/Hakyll/Context.hs+3−3
@@ -24,9 +24,9 @@ type Context = Map String String
24-- | Type for context manipulating functions.
24-- | Type for context manipulating functions.
25type ContextManipulation = Context -> Context
25type ContextManipulation = Context -> Context
26
26
27−-- | Do something with a value in a "Context", but keep the old value as well.
27+-- | Do something with a value in a @Context@, but keep the old value as well.
28-- This is probably the most common function to construct a
28-- This is probably the most common function to construct a
29−-- "ContextManipulation".
29+-- @ContextManipulation@.
30renderValue :: String -- ^ Key of which the value should be copied.
30renderValue :: String -- ^ Key of which the value should be copied.
31 -> String -- ^ Key the value should be copied to.
31 -> String -- ^ Key the value should be copied to.
32 -> (String -> String) -- ^ Function to apply on the value.
32 -> (String -> String) -- ^ Function to apply on the value.
@@ -35,7 +35,7 @@ renderValue src dst f context = case M.lookup src context of
35 Nothing -> context
35 Nothing -> context
36 (Just value) -> M.insert dst (f value) context
36 (Just value) -> M.insert dst (f value) context
37
37
38−-- | Change a value in a "Context".
38+-- | Change a value in a @Context@.
39--
39--
40-- > import Data.Char (toUpper)
40-- > import Data.Char (toUpper)
41-- > changeValue "title" (map toUpper)
41-- > changeValue "title" (map toUpper)
src/Text/Hakyll/Internal/Template.hs+6−6
@@ -29,7 +29,7 @@ data Template = Chunk String Template
29 | End
29 | End
30 deriving (Show, Read, Eq)
30 deriving (Show, Read, Eq)
31
31
32−-- | Construct a "Template" from a string.
32+-- | Construct a @Template@ from a string.
33fromString :: String -> Template
33fromString :: String -> Template
34fromString [] = End
34fromString [] = End
35fromString string
35fromString string
@@ -41,7 +41,7 @@ fromString string
41 where
41 where
42 tail' = tail string
42 tail' = tail string
43
43
44−-- | Read a "Template" from a file. This function might fetch the "Template"
44+-- | Read a @Template@ from a file. This function might fetch the @Template@
45-- from the cache, if available.
45-- from the cache, if available.
46readTemplate :: FilePath -> Hakyll Template
46readTemplate :: FilePath -> Hakyll Template
47readTemplate path = do
47readTemplate path = do
@@ -54,7 +54,7 @@ readTemplate path = do
54 where
54 where
55 fileName = "templates" </> path
55 fileName = "templates" </> path
56
56
57−-- | Substitutes @$identifiers@ in the given "Template" by values from the given
57+-- | Substitutes @$identifiers@ in the given @Template@ by values from the given
58-- "Context". When a key is not found, it is left as it is. You can specify
58-- "Context". When a key is not found, it is left as it is. You can specify
59-- the characters used to replace escaped dollars (@$$@) here.
59-- the characters used to replace escaped dollars (@$$@) here.
60substitute :: String -> Template -> Context -> String
60substitute :: String -> Template -> Context -> String
@@ -68,12 +68,12 @@ substitute escaper (EscapeCharacter template) context =
68 escaper ++ substitute escaper template context
68 escaper ++ substitute escaper template context
69substitute _ End _ = []
69substitute _ End _ = []
70
70
71−-- | "substitute" for use during a chain. This will leave escaped characters as
71+-- | @substitute@ for use during a chain. This will leave escaped characters as
72-- they are.
72-- they are.
73regularSubstitute :: Template -> Context -> String
73regularSubstitute :: Template -> Context -> String
74regularSubstitute = substitute "$$"
74regularSubstitute = substitute "$$"
75
75
76−-- | "substitute" for the end of a chain (just before writing). This renders
76+-- | @substitute@ for the end of a chain (just before writing). This renders
77-- escaped characters.
77-- escaped characters.
78finalSubstitute :: Template -> Context -> String
78finalSubstitute :: Template -> Context -> String
79finalSubstitute = substitute "$"
79finalSubstitute = substitute "$"
@@ -111,7 +111,7 @@ arbitraryTemplate length' = oneof [ do chunk <- chunk'
111 return $ if null sanitized then "foo"
111 return $ if null sanitized then "foo"
112 else sanitized
112 else sanitized
113
113
114−-- | Make "Template" testable.
114+-- | Make @Template@ testable.
115instance Arbitrary Template where
115instance Arbitrary Template where
116 arbitrary = choose (0, 20) >>= arbitraryTemplate
116 arbitrary = choose (0, 20) >>= arbitraryTemplate
117
117
src/Text/Hakyll/Page.hs+1−1
@@ -1,4 +1,4 @@
1−-- | A module for dealing with "Page"s. This module is mostly internally used.
1+-- | A module for dealing with @Page@s. This module is mostly internally used.
2module Text.Hakyll.Page
2module Text.Hakyll.Page
3 ( Page
3 ( Page
4 , fromContext
4 , fromContext
src/Text/Hakyll/Render.hs+3−3
@@ -71,8 +71,8 @@ renderAndConcat :: Renderable a
71renderAndConcat = renderAndConcatWith id
71renderAndConcat = renderAndConcatWith id
72
72
73-- | Render each renderable with the given templates, then concatenate the
73-- | Render each renderable with the given templates, then concatenate the
74−-- result. This function allows you to specify a "ContextManipulation" to
74+-- result. This function allows you to specify a @ContextManipulation@ to
75−-- apply on every "Renderable".
75+-- apply on every @Renderable@.
76renderAndConcatWith :: Renderable a
76renderAndConcatWith :: Renderable a
77 => ContextManipulation
77 => ContextManipulation
78 -> [FilePath]
78 -> [FilePath]
@@ -97,7 +97,7 @@ renderChain :: Renderable a => [FilePath] -> a -> Hakyll ()
97renderChain = renderChainWith id
97renderChain = renderChainWith id
98
98
99-- | A more custom render chain that allows you to specify a
99-- | A more custom render chain that allows you to specify a
100−-- "ContextManipulation" which to apply on the context when it is read first.
100+-- @ContextManipulation@ which to apply on the context when it is read first.
101renderChainWith :: Renderable a
101renderChainWith :: Renderable a
102 => ContextManipulation -> [FilePath] -> a -> Hakyll ()
102 => ContextManipulation -> [FilePath] -> a -> Hakyll ()
103renderChainWith manipulation templatePaths renderable =
103renderChainWith manipulation templatePaths renderable =
src/Text/Hakyll/Renderables.hs+41−3
@@ -1,6 +1,8 @@
1module Text.Hakyll.Renderables
1module Text.Hakyll.Renderables
2 ( CustomPage
2 ( CustomPage
3 , createCustomPage
3 , createCustomPage
4+ , createListing
5+ , createListingWith
4 , PagePath
6 , PagePath
5 , createPagePath
7 , createPagePath
6 , CombinedRenderable
8 , CombinedRenderable
@@ -9,11 +11,14 @@ module Text.Hakyll.Renderables
9 ) where
11 ) where
10
12
11import qualified Data.Map as M
13import qualified Data.Map as M
14+import Control.Arrow (second)
12
15
13import Text.Hakyll.Hakyll (Hakyll)
16import Text.Hakyll.Hakyll (Hakyll)
14import Text.Hakyll.Page
17import Text.Hakyll.Page
15import Text.Hakyll.Renderable
18import Text.Hakyll.Renderable
16import Text.Hakyll.File
19import Text.Hakyll.File
20+import Text.Hakyll.Context
21+import Text.Hakyll.Render
17
22
18-- | A custom page.
23-- | A custom page.
19data CustomPage = CustomPage
24data CustomPage = CustomPage
@@ -35,6 +40,39 @@ createCustomPage :: String -- ^ Destination of the page, relative to _site.
35 -> CustomPage
40 -> CustomPage
36createCustomPage = CustomPage
41createCustomPage = CustomPage
37
42
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+
38instance Renderable CustomPage where
76instance Renderable CustomPage where
39 getDependencies = customPageDependencies
77 getDependencies = customPageDependencies
40 getURL = customPageURL
78 getURL = customPageURL
@@ -62,15 +100,15 @@ data CombinedRenderable a b = CombinedRenderable a b
62 | CombinedRenderableWithURL FilePath a b
100 | CombinedRenderableWithURL FilePath a b
63
101
64-- | Combine two renderables. The url will always be taken from the first
102-- | Combine two renderables. The url will always be taken from the first
65−-- "Renderable". Also, if a `$key` is present in both renderables, the
103+-- @Renderable@. Also, if a `$key` is present in both renderables, the
66−-- value from the first "Renderable" will be taken as well.
104+-- value from the first @Renderable@ will be taken as well.
67--
105--
68-- Since renderables are always more or less key-value maps, you can see
106-- Since renderables are always more or less key-value maps, you can see
69-- this as a @union@ between two maps.
107-- this as a @union@ between two maps.
70combine :: (Renderable a, Renderable b) => a -> b -> CombinedRenderable a b
108combine :: (Renderable a, Renderable b) => a -> b -> CombinedRenderable a b
71combine = CombinedRenderable
109combine = CombinedRenderable
72
110
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@,
74-- except that for the @url@ field, the given URL is always chosen.
112-- except that for the @url@ field, the given URL is always chosen.
75combineWithURL :: (Renderable a, Renderable b)
113combineWithURL :: (Renderable a, Renderable b)
76 => FilePath
114 => FilePath
src/Text/Hakyll/Tags.hs+1−1
@@ -59,7 +59,7 @@ readTagMap identifier paths = do
59 return $ foldr (flip (M.insertWith (++)) [path]) current tags
59 return $ foldr (flip (M.insertWith (++)) [path]) current tags
60
60
61-- | Render a tag cloud.
61-- | Render a tag cloud.
62−renderTagCloud :: M.Map String [FilePath] -- ^ Map as produced by "readTagMap".
62+renderTagCloud :: M.Map String [FilePath] -- ^ Map as produced by @readTagMap@.
63 -> (String -> String) -- ^ Function to produce an url for a tag.
63 -> (String -> String) -- ^ Function to produce an url for a tag.
64 -> Float -- ^ Smallest font size, in percent.
64 -> Float -- ^ Smallest font size, in percent.
65 -> Float -- ^ Biggest font size, in percent.
65 -> Float -- ^ Biggest font size, in percent.