summaryrefslogtreecommitdiff
path: root/src/Hakyll/Web/Template/Read.hs
diff options
context:
space:
mode:
authorJasper Van der Jeugt <m@jaspervdj.be>2013-05-04 11:36:58 +0200
committerJasper Van der Jeugt <m@jaspervdj.be>2013-05-04 11:36:58 +0200
commit4100083709a894225717dbe3068f73057e908dd6 (patch)
treebd519b91f8e3dcbdab03a5807112997d56d55d75 /src/Hakyll/Web/Template/Read.hs
parent28bc3f1f3b98f3bf4c8601af8eb8fa7a9c226ed2 (diff)
downloadhakyll-4100083709a894225717dbe3068f73057e908dd6.tar.gz
Style changes, move stuff to common parser module
Diffstat (limited to 'src/Hakyll/Web/Template/Read.hs')
-rw-r--r--src/Hakyll/Web/Template/Read.hs57
1 files changed, 29 insertions, 28 deletions
diff --git a/src/Hakyll/Web/Template/Read.hs b/src/Hakyll/Web/Template/Read.hs
index 9504f0b..620ce14 100644
--- a/src/Hakyll/Web/Template/Read.hs
+++ b/src/Hakyll/Web/Template/Read.hs
@@ -4,56 +4,57 @@ module Hakyll.Web.Template.Read
( readTemplate
) where
+
--------------------------------------------------------------------------------
-import Control.Applicative ((<$>), (<$), (<*>))
-import Control.Monad (void, mzero)
+import Control.Applicative ((<$), (<$>))
+import Control.Monad (void)
import Text.Parsec
import Text.Parsec.String
+
--------------------------------------------------------------------------------
+import Hakyll.Core.Util.Parser
import Hakyll.Web.Template.Internal
---------------------------------------------------------------------------------
+--------------------------------------------------------------------------------
readTemplate :: String -> Template
-readTemplate input =
- case parse template "" input of
+readTemplate input = case parse template "" input of
Left err -> error $ "Cannot parse template: " ++ show err
Right t -> t
+
+--------------------------------------------------------------------------------
template :: Parser Template
-template = Template <$>
- (many1 $ chunk <|> escaped <|> conditional <|> key)
+template = Template <$> (many1 $ chunk <|> escaped <|> conditional <|> key)
+
+--------------------------------------------------------------------------------
chunk :: Parser TemplateElement
chunk = Chunk <$> (many1 $ noneOf "$")
+
+--------------------------------------------------------------------------------
escaped :: Parser TemplateElement
escaped = Escaped <$ (try $ string "$$")
+
+--------------------------------------------------------------------------------
conditional :: Parser TemplateElement
conditional = try $ do
- void $ string "$if("
- i <- ident
- void $ string ")$"
- thenBranch <- template
- elseBranch <- optionMaybe $ try (string "$else$") >> template
- void $ string "$endif$"
- return $ If i thenBranch elseBranch
-
-ident :: Parser String
-ident = do
- i <- (:) <$> letter <*> (many $ alphaNum <|> oneOf " _-.")
- if i `elem` reserved
- then mzero
- else return i
-
-reserved :: [String]
-reserved = ["if", "else","endif"]
+ void $ string "$if("
+ i <- metadataKey
+ void $ string ")$"
+ thenBranch <- template
+ elseBranch <- optionMaybe $ try (string "$else$") >> template
+ void $ string "$endif$"
+ return $ If i thenBranch elseBranch
+
+--------------------------------------------------------------------------------
key :: Parser TemplateElement
key = try $ do
- void $ char '$'
- k <- ident
- void $ char '$'
- return $ Key k
+ void $ char '$'
+ k <- metadataKey
+ void $ char '$'
+ return $ Key k