on master help sign in
Added createListing, createListingWith functions.

These functions were added to have a simpler and more high-level way to do the common createCustomPage/renderAndConcat combination to create post lists, rss feeds, ...

commit592845985828f3ee135c309257e9bbcb305be1fa authorJasper Van der Jeugt <jaspervdj@gmail.com> dateWed 27 Jan 2010, 18:02 (17 years ago) parentf06b77b
6 files changed+55−17
src/Text/Hakyll/Context.hs+3−3 src/Text/Hakyll/Internal/Template.hs+6−6 src/Text/Hakyll/Page.hs+1−1 src/Text/Hakyll/Render.hs+3−3 src/Text/Hakyll/Renderables.hs+41−3 src/Text/Hakyll/Tags.hs+1−1
src/Text/Hakyll/Context.hs+3−3
@@ -24,9 +24,9 @@ type Context = Map String String
2424-- | Type for context manipulating functions.
2525type ContextManipulation = Context -> Context
2626
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.
2828-- This is probably the most common function to construct a
29−-- "ContextManipulation".
29+-- @ContextManipulation@.
3030renderValue :: String -- ^ Key of which the value should be copied.
3131 -> String -- ^ Key the value should be copied to.
3232 -> (String -> String) -- ^ Function to apply on the value.
@@ -35,7 +35,7 @@ renderValue src dst f context = case M.lookup src context of
3535 Nothing -> context
3636 (Just value) -> M.insert dst (f value) context
3737
38−-- | Change a value in a "Context".
38+-- | Change a value in a @Context@.
3939--
4040-- > import Data.Char (toUpper)
4141-- > changeValue "title" (map toUpper)
src/Text/Hakyll/Internal/Template.hs+6−6
@@ -29,7 +29,7 @@ data Template = Chunk String Template
2929 | End
3030 deriving (Show, Read, Eq)
3131
32−-- | Construct a "Template" from a string.
32+-- | Construct a @Template@ from a string.
3333fromString :: String -> Template
3434fromString [] = End
3535fromString string
@@ -41,7 +41,7 @@ fromString string
4141 where
4242 tail' = tail string
4343
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@
4545-- from the cache, if available.
4646readTemplate :: FilePath -> Hakyll Template
4747readTemplate path = do
@@ -54,7 +54,7 @@ readTemplate path = do
5454 where
5555 fileName = "templates" </> path
5656
57−-- | Substitutes @$identifiers@ in the given "Template" by values from the given
57+-- | Substitutes @$identifiers@ in the given @Template@ by values from the given
5858-- "Context". When a key is not found, it is left as it is. You can specify
5959-- the characters used to replace escaped dollars (@$$@) here.
6060substitute :: String -> Template -> Context -> String
@@ -68,12 +68,12 @@ substitute escaper (EscapeCharacter template) context =
6868 escaper ++ substitute escaper template context
6969substitute _ End _ = []
7070
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
7272-- they are.
7373regularSubstitute :: Template -> Context -> String
7474regularSubstitute = substitute "$$"
7575
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
7777-- escaped characters.
7878finalSubstitute :: Template -> Context -> String
7979finalSubstitute = substitute "$"
@@ -111,7 +111,7 @@ arbitraryTemplate length' = oneof [ do chunk <- chunk'
111111 return $ if null sanitized then "foo"
112112 else sanitized
113113
114−-- | Make "Template" testable.
114+-- | Make @Template@ testable.
115115instance Arbitrary Template where
116116 arbitrary = choose (0, 20) >>= arbitraryTemplate
117117
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.
22module Text.Hakyll.Page
33 ( Page
44 , fromContext
src/Text/Hakyll/Render.hs+3−3
@@ -71,8 +71,8 @@ renderAndConcat :: Renderable a
7171renderAndConcat = renderAndConcatWith id
7272
7373-- | Render each renderable with the given templates, then concatenate the
74−-- result. This function allows you to specify a "ContextManipulation" to
75−-- apply on every "Renderable".
74+-- result. This function allows you to specify a @ContextManipulation@ to
75+-- apply on every @Renderable@.
7676renderAndConcatWith :: Renderable a
7777 => ContextManipulation
7878 -> [FilePath]
@@ -97,7 +97,7 @@ renderChain :: Renderable a => [FilePath] -> a -> Hakyll ()
9797renderChain = renderChainWith id
9898
9999-- | 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.
101101renderChainWith :: Renderable a
102102 => ContextManipulation -> [FilePath] -> a -> Hakyll ()
103103renderChainWith manipulation templatePaths renderable =
src/Text/Hakyll/Renderables.hs+41−3
@@ -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
src/Text/Hakyll/Tags.hs+1−1
@@ -59,7 +59,7 @@ readTagMap identifier paths = do
5959 return $ foldr (flip (M.insertWith (++)) [path]) current tags
6060
6161-- | 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@.
6363 -> (String -> String) -- ^ Function to produce an url for a tag.
6464 -> Float -- ^ Smallest font size, in percent.
6565 -> Float -- ^ Biggest font size, in percent.