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

Safe HaskellNone
LanguageHaskell98

Lucid.Base

Contents

Description

Base types and combinators.

Synopsis

Rendering

renderText :: Html a -> Text Source #

Render the HTML to a lazy Text.

This is a convenience function defined in terms of execHtmlT, runIdentity and toLazyByteString, and decodeUtf8. Check the source if you're interested in the lower-level behaviour.

renderBS :: Html a -> ByteString Source #

Render the HTML to a lazy ByteString.

This is a convenience function defined in terms of execHtmlT, runIdentity and toLazyByteString. Check the source if you're interested in the lower-level behaviour.

renderTextT :: Monad m => HtmlT m a -> m Text Source #

Render the HTML to a lazy Text, but in a monad.

This is a convenience function defined in terms of execHtmlT and toLazyByteString, and decodeUtf8. Check the source if you're interested in the lower-level behaviour.

renderBST :: Monad m => HtmlT m a -> m ByteString Source #

Render the HTML to a lazy ByteString, but in a monad.

This is a convenience function defined in terms of execHtmlT and toLazyByteString. Check the source if you're interested in the lower-level behaviour.

renderToFile :: FilePath -> Html a -> IO () Source #

Render the HTML to a lazy ByteString.

This is a convenience function defined in terms of execHtmlT, runIdentity and toLazyByteString. Check the source if you're interested in the lower-level behaviour.

Running

execHtmlT Source #

Arguments

:: Monad m 
=> HtmlT m a

The HTML to generate.

-> m Builder

The a is discarded.

Build the HTML. Analogous to execState.

You might want to use this is if you want to do something with the raw Builder. Otherwise for simple cases you can just use renderText or renderBS.

evalHtmlT Source #

Arguments

:: Monad m 
=> HtmlT m a

HTML monad to evaluate.

-> m a

Ignore the HTML output and just return the value.

Evaluate the HTML to its return value. Analogous to evalState.

Use this if you want to ignore the HTML output of an action completely and just get the result.

For using with the Html type, you'll need runIdentity e.g.

>>> runIdentity (evalHtmlT (p_ "Hello!"))
()

relaxHtmlT Source #

Arguments

:: Monad m 
=> HtmlT Identity a

The HTML generated purely.

-> HtmlT m a

Same HTML accessible in a polymorphic context.

Generalize the underlying monad.

Some builders are happy to deliver results in a pure underlying monad, here Identity, but have trouble maintaining the polymorphic type. This utility generalizes from Identity.

Combinators

makeElement Source #

Arguments

:: Monad m 
=> Text

Name.

-> HtmlT m a

Children HTML.

-> HtmlT m a

A parent element.

Make an HTML builder.

makeElementNoEnd Source #

Arguments

:: Monad m 
=> Text

Name.

-> HtmlT m ()

A parent element.

Make an HTML builder for elements which have no ending tag.

makeXmlElementNoEnd Source #

Arguments

:: Monad m 
=> Text

Name.

-> HtmlT m ()

A parent element.

Make an XML builder for elements which have no ending tag.

makeAttribute Source #

Arguments

:: Text

Attribute name.

-> Text

Attribute value.

-> Attribute 

Make an attribute builder.

Types

type Html = HtmlT Identity Source #

Simple HTML builder type. Defined in terms of HtmlT. Check out that type for instance information.

Simple use-cases will just use this type. But if you want to transformer over Reader or something, you can go and use HtmlT.

newtype HtmlT m a Source #

A monad transformer that generates HTML. Use the simpler Html type if you don't want to transform over some other monad.

Constructors

HtmlT 

Fields

Instances

MFunctor HtmlT Source # 

Methods

hoist :: Monad m => (forall a. m a -> n a) -> HtmlT m b -> HtmlT n b #

MonadTrans HtmlT Source #

Used for lift.

Methods

lift :: Monad m => m a -> HtmlT m a #

MonadReader r m => MonadReader r (HtmlT m) Source # 

Methods

ask :: HtmlT m r #

local :: (r -> r) -> HtmlT m a -> HtmlT m a #

reader :: (r -> a) -> HtmlT m a #

MonadState s m => MonadState s (HtmlT m) Source # 

Methods

get :: HtmlT m s #

put :: s -> HtmlT m () #

state :: (s -> (a, s)) -> HtmlT m a #

(Monad m, (~) * a ()) => TermRaw Text (HtmlT m a) Source #

Given children immediately, just use that and expect no attributes.

Methods

termRaw :: Text -> Text -> HtmlT m a Source #

termRawWith :: Text -> [Attribute] -> Text -> HtmlT m a Source #

Monad m => Monad (HtmlT m) Source #

Basically acts like Writer.

Methods

(>>=) :: HtmlT m a -> (a -> HtmlT m b) -> HtmlT m b #

