File: README.md

package info (click to toggle)
haskell-lucid 2.9.11-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 152 kB
  • sloc: haskell: 1,306; makefile: 4
file content (134 lines) | stat: -rwxr-xr-x 2,874 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
lucid [![Hackage](https://img.shields.io/hackage/v/lucid.svg?style=flat)](https://hackage.haskell.org/package/lucid) [![Build Status](https://travis-ci.org/chrisdone/lucid.png)](https://travis-ci.org/chrisdone/lucid)
=====

Clear to write, read and edit DSL for writing HTML

[Documentation](http://chrisdone.github.io/lucid/)

[lucid-from-html](https://github.com/dbaynard/lucid-from-html) will convert html to the `lucid` DSL, though it is experimental.

## 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:

``` haskell
λ> "123 < 456" :: Html ()
```

``` html
123 &lt; 456
```

Elements nest by function application:

``` haskell
λ> table_ (tr_ (td_ (p_ "Hello, World!"))) :: Html ()
```

``` html
<table><tr><td><p>Hello, World!</p></td></tr></table>
```

Elements are juxtaposed via monoidal append:

``` haskell
λ> p_ "hello" <> p_ "sup" :: Html ()
```

``` html
<p>hello</p><p>sup</p>
```

Or monadic sequencing:

``` haskell
λ> div_ (do p_ "hello"; p_ "sup") :: Html ()
```

``` html
<div><p>hello</p><p>sup</p></div>
```

Attributes are set by providing an argument list:

``` haskell
λ> p_ [class_ "brand"] "Lucid Inc" :: Html ()
```

``` html
<p class="brand">Lucid Inc</p>
```

Here is a fuller example of Lucid:

``` haskell
table_ [rows_ "2"]
       (tr_ (do td_ [class_ "top",colspan_ "2",style_ "color:red"]
                    (p_ "Hello, attributes!")
                td_ "yay!"))
```

``` html
<table rows="2">
  <tr>
    <td style="color:red" colspan="2" class="top">
      <p>Hello, attributes!</p>
    </td>
    <td>yay!</td>
  </tr>
</table>
```

## Rendering

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

``` haskell
λ> renderText (p_ "Hello!")
```

``` html
"<p>Hello!</p>"
```

Or to bytes:

``` haskell
λ> renderBS (p_ [style_ "color:red"] "Hello!")
```

``` html
"<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.

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