lucid 2.0 → 2.1
raw patch · 5 files changed
+36/−23 lines, 5 files
Files
- README.md +7/−7
- lucid.cabal +1/−1
- src/Lucid/Base.hs +12/−12
- src/Lucid/Html5.hs +13/−1
- test/Main.hs +3/−2
README.md view
@@ -53,10 +53,10 @@ <div><p>hello</p><p>sup</p></div> ``` -Attributes are set using the 'with' combinator:+Attributes are set by providing an argument list: ``` haskell-λ> with p_ [class_ "brand"] "Lucid Inc" :: Html ()+λ> p_ [class_ "brand"] "Lucid Inc" :: Html () ``` ``` html@@ -66,10 +66,10 @@ Here is a fuller example of Lucid: ``` haskell-with table_ [rows_ "2"]- (tr_ (do with td_ [class_ "top",colspan_ "2"]- (p_ "Hello, attributes!")- td_ "yay!"))+table_ [rows_ "2"]+ (tr_ (do td_ [class_ "top",colspan_ "2",style_ "color:red"]+ (p_ "Hello, attributes!")+ td_ "yay!")) ``` ``` html@@ -98,7 +98,7 @@ Or to bytes: ``` haskell-λ> renderBS (with p_ [style_ "color:red"] "Hello!")+λ> renderBS (p_ [style_ "color:red"] "Hello!") ``` ``` html
lucid.cabal view
@@ -1,5 +1,5 @@ name: lucid-version: 2.0+version: 2.1 synopsis: Clear to write, read and edit DSL for HTML description: Clear to write, read and edit DSL for HTML. See the 'Lucid' module for description and documentation.
src/Lucid/Base.hs view
@@ -178,12 +178,12 @@ -> result -- ^ Result: either an element or an attribute. -- | Given attributes, expect more child input.-instance (Monad m,f ~ HtmlT m a, a ~ ()) => Term [Attribute] (f -> HtmlT m a) where+instance (Monad m,f ~ HtmlT m a) => Term [Attribute] (f -> HtmlT m a) where termWith name f = with (makeElement name) . (<> f) -- | Given children immediately, just use that and expect no -- attributes.-instance (Monad m,a ~ ()) => Term (HtmlT m a) (HtmlT m a) where+instance (Monad m) => Term (HtmlT m a) (HtmlT m a) where termWith name f = with (makeElement name) f -- | Some terms (like 'Lucid.Html5.style_', 'Lucid.Html5.title_') can be used for@@ -226,28 +226,28 @@ -- or one that just accepts attributes. See the two instances. class With a where -- | With the given element(s), use the given attributes.- with :: a -- ^ Some element, either @Html ()@ or @Html () -> Html ()@.+ with :: a -- ^ Some element, either @Html a@ or @Html a -> Html a@. -> [Attribute] -> a -- | For the contentless elements: 'Lucid.Html5.br_'-instance (Monad m,a ~ ()) => With (HtmlT m a) where+instance (Monad m) => With (HtmlT m a) where with f = \attr ->- HtmlT (do ~(f',_) <- runHtmlT f+ HtmlT (do ~(f',a) <- runHtmlT f return (\attr' m' -> f' (unionArgs (M.fromListWith (<>) (map toPair attr)) attr') m'- ,()))+ ,a)) where toPair (Attribute x) = x -- | For the contentful elements: 'Lucid.Html5.div_'-instance (Monad m,a ~ ()) => With (HtmlT m a -> HtmlT m a) where+instance (Monad m) => With (HtmlT m a -> HtmlT m a) where with f = \attr inner ->- HtmlT (do ~(f',_) <- runHtmlT (f inner)+ HtmlT (do ~(f',a) <- runHtmlT (f inner) return ((\attr' m' -> f' (unionArgs (M.fromListWith (<>) (map toPair attr)) attr') m')- ,()))+ ,a)) where toPair (Attribute x) = x -- | Union two sets of arguments and append duplicate keys.@@ -348,15 +348,15 @@ makeElement :: Monad m => Text -- ^ Name. -> HtmlT m a -- ^ Children HTML.- -> HtmlT m () -- ^ A parent element.+ -> HtmlT m a -- ^ A parent element. makeElement name = \m' ->- HtmlT (do ~(f,_) <- runHtmlT m'+ HtmlT (do ~(f,a) <- runHtmlT m' return (\attr m -> s "<" <> Blaze.fromText name <> foldlMapWithKey buildAttr attr <> s ">" <> m <> f mempty mempty <> s "</" <> Blaze.fromText name <> s ">",- ()))+ a)) -- | Make an HTML builder for elements which have no ending tag. makeElementNoEnd :: Monad m
src/Lucid/Html5.hs view
@@ -16,7 +16,7 @@ doctype_ = makeElementNoEnd "!DOCTYPE HTML" -- | @DOCTYPE@ element + @html@ element-doctypehtml_ :: Monad m => HtmlT m () -> HtmlT m ()+doctypehtml_ :: Monad m => HtmlT m a -> HtmlT m a doctypehtml_ m = do doctype_ html_ m@@ -257,6 +257,10 @@ map_ :: Term arg result => arg -> result map_ = term "map" +-- | @main@ element+main_ :: Term arg result => arg -> result+main_ = term "main"+ -- | @mark@ element mark_ :: Term arg result => arg -> result mark_ = term "mark"@@ -409,6 +413,10 @@ th_ :: Term arg result => arg -> result th_ = term "th" +-- | @template@ element+template_ :: Term arg result => arg -> result+template_ = term "template"+ -- | @thead@ element thead_ :: Term arg result => arg -> result thead_ = term "thead"@@ -1000,6 +1008,10 @@ -- | The @scope@ attribute. scope_ :: Text -> Attribute scope_ = makeAttribute "scope"++-- | The @svg@ attribute.+svg_ :: Text -> Attribute+svg_ = makeAttribute "svg" -- | The @scoped@ attribute. scoped_ :: Text -> Attribute
test/Main.hs view
@@ -93,8 +93,9 @@ it "mixed" (renderText (p_ [class_ "foo",style_ "attrib"]- (style_ "")) ==- "<p style=\"attrib\" class=\"foo\"><style></style></p>")+ (do style_ ""+ style_ "")) ==+ "<p style=\"attrib\" class=\"foo\"><style></style><style></style></p>") it "no closing" (renderText (p_ [class_ "foo"] (input_ [])) == "<p class=\"foo\"><input></p>")