From 1ee5ec030b8366b5d7a7a16b1c644cb9ed3cf1e1 Mon Sep 17 00:00:00 2001 From: Vo Minh Thu Date: Tue, 18 Jun 2024 17:08:43 +0200 Subject: [PATCH] Support arbitrary element names. Standard HTML elements can now be defined using a combination of `frag` and `el` as shown in the example `.slab` file. --- examples/el.html | 20 ++++++++++++++++++++ examples/el.slab | 17 +++++++++++++++++ src/Slab/Parse.hs | 4 +++- src/Slab/Render.hs | 2 ++ src/Slab/Syntax.hs | 2 ++ 5 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 examples/el.html create mode 100644 examples/el.slab diff --git a/examples/el.html b/examples/el.html new file mode 100644 index 0000000..095b48f --- /dev/null +++ b/examples/el.html @@ -0,0 +1,20 @@ + + Hello. + + +

+ Hello. +

+
+ + Hello. + + +

+ Hello. +

+
+ + + + diff --git a/examples/el.slab b/examples/el.slab new file mode 100644 index 0000000..ceead57 --- /dev/null +++ b/examples/el.slab @@ -0,0 +1,17 @@ +el x Hello. + +el x + p Hello. + +frag y + el y + default content + +y Hello. + +y + p Hello. + +y + +el z.a diff --git a/src/Slab/Parse.hs b/src/Slab/Parse.hs index 3fe3473..3e2e339 100644 --- a/src/Slab/Parse.hs +++ b/src/Slab/Parse.hs @@ -266,7 +266,9 @@ parserNameWithAttrs :: Parser (Elem, [Attr], TrailingSym) parserNameWithAttrs = lexeme ( do - a <- parserElem + a <- + parserElem + <|> (lexeme (string "el") *> (Elem <$> lexeme parserName)) -- `try` because we want to backtrack if there is a dot -- not followed by a class name, for mdot to succeed. b <- many parserAttrs' diff --git a/src/Slab/Render.hs b/src/Slab/Render.hs index f03ebc2..8eeb24a 100644 --- a/src/Slab/Render.hs +++ b/src/Slab/Render.hs @@ -14,6 +14,7 @@ import Text.Blaze.Html.Renderer.Text (renderHtml) import Text.Blaze.Html5 (Html, (!)) import Text.Blaze.Html5 qualified as H import Text.Blaze.Html5.Attributes qualified as A +import Text.Blaze.Internal qualified as I import Text.Blaze.Svg11 qualified as S -------------------------------------------------------------------------------- @@ -195,3 +196,4 @@ renderElem = \case Syntax.Svg -> S.svg Syntax.Textarea -> H.textarea Syntax.Canvas -> H.canvas + Syntax.Elem name -> I.customParent (H.textTag name) diff --git a/src/Slab/Syntax.hs b/src/Slab/Syntax.hs index 9b78c10..9ae0e0d 100644 --- a/src/Slab/Syntax.hs +++ b/src/Slab/Syntax.hs @@ -143,6 +143,8 @@ data Elem | Svg | Textarea | Canvas + | -- | Arbitrary element name, using the @el@ keyword. + Elem Text deriving (Show, Eq) data TrailingSym = HasDot | HasEqual | NoSym