-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support curly braces #26
Open
uhbif19
wants to merge
6
commits into
astynax:master
Choose a base branch
from
uhbif19:support-curly-braces
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
533723a
Support GHC 9.0 and 9.2 #19
uhbif19 b86a301
Rename BEM tests to reflect that they are BEM tests
uhbif19 fb733cf
Support Dom multiplicity #3
uhbif19 420e54a
Simplify multiplicity parsing
uhbif19 ba7e0e8
Curly braces initial rendering #4
uhbif19 1d981be
Update src/Hemmet/Dom/Rendering/KotlinxHtml.hs
uhbif19 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,71 @@ | ||
{-# LANGUAGE BlockArguments #-} | ||
|
||
module Hemmet.Dom.Template where | ||
|
||
import Data.Char | ||
import Data.Text hiding (map) | ||
import Data.Maybe (isJust) | ||
|
||
import Hemmet.Megaparsec | ||
import Hemmet.Tree | ||
import Text.Megaparsec.Char.Lexer (decimal) | ||
|
||
import Hemmet.Dom.Tree | ||
|
||
newtype Template = | ||
Template [Tag] | ||
Template [Element] | ||
deriving (Show, Eq) | ||
|
||
instance ToTree Template DomPayload where | ||
toTree = toTree' | ||
|
||
data Tag = | ||
data Element = | ||
Tag | ||
{ _tName :: !Text | ||
, _tId :: !(Maybe Text) | ||
, _tClasses :: ![Text] | ||
, _tChilds :: [Tag] | ||
} deriving (Show, Eq) | ||
, _tChilds :: [Element] | ||
} | ||
Comment on lines
24
to
+28
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now these are partial field selectors. Maybe we should extract a record? data Tag = Tag {...}
data Element = ETag !Tag | EText !Text Something like that. |
||
| PlainText !Text | ||
deriving (Show, Eq) | ||
|
||
|
||
template :: Parser Template | ||
template = Template <$> many_ tag <* eof | ||
template = Template <$> (Prelude.concat <$> many_ element) <* eof | ||
|
||
element :: Parser [Element] | ||
element = try tag <|> plainText | ||
|
||
tag :: Parser Tag | ||
tag :: Parser [Element] | ||
tag = do | ||
-- Order of attributes to parse is fixed, not arbitrary, like in Emmet. | ||
-- This is design decision. | ||
_tName <- try_ identifier | ||
_tId <- try_ (Just <$> (char '#' *> kebabCasedName)) <|> pure Nothing | ||
_tClasses <- many $ char '.' *> kebabCasedName | ||
_tChilds <- try_ childs | ||
return Tag {..} | ||
multiplicity <- char '*' *> decimal <|> pure 1 | ||
text <- optional curlyBraces | ||
childs <- Prelude.concat <$> try_ childsParser | ||
-- Text in curly braces is interpreted as the first child (as in Emmet) | ||
let _tChilds = case text of | ||
Just t -> PlainText t:childs | ||
Nothing -> childs | ||
let notEmpty = not (Data.Text.null _tName) | ||
|| isJust _tId | ||
|| not (Prelude.null _tClasses) | ||
if notEmpty | ||
then return $ Prelude.replicate multiplicity $ Tag {..} | ||
else fail "Tag is empty!" | ||
where | ||
childsParser = char '>' *> many_ element | ||
|
||
plainText :: Parser [Element] | ||
plainText = (:[]) . PlainText <$> curlyBraces | ||
|
||
curlyBraces :: Parser Text | ||
curlyBraces = textBetween '{' '}' | ||
where | ||
childs = char '>' *> many_ tag | ||
textBetween a b = between (char a) (char b) (takeWhileP Nothing (/= b)) | ||
|
||
identifier :: Parser Text | ||
identifier = cons <$> firstChar <*> (pack <$> many restChar) | ||
|
@@ -58,7 +89,8 @@ try_ = (<|> pure mempty) | |
|
||
-- transrormation to Tree | ||
toTree' :: Template -> Tree DomPayload | ||
toTree' (Template bs) = DomPayload Nothing [] $ map fromTag bs | ||
toTree' (Template bs) = DomTag Nothing [] $ map fromElement bs | ||
|
||
fromTag :: Tag -> Node DomPayload | ||
fromTag (Tag n i cls cs) = Node n $ DomPayload i cls $ map fromTag cs | ||
fromElement :: Element -> Node DomPayload | ||
fromElement (Tag n i cls cs) = Node n $ DomTag i cls $ map fromElement cs | ||
fromElement (PlainText text) = Node "" $ DomPlainText text |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,11 +8,11 @@ import Hemmet.Tree | |
|
||
type DomTree = Tree DomPayload | ||
|
||
data DomPayload a = DomPayload | ||
data DomPayload a = DomTag | ||
{ _dpId :: !(Maybe Text) | ||
, _dpClasses :: ![Text] | ||
, _dpChilds :: ![a] | ||
Comment on lines
12
to
14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. More partial field selectors. |
||
} | ||
} | DomPlainText !Text | ||
|
||
deriving instance Eq a => Eq (DomPayload a) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,5 @@ | |
<li.item> | ||
<li.item> | ||
<div#content.width-800.selected> | ||
text with space after # | ||
\ text with space before |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
#container>.nav>(ul.menu>li.item+li.item)+#content.width-800.selected | ||
#container>.nav>(ul.menu>li.item+li.item)+#content.width-800.selected>{text with space after }+{ text with space before} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe
uncons
&unsnoc
instead of partialhead
&tail
?