@@ -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)
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
@@ -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
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)
@@ -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
52+-- | The default writer options for pandoc rendering.
50writerOptions :: WriterOptions
53writerOptions :: WriterOptions
51writerOptions = defaultWriterOptions
54writerOptions = defaultWriterOptions
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
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)
75+-- | Check if the given string is a metadata delimiter.
70isDelimiter :: String -> Bool
76isDelimiter :: String -> Bool
71isDelimiter = L.isPrefixOf "---"
77isDelimiter = L.isPrefixOf "---"
@@ -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
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