lucid: Clear to write, read and edit DSL for HTML

[ bsd3, library, web ] [ Propose Tags ]

Clear to write, read and edit DSL for HTML. See the Lucid module for description and documentation.


[Skip to Readme]

Downloads

Note: This package has metadata revisions in the cabal description newer than included in the tarball. To unpack the package including the revisions, use 'cabal get'.

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.0, 0.1, 0.2, 0.3, 0.4, 1.0, 2.0, 2.1, 2.2, 2.3, 2.4, 2.5, 2.6.0, 2.6.1, 2.7.0, 2.8.0, 2.8.1, 2.9.0, 2.9.1, 2.9.2, 2.9.3, 2.9.4, 2.9.5, 2.9.6, 2.9.7, 2.9.8, 2.9.8.1, 2.9.9, 2.9.10, 2.9.11, 2.9.12, 2.9.12.1, 2.9.13, 2.10.0, 2.11.0, 2.11.1, 2.11.20230408 (info)
Change log CHANGELOG.md
Dependencies base (>=4.5 && <4.11), blaze-builder, bytestring, containers, mtl, text, transformers, unordered-containers [details]
License BSD-3-Clause
Copyright 2014 Chris Done
Author Chris Done
Maintainer chrisdone@gmail.com
Revised Revision 2 made by phadej at 2018-03-21T13:42:34Z
Category Web
Home page https://github.com/chrisdone/lucid
Uploaded by ChrisDone at 2014-11-25T21:07:13Z
Distributions Arch:2.11.20230408, Debian:2.9.12, Fedora:2.11.20230408, LTSHaskell:2.11.20230408, NixOS:2.11.20230408, Stackage:2.11.20230408, openSUSE:2.11.20230408
Reverse Dependencies 75 direct, 105 indirect [details]
Downloads 44380 total (181 in the last 30 days)
Rating 2.5 (votes: 3) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]

Readme for lucid-2.0

[back to package description]

lucid

Clear to write, read and edit DSL for writing HTML

Introduction

HTML terms in Lucid are written with a postfix ‘_’ to indicate data rather than code. Some examples:

p_, class_, table_, style_

See Lucid.Html5 for a complete list of Html5 combinators.

Plain text is written using the OverloadedStrings and ExtendedDefaultRules extensions, and is automatically escaped:

λ> "123 < 456" :: Html ()
123 &lt; 456

Elements nest by function application:

λ> table_ (tr_ (td_ (p_ "Hello, World!"))) :: Html ()
<table><tr><td><p>Hello, World!</p></td></tr></table>

Elements are juxtaposed via monoidal append:

λ> p_ "hello" <> p_ "sup" :: Html ()
<p>hello</p><p>sup</p>

Or monadic sequencing:

λ> div_ (do p_ "hello"; p_ "sup") :: Html ()
<div><p>hello</p><p>sup</p></div>

Attributes are set using the 'with' combinator:

λ> with p_ [class_ "brand"] "Lucid Inc" :: Html ()
<p class="brand">Lucid Inc</p>

Here is a fuller example of Lucid:

with table_ [rows_ "2"]
     (tr_ (do with td_ [class_ "top",colspan_ "2"]
                   (p_ "Hello, attributes!")
              td_ "yay!"))
<table rows="2">
  <tr>
     <td class="top" colspan="2">
       <p>Hello, attributes!</p>
     </td>
     <td>yay!</td>
  </tr>
</table>

Rendering

For proper rendering you can easily run some HTML immediately with:

λ> renderText (p_ "Hello!")
"<p>Hello!</p>"

Or to bytes:

λ> renderBS (with p_ [style_ "color:red"] "Hello!")
"<p style=\"color:red\">Hello!</p>"

For ease of use in GHCi, there is a Show instance, as demonstrated above.

If the above rendering functions aren't suited for your purpose, you can run the monad directly via execHtml and use the more low-level blaze Builder, which has a plethora of output modes in Blaze.ByteString.Builder.

See the documentation for the Lucid module for information about using it as a monad transformer.

Transforming

You can use lift to call parent monads.

λ> runReader (renderTextT (html_ (body_ (do name <- lift ask
                                            p_ [class_ "name"] (toHtml name)))))
             ("Chris" :: String)
"<html><body><p class=\"name\">Chris</p></body></html>"