on master help sign in
Wrote documentation for Text.Hakyll.Page and Text.Hakyll.Renderable.
e609920Jasper Van der Jeugt
src/Text/Hakyll/Page.hs+7−1
@@ -24,6 +24,7 @@ import Text.Pandoc
24-- meanings, like for example url, body and title.
24-- meanings, like for example url, body and title.
25data Page = Page (M.Map B.ByteString B.ByteString)
25data Page = Page (M.Map B.ByteString B.ByteString)
26
26
27+-- | Create a Page from a key-value mapping.
27fromContext :: (M.Map B.ByteString B.ByteString) -> Page
28fromContext :: (M.Map B.ByteString B.ByteString) -> Page
28fromContext = Page
29fromContext = Page
29
30
@@ -31,6 +32,7 @@ fromContext = Page
31addContext :: String -> String -> Page -> Page
32addContext :: String -> String -> Page -> Page
32addContext key value (Page page) = Page $ M.insert (B.pack key) (B.pack value) page
33addContext key value (Page page) = Page $ M.insert (B.pack key) (B.pack value) page
33
34
35+-- | Auxiliary function to pack a pair.
34packPair :: (String, String) -> (B.ByteString, B.ByteString)
36packPair :: (String, String) -> (B.ByteString, B.ByteString)
35packPair (a, b) = (B.pack a, B.pack b)
37packPair (a, b) = (B.pack a, B.pack b)
36
38
@@ -47,9 +49,11 @@ getPageURL (Page page) =
47getBody :: Page -> B.ByteString
49getBody :: Page -> B.ByteString
48getBody (Page page) = fromMaybe B.empty $ M.lookup (B.pack "body") page
50getBody (Page page) = fromMaybe B.empty $ M.lookup (B.pack "body") page
49
51
52+-- | The default writer options for pandoc rendering.
50writerOptions :: WriterOptions
53writerOptions :: WriterOptions
51writerOptions = defaultWriterOptions
54writerOptions = defaultWriterOptions
52
55
56+-- | Get a render function for a given extension.
53renderFunction :: String -> (String -> String)
57renderFunction :: String -> (String -> String)
54renderFunction ".html" = id
58renderFunction ".html" = id
55renderFunction ext = writeHtmlString writerOptions .
59renderFunction ext = writeHtmlString writerOptions .
@@ -59,6 +63,7 @@ renderFunction ext = writeHtmlString writerOptions .
59 readFunction ".tex" = readLaTeX
63 readFunction ".tex" = readLaTeX
60 readFunction _ = readMarkdown
64 readFunction _ = readMarkdown
61
65
66+-- | Read metadata header from a file handle.
62readMetaData :: Handle -> IO [(String, String)]
67readMetaData :: Handle -> IO [(String, String)]
63readMetaData handle = do
68readMetaData handle = do
64 line <- hGetLine handle
69 line <- hGetLine handle
@@ -67,6 +72,7 @@ readMetaData handle = do
67 return $ (trimPair . break (== ':')) line : others
72 return $ (trimPair . break (== ':')) line : others
68 where trimPair (key, value) = (trim key, trim $ tail value)
73 where trimPair (key, value) = (trim key, trim $ tail value)
69
74
75+-- | Check if the given string is a metadata delimiter.
70isDelimiter :: String -> Bool
76isDelimiter :: String -> Bool
71isDelimiter = L.isPrefixOf "---"
77isDelimiter = L.isPrefixOf "---"
72
78
@@ -119,7 +125,7 @@ readPage pagePath = do
119pageFromList :: [(String, String)] -> Page
125pageFromList :: [(String, String)] -> Page
120pageFromList = Page . M.fromList . map packPair
126pageFromList = Page . M.fromList . map packPair
121
127
122−-- Make pages renderable
128+-- Make pages renderable.
123instance Renderable Page where
129instance Renderable Page where
124 getDependencies = (:[]) . flip addExtension ".html" . dropExtension . getPageURL
130 getDependencies = (:[]) . flip addExtension ".html" . dropExtension . getPageURL
125 getURL = getPageURL
131 getURL = getPageURL
src/Text/Hakyll/Renderable.hs+8−4
@@ -1,14 +1,18 @@
1module Text.Hakyll.Renderable
1module Text.Hakyll.Renderable
2− ( Renderable,
3− toContext,
4− getDependencies,
2+ ( Renderable(toContext, getDependencies, getURL)
5− getURL
6 ) where
3 ) where
7
4
8import System.FilePath
5import System.FilePath
9import Text.Template
6import Text.Template
10
7
8+-- | A class for datatypes that can be rendered to pages.
11class Renderable a where
9class Renderable a where
10+ -- | Get a context to do substitutions with.
12 toContext :: a -> IO Context
11 toContext :: a -> IO Context
12+
13+ -- | Get the dependencies for the renderable. This is used for cache
14+ -- invalidation.
13 getDependencies :: a -> [FilePath]
15 getDependencies :: a -> [FilePath]
16+
17+ -- | Get the destination for the renderable.
14 getURL :: a -> FilePath
18 getURL :: a -> FilePath