@@ -5,7 +5,8 @@ module Text.Hakyll.Page
55 getBody,
66 readPage,
77 pageFromList,
8− concatPages
8+ concatPages,
9+ concatPagesWith
910 ) where
1011
1112import qualified Data.Map as M
@@ -14,14 +15,21 @@ import System.FilePath
1415import Data.Maybe
1516import Text.Pandoc
1617
18+-- | A Page is basically key-value mapping. Certain keys have special
19+-- meanings, like for example url, body and title.
1720type Page = M.Map String String
1821
22+-- | Add a key-value mapping to the Page.
1923addContext :: String -> String -> Page -> Page
2024addContext = M.insert
2125
26+-- | Get the URL for a certain page. This should always be defined. If
27+-- not, it will return trash.html.
2228getURL :: Page -> String
23−getURL context = fromMaybe "404.html" $ M.lookup "url" context
29+getURL context = fromMaybe "trash.html" $ M.lookup "url" context
2430
31+-- | Get the body for a certain page. When not defined, the body will be
32+-- empty.
2533getBody :: Page -> String
2634getBody context = fromMaybe "" $ M.lookup "body" context
2735
@@ -44,6 +52,9 @@ markdownToHTML :: String -> String
4452markdownToHTML = writeHtmlString writerOptions .
4553 readMarkdown defaultParserState
4654
55+-- | Read a page from a file. Metadata is supported, and if the filename
56+-- has a .markdown extension, it will be rendered using pandoc. Note that
57+-- pages are not templates, so they should not contain $identifiers.
4758readPage :: FilePath -> IO Page
4859readPage path = do
4960 content <- readFile path
@@ -53,8 +64,16 @@ readPage path = do
5364 url = addExtension (dropExtension path) ".html"
5465 return $ addContext "url" url $ addContext "body" body $ context
5566
67+-- | Create a key-value mapping page from an association list.
5668pageFromList :: [(String, String)] -> Page
5769pageFromList = M.fromList
5870
71+-- | Concat the bodies of pages, and return the result.
5972concatPages :: [Page] -> String
60−concatPages = concat . map getBody
73+concatPages = concatPagesWith "body"
74+
75+-- | Concat certain values of pages, and return the result.
76+concatPagesWith :: String -- ^ Key of which to concat the values.
77+ -> [Page] -- ^ Pages to get the values from.
78+ -> String -- ^ The concatenation.
79+concatPagesWith key = concat . map (fromMaybe "" . M.lookup key)