blob: cf39ddd5174840761bc5db1aa3d351478de63576 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
-- | Module providing a function to parse a page from a file
--
module Hakyll.Web.Page.Read
( readPage
) where
import Control.Applicative ((<$>), (<*>))
import Control.Arrow (second, (***))
import Control.Monad.State (State, get, put, evalState)
import Data.List (isPrefixOf)
import Data.Map (Map)
import qualified Data.Map as M
import Hakyll.Web.Page.Internal
import Hakyll.Core.Util.String
-- | We're using a simple state monad as parser
--
type LineParser = State [String]
-- | Read the metadata section from a page
--
parseMetadata :: LineParser (Map String String)
parseMetadata = get >>= \content -> case content of
-- No lines means no metadata
[] -> return M.empty
-- Check if the file begins with a delimiter
(l : ls) -> if not (isPossibleDelimiter l)
then -- No delimiter means no metadata
return M.empty
else do -- Break the metadata section
let (metadata, rest) = second (drop 1) $ break (== l) ls
-- Put the rest back
put rest
-- Parse the metadata
return $ M.fromList $ map parseMetadata' metadata
where
-- Check if a line can be a delimiter
isPossibleDelimiter = isPrefixOf "---"
-- Parse a "key: value" string to a (key, value) tupple
parseMetadata' = (trim *** trim . drop 1) . break (== ':')
-- | Read the body section of a page
--
parseBody :: LineParser String
parseBody = do
body <- get
put []
return $ unlines body
-- | Read an entire page
--
parsePage :: LineParser (Page String)
parsePage = Page <$> parseMetadata <*> parseBody
-- | Read a page from a string
--
readPage :: String -> Page String
readPage = evalState parsePage . lines
|