diff options
-rw-r--r-- | src/Hakyll/Core/Util/Parser.hs | 11 | ||||
-rw-r--r-- | src/Hakyll/Web/Template/Internal.hs | 13 | ||||
-rw-r--r-- | tests/Hakyll/Web/Template/Tests.hs | 7 |
3 files changed, 24 insertions, 7 deletions
diff --git a/src/Hakyll/Core/Util/Parser.hs b/src/Hakyll/Core/Util/Parser.hs index e958b76..c4b2f8d 100644 --- a/src/Hakyll/Core/Util/Parser.hs +++ b/src/Hakyll/Core/Util/Parser.hs @@ -8,7 +8,7 @@ module Hakyll.Core.Util.Parser -------------------------------------------------------------------------------- import Control.Applicative ((<|>)) -import Control.Monad (mzero) +import Control.Monad (guard, mzero, void) import qualified Text.Parsec as P import Text.Parsec.String (Parser) @@ -16,7 +16,14 @@ import Text.Parsec.String (Parser) -------------------------------------------------------------------------------- metadataKey :: Parser String metadataKey = do - i <- (:) <$> P.letter <*> (P.many $ P.alphaNum <|> P.oneOf "_-.") + -- Ensure trailing '-' binds to '$' if present. + let hyphon = P.try $ do + void $ P.char '-' + x <- P.lookAhead P.anyChar + guard $ x /= '$' + pure '-' + + i <- (:) <$> P.letter <*> P.many (P.alphaNum <|> P.oneOf "_." <|> hyphon) if i `elem` reservedKeys then mzero else return i diff --git a/src/Hakyll/Web/Template/Internal.hs b/src/Hakyll/Web/Template/Internal.hs index 6ccad74..fce163f 100644 --- a/src/Hakyll/Web/Template/Internal.hs +++ b/src/Hakyll/Web/Template/Internal.hs @@ -143,7 +143,7 @@ template = mconcat <$> P.many (P.choice [ lift chunk , conditional , for , partial - , lift expr + , expr ]) where lift = fmap (Template . (:[])) @@ -154,12 +154,15 @@ chunk = Chunk <$> P.many1 (P.noneOf "$") -------------------------------------------------------------------------------- -expr :: P.Parser TemplateElement +expr :: P.Parser Template expr = P.try $ do - void $ P.char '$' + trimLExpr <- trimOpen e <- expr' - void $ P.char '$' - return $ Expr e + trimRExpr <- trimClose + return $ Template $ mconcat [ [TrimL | trimLExpr] + , [Expr e] + , [TrimR | trimRExpr] + ] -------------------------------------------------------------------------------- diff --git a/tests/Hakyll/Web/Template/Tests.hs b/tests/Hakyll/Web/Template/Tests.hs index 1ace2d4..a7b31a7 100644 --- a/tests/Hakyll/Web/Template/Tests.hs +++ b/tests/Hakyll/Web/Template/Tests.hs @@ -75,6 +75,13 @@ tests = testGroup "Hakyll.Core.Template.Tests" $ concat , TrimR ] @=? readTemplate "$-partial(\"path\")-$" + -- 'Expr' trim check. + , Template + [ TrimL + , Expr (Ident (TemplateKey "foo")) + , TrimR + ] + @=? readTemplate "$-foo-$" ] ] |