on master help sign in
Expr trimming. '-' binds to '$'.
commit9867094bd27ed170315e7e02b788243d654c6bc1 authorsamgd <sam@samgd.com> dateSat 23 Jul 2016, 14:46 (10 years ago) parent430a0a8
3 files changed+24−7
src/Hakyll/Core/Util/Parser.hs+9−2 src/Hakyll/Web/Template/Internal.hs+8−5 tests/Hakyll/Web/Template/Tests.hs+7
src/Hakyll/Core/Util/Parser.hs+9−2
@@ -8,7 +8,7 @@ module Hakyll.Core.Util.Parser
88
99--------------------------------------------------------------------------------
1010import Control.Applicative ((<|>))
11−import Control.Monad (mzero)
11+import Control.Monad (guard, mzero, void)
1212import qualified Text.Parsec as P
1313import Text.Parsec.String (Parser)
1414
@@ -16,7 +16,14 @@ import Text.Parsec.String (Parser)
1616--------------------------------------------------------------------------------
1717metadataKey :: Parser String
1818metadataKey = do
19− i <- (:) <$> P.letter <*> (P.many $ P.alphaNum <|> P.oneOf "_-.")
19+ -- Ensure trailing '-' binds to '$' if present.
20+ let hyphon = P.try $ do
21+ void $ P.char '-'
22+ x <- P.lookAhead P.anyChar
23+ guard $ x /= '$'
24+ pure '-'
25+
26+ i <- (:) <$> P.letter <*> P.many (P.alphaNum <|> P.oneOf "_." <|> hyphon)
2027 if i `elem` reservedKeys then mzero else return i
2128
2229
src/Hakyll/Web/Template/Internal.hs+8−5
@@ -143,7 +143,7 @@ template = mconcat <$> P.many (P.choice [ lift chunk
143143 , conditional
144144 , for
145145 , partial
146− , lift expr
146+ , expr
147147 ])
148148 where lift = fmap (Template . (:[]))
149149
@@ -154,12 +154,15 @@ chunk = Chunk <$> P.many1 (P.noneOf "$")
154154
155155
156156--------------------------------------------------------------------------------
157−expr :: P.Parser TemplateElement
157+expr :: P.Parser Template
158158expr = P.try $ do
159− void $ P.char '$'
159+ trimLExpr <- trimOpen
160160 e <- expr'
161− void $ P.char '$'
162− return $ Expr e
161+ trimRExpr <- trimClose
162+ return $ Template $ mconcat [ [TrimL | trimLExpr]
163+ , [Expr e]
164+ , [TrimR | trimRExpr]
165+ ]
163166
164167
165168--------------------------------------------------------------------------------
tests/Hakyll/Web/Template/Tests.hs+7
@@ -75,6 +75,13 @@ tests = testGroup "Hakyll.Core.Template.Tests" $ concat
7575 , TrimR
7676 ]
7777 @=? readTemplate "$-partial(\"path\")-$"
78+ -- 'Expr' trim check.
79+ , Template
80+ [ TrimL
81+ , Expr (Ident (TemplateKey "foo"))
82+ , TrimR
83+ ]
84+ @=? readTemplate "$-foo-$"
7885 ]
7986 ]
8087