Permalink
Browse files

better mustache parser

  • Loading branch information...
singpolyma committed Aug 23, 2012
1 parent 56f7616 commit f6deffc42e0ad4af63bd445f6645e80f05cc8d15
Showing with 15 additions and 8 deletions.
  1. +15 −8 mustache2hs.hs
View
@@ -68,12 +68,13 @@ parser = do
partial <|>
tripleVar <|>
ampVar <|>
- mustache (var True) <|>
+ mustache False (var True) <|>
+ singlebrace <|>
txt
)
return $ filter (not . isMuComment) body
where
- comment = mustache $ do
+ comment = mustache True $ do
_ <- char '!'
many $ do
c1 <- anyChar
@@ -84,31 +85,37 @@ parser = do
sectionInv =
liftA2 MuSectionInv (sectionPiece '^') parser <* sectionPiece '/'
section = liftA2 MuSection (sectionPiece '#') parser <* sectionPiece '/'
- sectionPiece c = mustache $ do
+ sectionPiece c = mustache True $ do
_ <- char c
name
- partial = mustache $ do
+ partial = mustache False $ do
_ <- char '>'
skipSpace
MuPartial <$> takeWhile1 (/='}')
- tripleVar = mustache $ do
+ tripleVar = mustache False $ do
_ <- char '{'
v <- var False
_ <- char '}'
return v
- ampVar = mustache $ do
+ ampVar = mustache False $ do
_ <- char '&'
skipSpace
var False
var escaped = (`MuVar` escaped) <$> name
- txt = MuText <$> takeWhile1 (/='{') -- TODO: allow single { in text
+ singlebrace = MuText <$> T.singleton <$> (do
+ c <- char '{'
+ n <- peekChar
+ if n == Just '{' then fail "singlebrace not match {{" else return c
+ )
+ txt = MuText <$> takeWhile1 (/='{')
name = takeWhile1 (\c -> isAlpha c || isDigit c)
- mustache f = do
+ mustache ws f = do
_ <- char '{'
_ <- char '{'
v <- f
_ <- char '}'
_ <- char '}'
+ when ws (endOfLine <|> pure ())
return v
mintercalate :: (Monoid a) => a -> [a] -> a

0 comments on commit f6deffc

Please sign in to comment.