on master help sign in
Expr trimming. '-' binds to '$'.
9867094samgd
src/Hakyll/Core/Util/Parser.hs+9−2
@@ -8,7 +8,7 @@ module Hakyll.Core.Util.Parser
8
8
9--------------------------------------------------------------------------------
9--------------------------------------------------------------------------------
10import Control.Applicative ((<|>))
10import Control.Applicative ((<|>))
11−import Control.Monad (mzero)
11+import Control.Monad (guard, mzero, void)
12import qualified Text.Parsec as P
12import qualified Text.Parsec as P
13import Text.Parsec.String (Parser)
13import Text.Parsec.String (Parser)
14
14
@@ -16,7 +16,14 @@ import Text.Parsec.String (Parser)
16--------------------------------------------------------------------------------
16--------------------------------------------------------------------------------
17metadataKey :: Parser String
17metadataKey :: Parser String
18metadataKey = do
18metadataKey = do
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+
19− i <- (:) <$> P.letter <*> (P.many $ P.alphaNum <|> P.oneOf "_-.")
26+ i <- (:) <$> P.letter <*> P.many (P.alphaNum <|> P.oneOf "_." <|> hyphon)
20 if i `elem` reservedKeys then mzero else return i
27 if i `elem` reservedKeys then mzero else return i
21
28
22
29
src/Hakyll/Web/Template/Internal.hs+8−5
@@ -143,7 +143,7 @@ template = mconcat <$> P.many (P.choice [ lift chunk
143 , conditional
143 , conditional
144 , for
144 , for
145 , partial
145 , partial
146− , lift expr
146+ , expr
147 ])
147 ])
148 where lift = fmap (Template . (:[]))
148 where lift = fmap (Template . (:[]))
149
149
@@ -154,12 +154,15 @@ chunk = Chunk <$> P.many1 (P.noneOf "$")
154
154
155
155
156--------------------------------------------------------------------------------
156--------------------------------------------------------------------------------
157−expr :: P.Parser TemplateElement
157+expr :: P.Parser Template
158expr = P.try $ do
158expr = P.try $ do
159− void $ P.char '$'
159+ trimLExpr <- trimOpen
160 e <- expr'
160 e <- expr'
161− void $ P.char '$'
161+ trimRExpr <- trimClose
162+ return $ Template $ mconcat [ [TrimL | trimLExpr]
162− return $ Expr e
163+ , [Expr e]
164+ , [TrimR | trimRExpr]
165+ ]
163
166
164
167
165--------------------------------------------------------------------------------
168--------------------------------------------------------------------------------
tests/Hakyll/Web/Template/Tests.hs+7
@@ -75,6 +75,13 @@ tests = testGroup "Hakyll.Core.Template.Tests" $ concat
75 , TrimR
75 , TrimR
76 ]
76 ]
77 @=? readTemplate "$-partial(\"path\")-$"
77 @=? readTemplate "$-partial(\"path\")-$"
78+ -- 'Expr' trim check.
79+ , Template
80+ [ TrimL
81+ , Expr (Ident (TemplateKey "foo"))
82+ , TrimR
83+ ]
84+ @=? readTemplate "$-foo-$"
78 ]
85 ]
79 ]
86 ]
80
87