(>>) :: HtmlT m a -> HtmlT m b -> HtmlT m b #

return :: a -> HtmlT m a #

fail :: String -> HtmlT m a #

Monad m => Functor (HtmlT m) Source #

Just re-uses Monad.

Methods

fmap :: (a -> b) -> HtmlT m a -> HtmlT m b #

(<$) :: a -> HtmlT m b -> HtmlT m a #

Monad m => Applicative (HtmlT m) Source #

Based on the monad instance.

Methods

pure :: a -> HtmlT m a #

(<*>) :: HtmlT m (a -> b) -> HtmlT m a -> HtmlT m b #

(*>) :: HtmlT m a -> HtmlT m b -> HtmlT m b #

(<*) :: HtmlT m a -> HtmlT m b -> HtmlT m a #

MonadIO m => MonadIO (HtmlT m) Source #

If you want to use IO in your HTML generation.

Methods

liftIO :: IO a -> HtmlT m a #

(Monad m, ToHtml f, (~) * a ()) => TermRaw [Attribute] (f -> HtmlT m a) Source #

Given attributes, expect more child input.

Methods

termRaw :: Text -> [Attribute] -> f -> HtmlT m a Source #

termRawWith :: Text -> [Attribute] -> [Attribute] -> f -> HtmlT m a Source #

(Monad m, (~) * f (HtmlT m a)) => Term [Attribute] (f -> HtmlT m a) Source #

Given attributes, expect more child input.

Methods

term :: Text -> [Attribute] -> f -> HtmlT m a Source #

termWith :: Text -> [Attribute] -> [Attribute] -> f -> HtmlT m a Source #

(~) (* -> *) m Identity => Show (HtmlT m a) Source #

Just calls renderText.

Methods

showsPrec :: Int -> HtmlT m a -> ShowS #

show :: HtmlT m a -> String #

showList :: [HtmlT m a] -> ShowS #

(Monad m, (~) * a ()) => IsString (HtmlT m a) Source #

We pack it via string. Could possibly encode straight into a builder. That might be faster.

Methods

fromString :: String -> HtmlT m a #

((~) * a (), Monad m) => Semigroup (HtmlT m a) Source # 

Methods

(<>) :: HtmlT m a -> HtmlT m a -> HtmlT m a #

sconcat :: NonEmpty (HtmlT m a) -> HtmlT m a #

stimes :: Integral b => b -> HtmlT m a -> HtmlT m a #

((~) * a (), Monad m) => Monoid (HtmlT m a) Source #

Monoid is right-associative, a la the Builder in it.

Methods

mempty :: HtmlT m a #

mappend :: HtmlT m a -> HtmlT m a -> HtmlT m a #

mconcat :: [HtmlT m a] -> HtmlT m a #

Monad m => With (HtmlT m a -> HtmlT m a) Source #

For the contentful elements: div_

Methods

with :: (HtmlT m a -> HtmlT m a) -> [Attribute] -> HtmlT m a -> HtmlT m a Source #

Monad m => With (HtmlT m a) Source #

For the contentless elements: br_

Methods

with :: HtmlT m a -> [Attribute] -> HtmlT m a Source #

Monad m => Term (HtmlT m a) (HtmlT m a) Source #

Given children immediately, just use that and expect no attributes.

Methods

term :: Text -> HtmlT m a -> HtmlT m a Source #

termWith :: Text -> [Attribute] -> HtmlT m a -> HtmlT m a Source #

data Attribute Source #

A simple attribute. Don't use the constructor, use makeAttribute.

Constructors

Attribute !Text !Text 

Instances

Eq Attribute Source # 
Show Attribute Source # 
Hashable Attribute Source # 
TermRaw Text Attribute Source #

Some termRaws (like style_, title_) can be used for attributes as well as elements.

Term Text Attribute Source #

Some terms (like style_, title_) can be used for attributes as well as elements.

(Monad m, ToHtml f, (~) * a ()) => TermRaw [Attribute] (f -> HtmlT m a) Source #

Given attributes, expect more child input.

Methods

termRaw :: Text -> [Attribute] -> f -> HtmlT m a Source #

termRawWith :: Text -> [Attribute] -> [Attribute] -> f -> HtmlT m a Source #

(Monad m, (~) * f (HtmlT m a)) => Term [Attribute] (f -> HtmlT m a) Source #

Given attributes, expect more child input.

Methods

term :: Text -> [Attribute] -> f -> HtmlT m a Source #

termWith :: Text -> [Attribute] -> [Attribute] -> f -> HtmlT m a Source #

Classes

class Term arg result | result -> arg where Source #

Used to construct HTML terms.

Simplest use: p_ = term "p" yields p_.

