@@ -1,5 +1,6 @@
11module Text.Hakyll.Page
22 ( Page,
3+ PageValue,
34 addContext,
45 getURL,
56 getBody,
@@ -11,6 +12,7 @@ module Text.Hakyll.Page
1112
1213import qualified Data.Map as M
1314import qualified Data.List as L
15+import qualified Data.ByteString.Lazy.Char8 as B
1416import Data.Maybe
1517
1618import System.FilePath
@@ -20,40 +22,50 @@ import Text.Pandoc
2022
2123-- | A Page is basically key-value mapping. Certain keys have special
2224-- meanings, like for example url, body and title.
23−type Page = M.Map String String
25+type Page = M.Map String PageValue
26+
27+-- | We use a ByteString for obvious reasons.
28+type PageValue = B.ByteString
29+
2430
2531-- | Add a key-value mapping to the Page.
2632addContext :: String -> String -> Page -> Page
27−addContext = M.insert
33+addContext key value = M.insert key (B.pack value)
2834
2935-- | Get the URL for a certain page. This should always be defined. If
3036-- not, it will return trash.html.
3137getURL :: Page -> String
32−getURL context = fromMaybe "trash.html" $ M.lookup "url" context
38+getURL context = let result = M.lookup "url" context
39+ in case result of (Just url) -> B.unpack url
40+ Nothing -> error "URL is not defined."
3341
3442-- | Get the body for a certain page. When not defined, the body will be
3543-- empty.
36−getBody :: Page -> String
37−getBody context = fromMaybe "" $ M.lookup "body" context
38−
39−readConfig :: [String] -> Page
40−readConfig = M.fromList . map (trim . break (== ':'))
41− where trim (key, value) = (key, dropWhile (`elem` ": ") value)
42−
43−extractContext :: String -> Page
44−extractContext str = M.insert "body" (unlines body) (readConfig header)
45− where allLines = lines str
46− isDelimiter = L.isPrefixOf "---"
47− (header, body) | isDelimiter (head allLines) = let (h, b) = L.break (isDelimiter) (tail allLines)
48− in (h, tail b)
49− | otherwise = ([], allLines)
44+getBody :: Page -> PageValue
45+getBody context = fromMaybe B.empty $ M.lookup "body" context
5046
5147writerOptions :: WriterOptions
5248writerOptions = defaultWriterOptions
5349
54−markdownToHTML :: String -> String
55−markdownToHTML = writeHtmlString writerOptions .
56− readMarkdown defaultParserState
50+renderFunction :: String -> (String -> String)
51+renderFunction ".html" = id
52+renderFunction ext = writeHtmlString writerOptions .
53+ renderFunction' ext defaultParserState
54+ where renderFunction' ".markdown" = readMarkdown
55+ renderFunction' ".md" = readMarkdown
56+ renderFunction' ".tex" = readLaTeX
57+ renderFunction' _ = readMarkdown
58+
59+readMetaData :: Handle -> IO [(String, String)]
60+readMetaData handle = do
61+ line <- hGetLine handle
62+ if isDelimiter line then return []
63+ else do others <- readMetaData handle
64+ return $ (trim . break (== ':')) line : others
65+ where trim (key, value) = (key, dropWhile (`elem` ": ") value)
66+
67+isDelimiter :: String -> Bool
68+isDelimiter = L.isPrefixOf "---"
5769
5870-- | Read a page from a file. Metadata is supported, and if the filename
5971-- has a .markdown extension, it will be rendered using pandoc. Note that
@@ -61,24 +73,30 @@ markdownToHTML = writeHtmlString writerOptions .
6173readPage :: FilePath -> IO Page
6274readPage path = do
6375 handle <- openFile path ReadMode
64− content <- hGetContents handle
65− seq content $ hClose handle
66− let context = extractContext content
67− body = (if takeExtension path == ".markdown" then markdownToHTML else id)
68− (getBody context)
76+ line <- hGetLine handle
77+ (context, body) <- if isDelimiter line
78+ then do md <- readMetaData handle
79+ c <- hGetContents handle
80+ return (md, c)
81+ else hGetContents handle >>= \b -> return ([], line ++ b)
82+
83+ let rendered = B.pack $ (renderFunction $ takeExtension path) body
6984 url = addExtension (dropExtension path) ".html"
70− return $ addContext "url" url $ addContext "body" body $ context
85+ seq rendered $ hClose handle
86+ return $ M.insert "body" rendered $ addContext "url" url $ pageFromList context
7187
7288-- | Create a key-value mapping page from an association list.
7389pageFromList :: [(String, String)] -> Page
74−pageFromList = M.fromList
90+pageFromList = M.fromList . map packPair
91+ where packPair (k, v) = let pv = B.pack v
92+ in seq pv (k, pv)
7593
7694-- | Concat the bodies of pages, and return the result.
77−concatPages :: [Page] -> String
95+concatPages :: [Page] -> PageValue
7896concatPages = concatPagesWith "body"
7997
8098-- | Concat certain values of pages, and return the result.
8199concatPagesWith :: String -- ^ Key of which to concat the values.
82100 -> [Page] -- ^ Pages to get the values from.
83− -> String -- ^ The concatenation.
84−concatPagesWith key = concat . map (fromMaybe "" . M.lookup key)
101+ -> PageValue -- ^ The concatenation.
102+concatPagesWith key = B.concat . map (fromMaybe B.empty . M.lookup key)