@@ -0,0 +1,47 @@
1+module Text.Hakyll.Page where
2+
3+import qualified Data.Map as M
4+import qualified Data.List as L
5+import System.FilePath
6+import Data.Maybe
7+import Text.Pandoc
8+
9+type Page = M.Map String String
10+
11+addContext :: String -> String -> Page -> Page
12+addContext key value = M.insert key value
13+
14+getURL :: Page -> String
15+getURL context = fromMaybe "404.html" $ M.lookup "url" context
16+
17+getBody :: Page -> String
18+getBody context = fromMaybe "" $ M.lookup "body" context
19+
20+readConfig :: [String] -> Page
21+readConfig lines = M.fromList $ map (trim . break (== ':')) lines
22+ where trim (key, value) = (key, dropWhile (`elem` ": ") value)
23+
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)
29+ in (h, tail b)
30+ | otherwise = ([], allLines)
31+
32+writerOptions :: WriterOptions
33+writerOptions = defaultWriterOptions
34+
35+markdownToHTML :: String -> String
36+markdownToHTML = writeHtmlString writerOptions .
37+ readMarkdown defaultParserState
38+
39+readPage :: FilePath -> IO Page
40+readPage path = do
41+ content <- readFile path
42+ let context = extractContext content
43+ body = (if takeExtension path == ".markdown" then markdownToHTML else id)
44+ (getBody context)
45+ url = addExtension (dropExtension path) ".html"
46+ return $ addContext "url" url $ addContext "body" body $ context
47+