| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
module Lucid |
| 14 |
(-- * Intro |
| 15 |
-- $intro |
| 16 |
renderText |
| 17 |
,renderBS |
| 18 |
,renderTextT |
| 19 |
,renderBST |
| 20 |
,renderToFile |
| 21 |
-- * Running |
| 22 |
-- $running |
| 23 |
,execHtmlT |
| 24 |
,evalHtmlT |
| 25 |
,runHtmlT |
| 26 |
-- * Types |
| 27 |
,Html |
| 28 |
,HtmlT |
| 29 |
,Attribute |
| 30 |
-- * Classes |
| 31 |
-- $overloaded |
| 32 |
,Term(..) |
| 33 |
,ToHtml(..) |
| 34 |
,With(..) |
| 35 |
-- * Re-exports |
| 36 |
,module Lucid.Html5) |
| 37 |
where |
| 38 |
|
| 39 |
import Lucid.Base |
| 40 |
import Lucid.Html5 |
| 41 |
|
| 42 |
-- $intro |
| 43 |
-- |
| 44 |
-- HTML terms in Lucid are written with a postfix ‘@_@’ to indicate data |
| 45 |
-- rather than code. Some examples: |
| 46 |
-- |
| 47 |
-- 'p_', 'class_', 'table_', 'style_' |
| 48 |
-- |
| 49 |
-- Note: If you're testing in the REPL you need to add a type annotation to |
| 50 |
-- indicate that you want HTML. In normal code your top-level |
| 51 |
-- declaration signatures handle that. |
| 52 |
-- |
| 53 |
-- For GHCi: |
| 54 |
-- |
| 55 |
-- @ |
| 56 |
-- :set -XOverloadedStrings -XExtendedDefaultRules@ |
| 57 |
-- import Lucid |
| 58 |
-- @ |
| 59 |
-- |
| 60 |
-- In a module: @{-\# LANGUAGE OverloadedStrings, ExtendedDefaultRules \#-}@ |
| 61 |
-- |
| 62 |
-- Plain text is written like this, and is automatically escaped: |
| 63 |
-- |
| 64 |
-- >>> "123 < 456" :: Html () |
| 65 |
-- 123 < 456 |
| 66 |
-- |
| 67 |
-- Except some elements, like 'script_': |
| 68 |
-- |
| 69 |
-- >>> script_ "alert('Hello!' > 12)" :: Html () |
| 70 |
-- <script>alert('Hello!' > 12)</script> |
| 71 |
-- |
| 72 |
-- Elements nest by function application: |
| 73 |
-- |
| 74 |
-- >>> table_ (tr_ (td_ (p_ "Hello, World!"))) :: Html () |
| 75 |
-- <table><tr><td><p>Hello, World!</p></td></tr></table> |
| 76 |
-- |
| 77 |
-- Elements are juxtaposed via monoidal append (remember to import "Data.Monoid"): |
| 78 |
-- |
| 79 |
-- >>> p_ "hello" <> p_ "sup" :: Html () |
| 80 |
-- <p>hello</p><p>sup</p> |
| 81 |
-- |
| 82 |
-- Or monadic sequencing: |
| 83 |
-- |
| 84 |
-- >>> div_ (do p_ "hello"; p_ "sup") :: Html () |
| 85 |
-- <div><p>hello</p><p>sup</p></div> |
| 86 |
-- |
| 87 |
-- Attributes are set by providing an argument list: |
| 88 |
-- |
| 89 |
-- >>> p_ [class_ "brand"] "Lucid Inc" :: Html () |
| 90 |
-- <p class="brand">Lucid Inc</p> |
| 91 |
-- |
| 92 |
-- >>> p_ [data_ "zot" "foo",checked_] "Go!" :: Html () |
| 93 |
-- <p data-zot="foo" checked>go</p> |
| 94 |
-- |
| 95 |
-- Attribute and element terms are not conflicting: |
| 96 |
-- |
| 97 |
-- >>> style_ [style_ "inception"] "Go deeper." :: Html () |
| 98 |
-- <style style="inception">Go deeper.</style> |
| 99 |
-- |
| 100 |
-- Here is a fuller example of Lucid: |
| 101 |
-- |
| 102 |
-- @ |
| 103 |
-- table_ [rows_ "2"] |
| 104 |
-- (tr_ (do td_ [class_ "top",colspan_ "2",style_ "color:red"] |
| 105 |
-- (p_ "Hello, attributes!") |
| 106 |
-- td_ "yay!")) |
| 107 |
-- @ |
| 108 |
-- |
| 109 |
-- Elements (and some attributes) are variadic and overloaded, see the |
| 110 |
-- 'Term' class for more explanation about that. |
| 111 |
-- |
| 112 |
-- For proper rendering you can easily run some HTML immediately with: |
| 113 |
-- |
| 114 |
-- >>> renderText (p_ "Hello!") |
| 115 |
-- > "<p>Hello!</p>" |
| 116 |
-- |
| 117 |
-- >>> renderBS (p_ [style_ "color:red"] "Hello!") |
| 118 |
-- "<p style=\"color:red\">Hello!</p>" |
| 119 |
-- |
| 120 |
-- For ease of use in GHCi, there is a 'Show' instance, as |
| 121 |
-- demonstrated above. |
| 122 |
|
| 123 |
-- $overloaded |
| 124 |
-- |
| 125 |
-- To support convenient use of HTML terms, HTML terms are |
| 126 |
-- overloaded. Here are the following types possible for an element |
| 127 |
-- term accepting attributes and/or children: |
| 128 |
-- |
| 129 |
-- @ |
| 130 |
-- p_ :: Term arg result => arg -> result |
| 131 |
-- p_ :: Monad m => [Attribute] -> HtmlT m () -> HtmlT m () |
| 132 |
-- p_ :: Monad m => HtmlT m () -> HtmlT m () |
| 133 |
-- @ |
| 134 |
-- |
| 135 |
-- The first is the generic form. The latter two are the possible |
| 136 |
-- types for an element. |
| 137 |
-- |
| 138 |
-- Elements that accept no content are always concrete: |
| 139 |
-- |
| 140 |
-- @ |
| 141 |
-- input_ :: Monad m => [Attribute] -> HtmlT m () |
| 142 |
-- @ |
| 143 |
-- |
| 144 |
-- And some elements share the same name as attributes, so you can |
| 145 |
-- also overload them as attributes: |
| 146 |
-- |
| 147 |
-- @ |
| 148 |
-- style_ :: TermRaw arg result => arg -> result |
| 149 |
-- style_ :: Monad m => [Attribute] -> Text -> HtmlT m () |
| 150 |
-- style_ :: Monad m => Text -> HtmlT m () |
| 151 |
-- style_ :: Text -> Attribute |
| 152 |
-- @ |
| 153 |
|
| 154 |
-- $running |
| 155 |
-- |
| 156 |
-- If the above rendering functions aren't suited for your purpose, |
| 157 |
-- you can run the monad directly and use the more low-level blaze |
| 158 |
-- `Builder`, which has a plethora of output modes in |
| 159 |
-- "Blaze.ByteString.Builder". |