Skip to content

Commit

Permalink
Merge branch 'master' of github.com:digitallyinduced/ihp
Browse files Browse the repository at this point in the history
  • Loading branch information
mpscholten committed Aug 21, 2020
2 parents c6b92e6 + 9481033 commit 0bb881e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
11 changes: 11 additions & 0 deletions Guide/hsx.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,14 @@ In JSX you have the restriction that you always have to have a single root tag.
#### Whitespace

Spaces and newline characters are removed where possible at HSX parse time.

#### Comments

Html Comments are supported and can be used like this:

```html
<div>
<!-- Begin of Main Section -->
<h1>Hello</h1>
</div>
```
11 changes: 10 additions & 1 deletion IHP/HtmlSupport/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ data Node = Node !Text ![Attribute] ![Node]
| PreEscapedTextNode !Text -- ^ Used in @script@ or @style@ bodies
| SplicedNode !Text -- ^ Inline haskell expressions like @{myVar}@ or @{f "hello"}@
| Children ![Node]
| CommentNode !Text
deriving (Show)

parseHsx position code = runParser (setPosition position *> parser) "" code
Expand All @@ -47,7 +48,7 @@ parser = do
eof
pure node

hsxElement = try hsxSelfClosingElement <|> hsxNormalElement
hsxElement = try hsxComment <|> try hsxSelfClosingElement <|> hsxNormalElement

manyHsxElement = do
children <- many hsxChild
Expand Down Expand Up @@ -94,6 +95,14 @@ hsxOpeningElement = do
attributes <- hsxNodeAttributes (char '>')
pure (name, attributes)

hsxComment :: Parser Node
hsxComment = do
string "<!--"
body :: String <- manyTill (satisfy (const True)) (string "-->")
space
pure (CommentNode (cs body))


hsxNodeAttributes :: Parser a -> Parser [Attribute]
hsxNodeAttributes end = staticAttributes
where
Expand Down
1 change: 1 addition & 0 deletions IHP/HtmlSupport/QQ.hs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ compileToHaskell (SplicedNode code) =
case parseExp (cs code) of
Right expression -> let patched = patchExpr expression in [| toHtml $(pure patched) |]
Left error -> fail ("compileToHaskell(" <> (cs code) <> "): " <> show error)
compileToHaskell (CommentNode value) = [| Html5.textComment value |]

patchExpr :: TH.Exp -> TH.Exp
patchExpr (TH.UInfixE (TH.VarE varName) (TH.VarE hash) (TH.VarE labelValue)) | hash == TH.mkName "#" = TH.AppE (TH.VarE varName) fromLabel
Expand Down

0 comments on commit 0bb881e

Please sign in to comment.