@@ -0,0 +1,47 @@
1+module Text.Hakyll.Page where
3+import qualified Data.Map as M
4+import qualified Data.List as L
9+type Page = M.Map String String
11+addContext :: String -> String -> Page -> Page
12+addContext key value = M.insert key value
14+getURL :: Page -> String
15+getURL context = fromMaybe "404.html" $ M.lookup "url" context
17+getBody :: Page -> String
18+getBody context = fromMaybe "" $ M.lookup "body" context
20+readConfig :: [String] -> Page
21+readConfig lines = M.fromList $ map (trim . break (== ':')) lines
22+ where trim (key, value) = (key, dropWhile (`elem` ": ") value)
24+extractContext :: String -> Page
25+extractContext str = M.insert "body" (unlines body) (readConfig header)
26+ where allLines = lines str
27+ isDelimiter = L.isPrefixOf "---"
28+ (header, body) | isDelimiter (head allLines) = let (h, b) = L.break (isDelimiter) (tail allLines)
30+ | otherwise = ([], allLines)
32+writerOptions :: WriterOptions
33+writerOptions = defaultWriterOptions
35+markdownToHTML :: String -> String
36+markdownToHTML = writeHtmlString writerOptions .
37+ readMarkdown defaultParserState
39+readPage :: FilePath -> IO Page
41+ content <- readFile path
42+ let context = extractContext content
43+ body = (if takeExtension path == ".markdown" then markdownToHTML else id)
45+ url = addExtension (dropExtension path) ".html"
46+ return $ addContext "url" url $ addContext "body" body $ context