Skip to content

Commit

Permalink
Support some content in an inline block.
Browse files Browse the repository at this point in the history
  • Loading branch information
noteed committed Jun 21, 2024
1 parent 15b6cb6 commit 1f9ce9f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 19 deletions.
3 changes: 3 additions & 0 deletions examples/interpolation.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
<p>
Hello, <a></a>.
</p>
<p>
Hello, <a>number 1</a>.
</p>
<p>
Hello, <x></x>.
</p>
1 change: 1 addition & 0 deletions examples/interpolation.slab
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ p Hello, #('Charlie').
p Hello, #(42).
p Hello, #(a).
p Hello, #{a}.
p Hello, #{a number #(1)}.
p Hello, #{el x}.
60 changes: 41 additions & 19 deletions src/Slab/Parse.hs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ parseFileE path = do
--------------------------------------------------------------------------------

parse :: FilePath -> Text -> Either (ParseErrorBundle Text Void) [Block]
parse fn = runParser (many parserNode <* eof) fn
parse fn = runParser (many parserBlock <* eof) fn

-- | We expose the expression parser for development:
--
Expand All @@ -60,8 +60,8 @@ parseExpr = runParser (sc *> parserExpr <* eof) ""
--------------------------------------------------------------------------------
type Parser = Parsec Void Text

parserNode :: Parser Block
parserNode = do
parserBlock :: Parser Block
parserBlock = do
node <-
L.indentBlock scn $
choice
Expand Down Expand Up @@ -93,12 +93,12 @@ parserIf = do
_ <- string "if"
_ <- some (char ' ' <|> char '\t')
cond <- parserExpr
pure $ L.IndentMany Nothing (pure . (\as -> BlockIf cond as [])) parserNode
pure $ L.IndentMany Nothing (pure . (\as -> BlockIf cond as [])) parserBlock

parserElse :: Parser (L.IndentOpt Parser [Block] Block)
parserElse = do
_ <- lexeme $ string "else"
pure $ L.IndentMany Nothing pure parserNode
pure $ L.IndentMany Nothing pure parserBlock

parserElement :: Parser (L.IndentOpt Parser Block Block)
parserElement = do
Expand Down Expand Up @@ -130,7 +130,7 @@ parserElemBody ref header =
NoSym -> do
template <- parseInlines
case template of
[] -> pure $ L.IndentMany Nothing (pure . header) parserNode
[] -> pure $ L.IndentMany Nothing (pure . header) parserBlock
_ -> pure $ L.IndentNone $ header [BlockText Normal template]

-- | Parse lines of text, indented more than `ref`.
Expand Down Expand Up @@ -397,7 +397,7 @@ parserFragmentDef = do
_ <- lexeme (string "fragment" <|> string "frag")
name <- lexeme parserIdentifier
params <- maybe [] id <$> optional parserParameters
pure $ L.IndentMany Nothing (pure . BlockFragmentDef name params) parserNode
pure $ L.IndentMany Nothing (pure . BlockFragmentDef name params) parserBlock

-- E.g. {}, {a, b}
parserParameters :: Parser [Text]
Expand Down Expand Up @@ -437,7 +437,7 @@ parserEach = do
_ <- lexeme (string "in")
collection <-
(List <$> parserList) <|> (Object <$> parserObject) <|> (Variable <$> parserVariable)
pure $ L.IndentMany Nothing (pure . BlockFor name mindex collection) parserNode
pure $ L.IndentMany Nothing (pure . BlockFor name mindex collection) parserBlock

parserList :: Parser [Expr]
parserList = parserList' "[" "]" parserExpr
Expand Down Expand Up @@ -529,7 +529,7 @@ parserName =
parserRawElement :: Parser (L.IndentOpt Parser Block Block)
parserRawElement = do
header <- parserAngleBracket
pure $ L.IndentMany Nothing (pure . header) parserNode
pure $ L.IndentMany Nothing (pure . header) parserBlock

parserAngleBracket :: Parser ([Block] -> Block)
parserAngleBracket = do
Expand All @@ -542,14 +542,14 @@ parserDefault :: Parser (L.IndentOpt Parser Block Block)
parserDefault = do
_ <- lexeme (string "default")
name <- parserText
pure $ L.IndentMany Nothing (pure . BlockDefault name) parserNode
pure $ L.IndentMany Nothing (pure . BlockDefault name) parserBlock

--------------------------------------------------------------------------------
parserImport :: Parser (L.IndentOpt Parser Block Block)
parserImport = do
_ <- lexeme (string "import")
path <- parserPath
pure $ L.IndentMany Nothing (pure . BlockImport path Nothing) parserNode
pure $ L.IndentMany Nothing (pure . BlockImport path Nothing) parserBlock

--------------------------------------------------------------------------------
parserRun :: Parser (L.IndentOpt Parser Block Block)
Expand Down Expand Up @@ -654,13 +654,22 @@ combineLits xs =
--------------------------------------------------------------------------------
-- Template parser

-- | Record whether we are parsing a template within the normal block syntax,
-- or within an inline block syntax (introduced by #{...}). This is allow a
-- closing curly bracket in the normal case without requiring to escape it, and
-- disallowing it in the inline case (since it is used to end the inline case).
data InlineContext = NormalBlock | InlineBlock

parseInlines :: Parser [Inline]
parseInlines = combineLits <$> M.many parseInline
parseInlines = parseInlines' NormalBlock

parseInlines' :: InlineContext -> Parser [Inline]
parseInlines' ctx = combineLits <$> M.many (parseInline ctx)

parseInline :: Parser Inline
parseInline =
parseInline :: InlineContext -> Parser Inline
parseInline ctx =
M.choice
[ parseLit
[ parseLit ctx
, parsePlaceExpr
, parsePlaceBlock
, parseEscape
Expand All @@ -669,9 +678,11 @@ parseInline =

-- TODO The \n condition could be optional if we want this module to be useful
-- outside Slab.
parseLit :: Parser Inline
parseLit = do
s <- M.takeWhile1P (Just "literal") (\c -> c /= '#' && c /= '\n')
parseLit :: InlineContext -> Parser Inline
parseLit ctx = do
s <- case ctx of
NormalBlock -> M.takeWhile1P (Just "literal") (\c -> c /= '#' && c /= '\n')
InlineBlock -> M.takeWhile1P (Just "literal") (\c -> c /= '#' && c /= '\n' && c/= '}')
pure $ Lit s

parsePlaceExpr :: Parser Inline
Expand All @@ -684,10 +695,21 @@ parsePlaceExpr = do
parsePlaceBlock :: Parser Inline
parsePlaceBlock = do
_ <- string $ T.pack "#{"
e <- (Block . ($ [])) <$> (parserDiv <|> parserCall)
e <- Block <$> parseInlineBlock
_ <- string $ T.pack "}"
pure $ Place e

-- | Equivalent to `parserBlock` but in an inline context.
parseInlineBlock :: Parser Block
parseInlineBlock = do
header <- parserDiv <|> parserCall
template <- parseInlines' InlineBlock
-- Don't return anything of the template is empty. to avoid a newline when
-- rendering.
if null template
then pure $ header []
else pure $ header [BlockText Dot template]

parseEscape :: Parser Inline
parseEscape = do
_ <- string $ T.pack "##"
Expand Down

0 comments on commit 1f9ce9f

Please sign in to comment.