on master help sign in
Gave some functions better (more appropriate) names.
commita2c15932b713e81dcd1344f9227db2c3a65103bf authorJasper Van der Jeugt <jaspervdj@gmail.com> dateThu 11 Mar 2010, 12:10 (17 years ago) parentd25b0b6
8 files changed+58−64
hakyll.cabal+1−1 src/Text/Hakyll/Context.hs+4 src/Text/Hakyll/ContextManipulations.hs+6−8 src/Text/Hakyll/Renderables.hs → src/Text/Hakyll/CreateContext.hs+21−24 src/Text/Hakyll/Feed.hs+9−9 src/Text/Hakyll/Paginate.hs+1−1 src/Text/Hakyll/Render.hs+13−18 src/Text/Hakyll/Tags.hs+3−3
hakyll.cabal+1−1
@@ -45,12 +45,12 @@ library
4545 Text.Hakyll
4646 Text.Hakyll.Context
4747 Text.Hakyll.ContextManipulations
48+ Text.Hakyll.CreateContext
4849 Text.Hakyll.File
4950 Text.Hakyll.HakyllMonad
5051 Text.Hakyll.Regex
5152 Text.Hakyll.Render
5253 Text.Hakyll.HakyllAction
53− Text.Hakyll.Renderables
5454 Text.Hakyll.Paginate
5555 Text.Hakyll.Util
5656 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.
14module Text.Hakyll.Context
25 ( Context
36 ) where
47
58import Data.Map (Map)
69
10+-- | Datatype used for key-value mappings.
711type Context = Map String String
src/Text/Hakyll/ContextManipulations.hs+6−8
@@ -21,8 +21,7 @@ import Text.Hakyll.HakyllAction (HakyllAction)
2121import Text.Hakyll.Context (Context)
2222
2323-- | 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
25−-- @ContextManipulation@.
24+-- If the key given is not present in the @Context@, nothing will happen.
2625renderValue :: String -- ^ Key of which the value should be copied.
2726 -> String -- ^ Key the value should be copied to.
2827 -> (String -> String) -- ^ Function to apply on the value.
@@ -49,8 +48,9 @@ copyValue :: String -- ^ Source key.
4948 -> HakyllAction Context Context
5049copyValue source destination = renderValue source destination id
5150
52−-- | When the context has a key called @path@ in a @yyyy-mm-dd-title.extension@
53−-- format (default for pages), this function can render the date.
51+-- | When the context has a key called @path@ in a
52+-- @folder/yyyy-mm-dd-title.extension@ format (the convention for pages),
53+-- this function can render the date.
5454--
5555-- > renderDate "date" "%B %e, %Y" "Date unknown"
5656--
@@ -72,11 +72,9 @@ renderDate key format defaultValue = renderValue "path" key renderDate'
7272-- | Change the extension of a file. This is only needed when you want to
7373-- render, for example, mardown to @.php@ files instead of @.html@ files.
7474--
75−-- > renderChainWith (changeExtension "php")
76−-- > ["templates/default.html"]
77−-- > (createPagePath "test.markdown")
75+-- > changeExtension "php"
7876--
79−-- Will render to @test.php@ instead of @test.html@.
77+-- Will render @test.markdown@ to @test.php@ instead of @test.html@.
8078changeExtension :: String -- ^ Extension to change to.
8179 -> HakyllAction Context Context
8280changeExtension extension = changeValue "url" changeExtension'
src/Text/Hakyll/Renderables.hs → src/Text/Hakyll/CreateContext.hsrenamed+21−24
@@ -1,7 +1,10 @@
1−module Text.Hakyll.Renderables
2− ( createCustomPage
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@.
4+module Text.Hakyll.CreateContext
5+ ( createPage
6+ , createCustomPage
37 , createListing
4− , createPagePath
58 , combine
69 , combineWithUrl
710 ) where
@@ -17,13 +20,22 @@ import Text.Hakyll.HakyllAction
1720import Text.Hakyll.Render
1821import Text.Hakyll.Internal.Page
1922
20−-- | Create a custom page.
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+
32+-- | Create a "custom page" @Context@.
2133--
2234-- 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@.
24−-- A @Hakyll String@ is preferred for more complex data, since it allows
25−-- dependency checking. A @String@ is obviously more simple to use in some
26−-- cases.
35+-- that as value, you can either give a @String@ or a
36+-- @HakyllAction () String@. The latter is preferred for more complex data,
37+-- since it allows dependency checking. A @String@ is obviously more simple
38+-- to use in some cases.
2739createCustomPage :: String
2840 -> [(String, Either String (HakyllAction () String))]
2941 -> HakyllAction () Context
@@ -42,17 +54,10 @@ createCustomPage url association = HakyllAction
4254
4355-- | A @createCustomPage@ function specialized in creating listings.
4456--
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
4658-- item in the list is created by applying the given template to every
4759-- renderable. You can also specify additional context to be included in the
4860-- @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
5661createListing :: String -- ^ Destination of the page.
5762 -> [FilePath] -- ^ Templates to render items with.
5863 -> [HakyllAction () Context] -- ^ Renderables in the list.
@@ -64,14 +69,6 @@ createListing url templates renderables additional =
6469 context = ("body", Right concatenation) : additional
6570 concatenation = renderAndConcat templates renderables
6671
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−
7572-- | Combine two renderables. The url will always be taken from the first
7673-- @Renderable@. Also, if a `$key` is present in both renderables, the
7774-- value from the first @Renderable@ will be taken as well.
src/Text/Hakyll/Feed.hs+9−9
@@ -2,12 +2,11 @@
22-- you must make sure you set the `absoluteUrl` field in the main Hakyll
33-- configuration.
44--
5−-- Apart from that, the main rendering functions (@renderRss@,
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
8−-- the first item in the list.
5+-- Apart from that, the main rendering functions (@renderRss@, @renderAtom@)
6+-- all assume that you pass the list of items so that the most recent entry
7+-- in the feed is the first item in the list.
98--
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
1110-- fields to produce a correct feed:
1211--
1312-- - @$title@: Title of the item.
@@ -16,7 +15,8 @@
1615--
1716-- - @$url@: URL to the item - this is usually set automatically.
1817--
19−-- Furthermore, the feed will not validate if an empty list is passed.
18+-- Furthermore, the feed will be generated, but will be incorrect (it won't
19+-- validate) if an empty list is passed.
2020module Text.Hakyll.Feed
2121 ( FeedConfiguration (..)
2222 , renderRss
@@ -29,10 +29,10 @@ import Data.Maybe (fromMaybe)
2929import qualified Data.Map as M
3030
3131import Text.Hakyll.Context (Context)
32+import Text.Hakyll.CreateContext (createListing)
3233import Text.Hakyll.ContextManipulations (renderDate)
3334import Text.Hakyll.HakyllMonad (Hakyll)
3435import Text.Hakyll.Render (render, renderChain)
35−import Text.Hakyll.Renderables (createListing)
3636import Text.Hakyll.HakyllAction
3737
3838import Paths_hakyll
@@ -53,8 +53,8 @@ data FeedConfiguration = FeedConfiguration
5353-- The items should be sorted on date.
5454createFeed :: FeedConfiguration -- ^ Feed configuration.
5555 -> [HakyllAction () Context] -- ^ Items to include.
56− -> FilePath -- ^ Feed template.
57− -> FilePath -- ^ Item template.
56+ -> FilePath -- ^ Feed template.
57+ -> FilePath -- ^ Item template.
5858 -> HakyllAction () Context
5959createFeed configuration renderables template itemTemplate =
6060 listing >>> render template
src/Text/Hakyll/Paginate.hs+1−1
@@ -8,7 +8,7 @@ module Text.Hakyll.Paginate
88import Control.Applicative ((<$>))
99
1010import Text.Hakyll.Context (Context)
11−import Text.Hakyll.Renderables
11+import Text.Hakyll.CreateContext
1212import Text.Hakyll.HakyllAction
1313import Text.Hakyll.Util (link)
1414
src/Text/Hakyll/Render.hs+13−18
@@ -9,6 +9,7 @@ module Text.Hakyll.Render
99 ) where
1010
1111import Control.Arrow ((>>>))
12+import Control.Applicative ((<$>))
1213import Control.Monad.Reader (liftIO)
1314import System.Directory (copyFile)
1415import Data.Maybe (fromMaybe)
@@ -21,10 +22,10 @@ import Text.Hakyll.HakyllAction
2122import Text.Hakyll.Internal.CompressCss
2223import Text.Hakyll.Internal.Template
2324
24−-- | A pure render function.
25+-- | A pure render function - used internally.
2526pureRender :: Template -- ^ Template to use for rendering.
26− -> Context -- ^ Renderable object to render with given template.
27− -> Context -- ^ The body of the result will contain the render.
27+ -> Context -- ^ Renderable object to render with given template.
28+ -> Context -- ^ The body of the result will contain the render.
2829pureRender template context =
2930 -- Ignore $root when substituting here. We will only replace that in the
3031 -- final render (just before writing).
@@ -32,29 +33,26 @@ pureRender template context =
3233 body = regularSubstitute template contextIgnoringRoot
3334 in M.insert "body" body context
3435
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.
3638render :: FilePath -- ^ Template to use for rendering.
3739 -> HakyllAction Context Context -- ^ The render computation.
3840render templatePath = HakyllAction
3941 { actionDependencies = [templatePath]
4042 , actionUrl = Nothing
41− , actionFunction = actionFunction'
43+ , actionFunction = \context ->
44+ flip pureRender context <$> readTemplate templatePath
4245 }
43− where
44− actionFunction' context = do
45− template <- readTemplate templatePath
46− return $ pureRender template context
4746
48−-- | Render each renderable with the given templates, then concatenate the
47+-- | Render each @Context@ with the given templates, then concatenate the
4948-- result. So, basically this function:
5049--
51−-- * Takes every renderable.
50+-- - Takes every @Context@.
5251--
53−-- * Renders every renderable with all given templates. This is comparable
52+-- - Renders every @Context@ with all given templates. This is comparable
5453-- with a renderChain action.
5554--
56−-- * Concatenates the result.
57−--
55+-- - Concatenates the result and returns that as a @String@.
5856renderAndConcat :: [FilePath]
5957 -> [HakyllAction () Context]
6058 -> HakyllAction () String
@@ -87,11 +85,8 @@ renderChain :: [FilePath]
8785renderChain templatePaths initial =
8886 runHakyllActionIfNeeded renderChainWith'
8987 where
90− renderChainWith' :: HakyllAction () ()
9188 renderChainWith' = initial >>> chain' >>> writePage
92−
93− chain' = chain (map render templatePaths)
94−
89+ chain' = chain $ map render templatePaths
9590
9691-- | Mark a certain file as static, so it will just be copied when the site is
9792-- generated.
src/Text/Hakyll/Tags.hs+3−3
@@ -46,9 +46,9 @@ import System.FilePath
4646
4747import Text.Hakyll.Context (Context)
4848import Text.Hakyll.ContextManipulations (changeValue)
49+import Text.Hakyll.CreateContext (createPage)
4950import Text.Hakyll.HakyllMonad (Hakyll)
5051import Text.Hakyll.Regex
51−import Text.Hakyll.Renderables
5252import Text.Hakyll.HakyllAction
5353import Text.Hakyll.Util
5454import Text.Hakyll.Internal.Cache
@@ -82,11 +82,11 @@ readMap getTagsFunction identifier paths = HakyllAction
8282 else do assocMap' <- readTagMap'
8383 storeInCache (M.toAscList assocMap') fileName
8484 return assocMap'
85− return $ M.map (map createPagePath) assocMap
85+ return $ M.map (map createPage) assocMap
8686
8787 readTagMap' = foldM addPaths M.empty paths
8888 addPaths current path = do
89− context <- runHakyllAction $ createPagePath path
89+ context <- runHakyllAction $ createPage path
9090 let tags = getTagsFunction context
9191 addPaths' = flip (M.insertWith (++)) [path]
9292 return $ foldr addPaths' current tags