--------------------------------------------------------------------------------
-- | Parser utilities
module Hakyll.Core.Util.Parser
    ( metadataKey
    ) where


--------------------------------------------------------------------------------
import           Control.Applicative ((<|>))
import           Control.Monad       (guard, mzero, void)
import qualified Text.Parsec         as P
import           Text.Parsec.String  (Parser)


--------------------------------------------------------------------------------
metadataKey :: Parser String
metadataKey :: Parser String
metadataKey = do
    -- Ensure trailing '-' binds to '$' if present.
    let hyphon :: ParsecT String u Identity Char
hyphon = forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
P.try forall a b. (a -> b) -> a -> b
$ do
            forall (f :: * -> *) a. Functor f => f a -> f ()
void forall a b. (a -> b) -> a -> b
$ forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
P.char Char
'-'
            Char
x <- forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m a
P.lookAhead forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Char
P.anyChar
            forall (f :: * -> *). Alternative f => Bool -> f ()
guard forall a b. (a -> b) -> a -> b
$ Char
x forall a. Eq a => a -> a -> Bool
/= Char
'$'
            forall (f :: * -> *) a. Applicative f => a -> f a
pure Char
'-'

    String
i <- (:) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Char
P.letter forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m [a]
P.many (forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Char
P.alphaNum forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m Char
P.oneOf String
"_." forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> forall {u}. ParsecT String u Identity Char
hyphon)
    if String
i forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [String]
reservedKeys then forall (m :: * -> *) a. MonadPlus m => m a
mzero else forall (m :: * -> *) a. Monad m => a -> m a
return String
i


--------------------------------------------------------------------------------
reservedKeys :: [String]
reservedKeys :: [String]
reservedKeys = [String
"if", String
"else", String
"endif", String
"for", String
"sep", String
"endfor", String
"partial"]