Very overloaded for three cases:

  • The first case is the basic arg of [(Text,Text)] which will return a function that wants children.
  • The second is an arg which is HtmlT m (), in which case the term accepts no attributes and just the children are used for the element.
  • Finally, this is also used for overloaded attributes, like style_ or title_. If a return type of (Text,Text) is inferred then an attribute will be made.

The instances look intimidating but actually the constraints make it very general so that type inference works well even in the presence of things like OverloadedLists and such.

Minimal complete definition

termWith

Methods

term :: Text -> arg -> result Source #

Used for constructing elements e.g. term "p" yields p_.

termWith :: Text -> [Attribute] -> arg -> result Source #

Use this if you want to make an element which inserts some pre-prepared attributes into the element.

Instances

Term Text Attribute Source #

Some terms (like style_, title_) can be used for attributes as well as elements.

(Monad m, (~) * f (HtmlT m a)) => Term [Attribute] (f -> HtmlT m a) Source #

Given attributes, expect more child input.

Methods

term :: Text -> [Attribute] -> f -> HtmlT m a Source #

termWith :: Text -> [Attribute] -> [Attribute] -> f -> HtmlT m a Source #

Monad m => Term (HtmlT m a) (HtmlT m a) Source #

Given children immediately, just use that and expect no attributes.

Methods

term :: Text -> HtmlT m a -> HtmlT m a Source #

termWith :: Text -> [Attribute] -> HtmlT m a -> HtmlT m a Source #

class TermRaw arg result | result -> arg where Source #

Same as the Term class, but will not HTML escape its children. Useful for elements like style_ or script_.

Minimal complete definition

termRawWith

Methods

termRaw :: Text -> arg -> result Source #

Used for constructing elements e.g. termRaw "p" yields p_.

termRawWith :: Text -> [Attribute] -> arg -> result Source #

Use this if you want to make an element which inserts some pre-prepared attributes into the element.

Instances

TermRaw Text Attribute Source #

Some termRaws (like style_, title_) can be used for attributes as well as elements.

(Monad m, (~) * a ()) => TermRaw Text (HtmlT m a) Source #

Given children immediately, just use that and expect no attributes.

Methods

termRaw :: Text -> Text -> HtmlT m a Source #

termRawWith :: Text -> [Attribute] -> Text -> HtmlT m a Source #

(Monad m, ToHtml f, (~) * a ()) => TermRaw [Attribute] (f -> HtmlT m a) Source #

Given attributes, expect more child input.

Methods

termRaw :: Text -> [Attribute] -> f -> HtmlT m a Source #

termRawWith :: Text -> [Attribute] -> [Attribute] -> f -> HtmlT m a Source #

class ToHtml a where Source #

Can be converted to HTML.

Minimal complete definition

toHtml, toHtmlRaw

Methods

toHtml :: Monad m => a -> HtmlT m () Source #

toHtmlRaw :: Monad m => a -> HtmlT m () Source #

Instances

ToHtml String Source # 

Methods

toHtml :: Monad m => String -> HtmlT m () Source #

toHtmlRaw :: Monad m => String -> HtmlT m () Source #

ToHtml ByteString Source #

This instance requires the ByteString to contain UTF-8 encoded text, for the toHtml method. The toHtmlRaw method doesn't care, but the overall HTML rendering methods in this module assume UTF-8.

Methods

toHtml :: Monad m => ByteString -> HtmlT m () Source #

toHtmlRaw :: Monad m => ByteString -> HtmlT m () Source #

ToHtml ByteString Source #

This instance requires the ByteString to contain UTF-8 encoded text, for the toHtml method. The toHtmlRaw method doesn't care, but the overall HTML rendering methods in this module assume UTF-8.

Methods

toHtml :: Monad m => ByteString -> HtmlT m () Source #

toHtmlRaw :: Monad m => ByteString -> HtmlT m () Source #

ToHtml Text Source # 

Methods

toHtml :: Monad m => Text -> HtmlT m () Source #

toHtmlRaw :: Monad m => Text -> HtmlT m () Source #

ToHtml Text Source # 

Methods

toHtml :: Monad m => Text -> HtmlT m () Source #

toHtmlRaw :: Monad m => Text -> HtmlT m () Source #

class With a where Source #

With an element use these attributes. An overloaded way of adding attributes either to an element accepting attributes-and-children or one that just accepts attributes. See the two instances.

Minimal complete definition

with

Methods

with :: a -> [Attribute] -> a Source #

With the given element(s), use the given attributes.

Instances

Monad m => With (HtmlT m a -> HtmlT m a) Source #

For the contentful elements: div_

Methods

with :: (HtmlT m a -> HtmlT m a) -> [Attribute] -> HtmlT m a -> HtmlT m a Source #

Monad m => With (HtmlT m a) Source #

For the contentless elements: br_

Methods

with :: HtmlT m a -> [Attribute] -> HtmlT m a Source #