on master help sign in
Gave some functions better (more appropriate) names.
a2c1593Jasper Van der Jeugt
hakyll.cabal+1−1
@@ -45,12 +45,12 @@ library
45 Text.Hakyll
45 Text.Hakyll
46 Text.Hakyll.Context
46 Text.Hakyll.Context
47 Text.Hakyll.ContextManipulations
47 Text.Hakyll.ContextManipulations
48+ Text.Hakyll.CreateContext
48 Text.Hakyll.File
49 Text.Hakyll.File
49 Text.Hakyll.HakyllMonad
50 Text.Hakyll.HakyllMonad
50 Text.Hakyll.Regex
51 Text.Hakyll.Regex
51 Text.Hakyll.Render
52 Text.Hakyll.Render
52 Text.Hakyll.HakyllAction
53 Text.Hakyll.HakyllAction
53− Text.Hakyll.Renderables
54 Text.Hakyll.Paginate
54 Text.Hakyll.Paginate
55 Text.Hakyll.Util
55 Text.Hakyll.Util
56 Text.Hakyll.Tags
56 Text.Hakyll.Tags
src/Text/Hakyll/Context.hs+4
@@ -1,7 +1,11 @@
1+-- | This (quite small) module exports the datatype used for contexts. A
2+-- @Context@ is a simple key-value mapping. You can render these @Context@s
3+-- with templates, and manipulate them in various ways.
1module Text.Hakyll.Context
4module Text.Hakyll.Context
2 ( Context
5 ( Context
3 ) where
6 ) where
4
7
5import Data.Map (Map)
8import Data.Map (Map)
6
9
10+-- | Datatype used for key-value mappings.
7type Context = Map String String
11type Context = Map String String
src/Text/Hakyll/ContextManipulations.hs+6−8
@@ -21,8 +21,7 @@ import Text.Hakyll.HakyllAction (HakyllAction)
21import Text.Hakyll.Context (Context)
21import Text.Hakyll.Context (Context)
22
22
23-- | Do something with a value in a @Context@, but keep the old value as well.
23-- | Do something with a value in a @Context@, but keep the old value as well.
24−-- This is probably the most common function to construct a
24+-- If the key given is not present in the @Context@, nothing will happen.
25−-- @ContextManipulation@.
26renderValue :: String -- ^ Key of which the value should be copied.
25renderValue :: String -- ^ Key of which the value should be copied.
27 -> String -- ^ Key the value should be copied to.
26 -> String -- ^ Key the value should be copied to.
28 -> (String -> String) -- ^ Function to apply on the value.
27 -> (String -> String) -- ^ Function to apply on the value.
@@ -49,8 +48,9 @@ copyValue :: String -- ^ Source key.
49 -> HakyllAction Context Context
48 -> HakyllAction Context Context
50copyValue source destination = renderValue source destination id
49copyValue source destination = renderValue source destination id
51
50
52−-- | When the context has a key called @path@ in a @yyyy-mm-dd-title.extension@
51+-- | When the context has a key called @path@ in a
52+-- @folder/yyyy-mm-dd-title.extension@ format (the convention for pages),
53−-- format (default for pages), this function can render the date.
53+-- this function can render the date.
54--
54--
55-- > renderDate "date" "%B %e, %Y" "Date unknown"
55-- > renderDate "date" "%B %e, %Y" "Date unknown"
56--
56--
@@ -72,11 +72,9 @@ renderDate key format defaultValue = renderValue "path" key renderDate'
72-- | Change the extension of a file. This is only needed when you want to
72-- | Change the extension of a file. This is only needed when you want to
73-- render, for example, mardown to @.php@ files instead of @.html@ files.
73-- render, for example, mardown to @.php@ files instead of @.html@ files.
74--
74--
75−-- > renderChainWith (changeExtension "php")
75+-- > changeExtension "php"
76−-- > ["templates/default.html"]
77−-- > (createPagePath "test.markdown")
78--
76--
79−-- Will render to @test.php@ instead of @test.html@.
77+-- Will render @test.markdown@ to @test.php@ instead of @test.html@.
80changeExtension :: String -- ^ Extension to change to.
78changeExtension :: String -- ^ Extension to change to.
81 -> HakyllAction Context Context
79 -> HakyllAction Context Context
82changeExtension extension = changeValue "url" changeExtension'
80changeExtension extension = changeValue "url" changeExtension'
src/Text/Hakyll/Renderables.hs → src/Text/Hakyll/CreateContext.hsrenamed+21−24
@@ -1,7 +1,10 @@
1+-- | A module that provides different ways to create a @Context@. These
2+-- functions all use the @HakyllAction@ arrow, so they produce values of the
3+-- type @HakyllAction () Context@.
1−module Text.Hakyll.Renderables
4+module Text.Hakyll.CreateContext
5+ ( createPage
2− ( createCustomPage
6+ , createCustomPage
3 , createListing
7 , createListing
4− , createPagePath
5 , combine
8 , combine
6 , combineWithUrl
9 , combineWithUrl
7 ) where
10 ) where
@@ -17,13 +20,22 @@ import Text.Hakyll.HakyllAction
17import Text.Hakyll.Render
20import Text.Hakyll.Render
18import Text.Hakyll.Internal.Page
21import Text.Hakyll.Internal.Page
19
22
23+-- | Create a @Context@ from a page file stored on the disk. This is probably
24+-- the most common way to create a @Context@.
25+createPage :: FilePath -> HakyllAction () Context
26+createPage path = HakyllAction
27+ { actionDependencies = [path]
28+ , actionUrl = Just $ toUrl path
29+ , actionFunction = const (readPage path)
30+ }
31+
20−-- | Create a custom page.
32+-- | Create a "custom page" @Context@.
21--
33--
22-- The association list given maps keys to values for substitution. Note
34-- The association list given maps keys to values for substitution. Note
23−-- that as value, you can either give a @String@ or a @Hakyll String@.
35+-- that as value, you can either give a @String@ or a
24−-- A @Hakyll String@ is preferred for more complex data, since it allows
36+-- @HakyllAction () String@. The latter is preferred for more complex data,
25−-- dependency checking. A @String@ is obviously more simple to use in some
37+-- since it allows dependency checking. A @String@ is obviously more simple
26−-- cases.
38+-- to use in some cases.
27createCustomPage :: String
39createCustomPage :: String
28 -> [(String, Either String (HakyllAction () String))]
40 -> [(String, Either String (HakyllAction () String))]
29 -> HakyllAction () Context
41 -> HakyllAction () Context
@@ -42,17 +54,10 @@ createCustomPage url association = HakyllAction
42
54
43-- | A @createCustomPage@ function specialized in creating listings.
55-- | A @createCustomPage@ function specialized in creating listings.
44--
56--
45−-- This function creates a listing of a certain list of renderables. Every
57+-- This function creates a listing of a certain list of @Context@s. Every
46-- item in the list is created by applying the given template to every
58-- 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
59-- renderable. You can also specify additional context to be included in the
48-- @CustomPage@.
60-- @CustomPage@.
49−--
50−-- > let customPage = createListing
51−-- > "index.html" -- Destination of the page.
52−-- > ["templates/postitem.html"] -- Paths to templates to render the
53−-- > -- items with.
54−-- > posts -- Renderables to create the list with.
55−-- > [("title", Left "Home")] -- Additional context
56createListing :: String -- ^ Destination of the page.
61createListing :: String -- ^ Destination of the page.
57 -> [FilePath] -- ^ Templates to render items with.
62 -> [FilePath] -- ^ Templates to render items with.
58 -> [HakyllAction () Context] -- ^ Renderables in the list.
63 -> [HakyllAction () Context] -- ^ Renderables in the list.
@@ -64,14 +69,6 @@ createListing url templates renderables additional =
64 context = ("body", Right concatenation) : additional
69 context = ("body", Right concatenation) : additional
65 concatenation = renderAndConcat templates renderables
70 concatenation = renderAndConcat templates renderables
66
71
67−-- | Create a PagePath from a FilePath.
68−createPagePath :: FilePath -> HakyllAction () Context
69−createPagePath path = HakyllAction
70− { actionDependencies = [path]
71− , actionUrl = Just $ toUrl path
72− , actionFunction = const (readPage path)
73− }
74−
75-- | Combine two renderables. The url will always be taken from the first
72-- | Combine two renderables. The url will always be taken from the first
76-- @Renderable@. Also, if a `$key` is present in both renderables, the
73-- @Renderable@. Also, if a `$key` is present in both renderables, the
77-- value from the first @Renderable@ will be taken as well.
74-- value from the first @Renderable@ will be taken as well.
src/Text/Hakyll/Feed.hs+9−9
@@ -2,12 +2,11 @@
2-- you must make sure you set the `absoluteUrl` field in the main Hakyll
2-- you must make sure you set the `absoluteUrl` field in the main Hakyll
3-- configuration.
3-- configuration.
4--
4--
5−-- Apart from that, the main rendering functions (@renderRss@,
5+-- Apart from that, the main rendering functions (@renderRss@, @renderAtom@)
6−-- @renderRssWith@, @renderAtom@ all @renderAtomWith@) all assume that you
7−-- pass the list of @Renderable@s so that the most recent entry in the feed is
6+-- all assume that you pass the list of items so that the most recent entry
8−-- the first item in the list.
7+-- in the feed is the first item in the list.
9--
8--
10−-- Also note that the @Renderable@s should have (at least) the following
9+-- Also note that the @Context@s should have (at least) the following
11-- fields to produce a correct feed:
10-- fields to produce a correct feed:
12--
11--
13-- - @$title@: Title of the item.
12-- - @$title@: Title of the item.
@@ -16,7 +15,8 @@
16--
15--
17-- - @$url@: URL to the item - this is usually set automatically.
16-- - @$url@: URL to the item - this is usually set automatically.
18--
17--
18+-- Furthermore, the feed will be generated, but will be incorrect (it won't
19−-- Furthermore, the feed will not validate if an empty list is passed.
19+-- validate) if an empty list is passed.
20module Text.Hakyll.Feed
20module Text.Hakyll.Feed
21 ( FeedConfiguration (..)
21 ( FeedConfiguration (..)
22 , renderRss
22 , renderRss
@@ -29,10 +29,10 @@ import Data.Maybe (fromMaybe)
29import qualified Data.Map as M
29import qualified Data.Map as M
30
30
31import Text.Hakyll.Context (Context)
31import Text.Hakyll.Context (Context)
32+import Text.Hakyll.CreateContext (createListing)
32import Text.Hakyll.ContextManipulations (renderDate)
33import Text.Hakyll.ContextManipulations (renderDate)
33import Text.Hakyll.HakyllMonad (Hakyll)
34import Text.Hakyll.HakyllMonad (Hakyll)
34import Text.Hakyll.Render (render, renderChain)
35import Text.Hakyll.Render (render, renderChain)
35−import Text.Hakyll.Renderables (createListing)
36import Text.Hakyll.HakyllAction
36import Text.Hakyll.HakyllAction
37
37
38import Paths_hakyll
38import Paths_hakyll
@@ -53,8 +53,8 @@ data FeedConfiguration = FeedConfiguration
53-- The items should be sorted on date.
53-- The items should be sorted on date.
54createFeed :: FeedConfiguration -- ^ Feed configuration.
54createFeed :: FeedConfiguration -- ^ Feed configuration.
55 -> [HakyllAction () Context] -- ^ Items to include.
55 -> [HakyllAction () Context] -- ^ Items to include.
56− -> FilePath -- ^ Feed template.
56+ -> FilePath -- ^ Feed template.
57− -> FilePath -- ^ Item template.
57+ -> FilePath -- ^ Item template.
58 -> HakyllAction () Context
58 -> HakyllAction () Context
59createFeed configuration renderables template itemTemplate =
59createFeed configuration renderables template itemTemplate =
60 listing >>> render template
60 listing >>> render template
src/Text/Hakyll/Paginate.hs+1−1
@@ -8,7 +8,7 @@ module Text.Hakyll.Paginate
8import Control.Applicative ((<$>))
8import Control.Applicative ((<$>))
9
9
10import Text.Hakyll.Context (Context)
10import Text.Hakyll.Context (Context)
11−import Text.Hakyll.Renderables
11+import Text.Hakyll.CreateContext
12import Text.Hakyll.HakyllAction
12import Text.Hakyll.HakyllAction
13import Text.Hakyll.Util (link)
13import Text.Hakyll.Util (link)
14
14
src/Text/Hakyll/Render.hs+13−18
@@ -9,6 +9,7 @@ module Text.Hakyll.Render
9 ) where
9 ) where
10
10
11import Control.Arrow ((>>>))
11import Control.Arrow ((>>>))
12+import Control.Applicative ((<$>))
12import Control.Monad.Reader (liftIO)
13import Control.Monad.Reader (liftIO)
13import System.Directory (copyFile)
14import System.Directory (copyFile)
14import Data.Maybe (fromMaybe)
15import Data.Maybe (fromMaybe)
@@ -21,10 +22,10 @@ import Text.Hakyll.HakyllAction
21import Text.Hakyll.Internal.CompressCss
22import Text.Hakyll.Internal.CompressCss
22import Text.Hakyll.Internal.Template
23import Text.Hakyll.Internal.Template
23
24
24−-- | A pure render function.
25+-- | A pure render function - used internally.
25pureRender :: Template -- ^ Template to use for rendering.
26pureRender :: Template -- ^ Template to use for rendering.
26− -> Context -- ^ Renderable object to render with given template.
27+ -> Context -- ^ Renderable object to render with given template.
27− -> Context -- ^ The body of the result will contain the render.
28+ -> Context -- ^ The body of the result will contain the render.
28pureRender template context =
29pureRender template context =
29 -- Ignore $root when substituting here. We will only replace that in the
30 -- Ignore $root when substituting here. We will only replace that in the
30 -- final render (just before writing).
31 -- final render (just before writing).
@@ -32,29 +33,26 @@ pureRender template context =
32 body = regularSubstitute template contextIgnoringRoot
33 body = regularSubstitute template contextIgnoringRoot
33 in M.insert "body" body context
34 in M.insert "body" body context
34
35
35−-- | Render to a Page.
36+-- | This is the most simple render action. You render a @Context@ with a
37+-- template, and get back the result.
36render :: FilePath -- ^ Template to use for rendering.
38render :: FilePath -- ^ Template to use for rendering.
37 -> HakyllAction Context Context -- ^ The render computation.
39 -> HakyllAction Context Context -- ^ The render computation.
38render templatePath = HakyllAction
40render templatePath = HakyllAction
39 { actionDependencies = [templatePath]
41 { actionDependencies = [templatePath]
40 , actionUrl = Nothing
42 , actionUrl = Nothing
41− , actionFunction = actionFunction'
43+ , actionFunction = \context ->
44+ flip pureRender context <$> readTemplate templatePath
42 }
45 }
43− where
44− actionFunction' context = do
45− template <- readTemplate templatePath
46− return $ pureRender template context
47
46
48−-- | Render each renderable with the given templates, then concatenate the
47+-- | Render each @Context@ with the given templates, then concatenate the
49-- result. So, basically this function:
48-- result. So, basically this function:
50--
49--
51−-- * Takes every renderable.
50+-- - Takes every @Context@.
52--
51--
53−-- * Renders every renderable with all given templates. This is comparable
52+-- - Renders every @Context@ with all given templates. This is comparable
54-- with a renderChain action.
53-- with a renderChain action.
55--
54--
56−-- * Concatenates the result.
55+-- - Concatenates the result and returns that as a @String@.
57−--
58renderAndConcat :: [FilePath]
56renderAndConcat :: [FilePath]
59 -> [HakyllAction () Context]
57 -> [HakyllAction () Context]
60 -> HakyllAction () String
58 -> HakyllAction () String
@@ -87,11 +85,8 @@ renderChain :: [FilePath]
87renderChain templatePaths initial =
85renderChain templatePaths initial =
88 runHakyllActionIfNeeded renderChainWith'
86 runHakyllActionIfNeeded renderChainWith'
89 where
87 where
90− renderChainWith' :: HakyllAction () ()
91 renderChainWith' = initial >>> chain' >>> writePage
88 renderChainWith' = initial >>> chain' >>> writePage
92−
93− chain' = chain (map render templatePaths)
89+ chain' = chain $ map render templatePaths
94−
95
90
96-- | Mark a certain file as static, so it will just be copied when the site is
91-- | Mark a certain file as static, so it will just be copied when the site is
97-- generated.
92-- generated.
src/Text/Hakyll/Tags.hs+3−3
@@ -46,9 +46,9 @@ import System.FilePath
46
46
47import Text.Hakyll.Context (Context)
47import Text.Hakyll.Context (Context)
48import Text.Hakyll.ContextManipulations (changeValue)
48import Text.Hakyll.ContextManipulations (changeValue)
49+import Text.Hakyll.CreateContext (createPage)
49import Text.Hakyll.HakyllMonad (Hakyll)
50import Text.Hakyll.HakyllMonad (Hakyll)
50import Text.Hakyll.Regex
51import Text.Hakyll.Regex
51−import Text.Hakyll.Renderables
52import Text.Hakyll.HakyllAction
52import Text.Hakyll.HakyllAction
53import Text.Hakyll.Util
53import Text.Hakyll.Util
54import Text.Hakyll.Internal.Cache
54import Text.Hakyll.Internal.Cache
@@ -82,11 +82,11 @@ readMap getTagsFunction identifier paths = HakyllAction
82 else do assocMap' <- readTagMap'
82 else do assocMap' <- readTagMap'
83 storeInCache (M.toAscList assocMap') fileName
83 storeInCache (M.toAscList assocMap') fileName
84 return assocMap'
84 return assocMap'
85− return $ M.map (map createPagePath) assocMap
85+ return $ M.map (map createPage) assocMap
86
86
87 readTagMap' = foldM addPaths M.empty paths
87 readTagMap' = foldM addPaths M.empty paths
88 addPaths current path = do
88 addPaths current path = do
89− context <- runHakyllAction $ createPagePath path
89+ context <- runHakyllAction $ createPage path
90 let tags = getTagsFunction context
90 let tags = getTagsFunction context
91 addPaths' = flip (M.insertWith (++)) [path]
91 addPaths' = flip (M.insertWith (++)) [path]
92 return $ foldr addPaths' current tags
92 return $ foldr addPaths' current tags