summaryrefslogtreecommitdiff
path: root/src/Hakyll/Web/Page/Read.hs
blob: bd7778967f6d5f96ab37fa45d74764e4c00c0700 (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
-- | Module providing a function to parse a page from a file
--
module Hakyll.Web.Page.Read
    ( readPage
    ) where

import Control.Applicative ((<$>), (<*>), (<*))
import qualified Data.Map as M

import Text.Parsec.Char (alphaNum, anyChar, char, newline, oneOf, string)
import Text.Parsec.Combinator (choice, many1, manyTill, option, skipMany1)
import Text.Parsec.Prim (many, parse, skipMany, (<?>))
import Text.Parsec.String (Parser)

import Hakyll.Core.Util.String
import Hakyll.Web.Page.Internal

-- | Space or tab, no newline
inlineSpace :: Parser Char
inlineSpace = oneOf ['\t', ' '] <?> "space"

-- | Parse a single metadata field
--
metadataField :: Parser (String, String)
metadataField = do
    key <- manyTill alphaNum $ char ':'
    skipMany1 inlineSpace
    value <- manyTill anyChar newline
    trailing' <- many trailing
    return (key, trim $ value ++ concat trailing')
  where
    trailing = (++) <$> many1 inlineSpace <*> manyTill anyChar newline

-- | Parse a metadata block, including delimiters and trailing newlines
--
metadata :: Parser [(String, String)]
metadata = do
    open <- many1 (char '-') <* many inlineSpace <* newline
    metadata' <- many metadataField
    _ <- choice $ map (string . replicate (length open)) ['-', '.']
    skipMany inlineSpace
    skipMany1 newline
    return metadata'

-- | Parse a Hakyll page
--
page :: Parser ([(String, String)], String)
page = do
    metadata' <- option [] metadata
    body <- many anyChar
    return (metadata', body)

readPage :: String -> Page String
readPage input = case parse page "page" input of
    Left err      -> error (show err)
    Right (md, b) -> Page (M.fromList md) b