-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Generic programming for the web -- -- This package implements generic functions for web programming. Based -- on the regular library [1], we provide generic functions for -- generating HTML, Formlets, and JSON. For a -- larger example, see the Example.lhs [2] file on github. -- -- 1. http://hackage.haskell.org/package/regular -- -- 2. -- http://github.com/chriseidhof/regular-web/blob/master/Example.lhs -- -- Example -- -- Consider the following datatypes: -- --
-- data Person = Person -- _name :: String -- , _age :: Int -- , _isMale :: Bool -- , _place :: Place -- -- data Place = Place -- _city :: String -- , _country :: String -- , _continent :: String ---- -- We can now derive a Regular instance for the Person -- datatype using Template Haskell: -- --
-- $(deriveAll ''Place "PFPlace") -- $(deriveAll ''Person "PFPerson") ---- --
-- type instance PF Place = PFPlace -- type instance PF Person = PFPerson ---- -- We can construct an example person: -- --
-- location :: Place -- location = Place "Utrecht" "The Netherlands" "Europe" -- chris :: Person -- chris = Person "chris" 25 True location ---- --
-- And, as an example, we can generate |HTML| and |JSON| values: ---- --
-- locationHtml :: X.Html -- locationHtml = ghtml location ---- --
-- personHtml :: X.Html -- personHtml = ghtml chris ---- --
-- locationJSON :: JSValue -- locationJSON = gto location --@package regular-web @version 0.1 -- | Summary: Functions for generating HTML. module Generics.Regular.Views -- | The function ghtml converts an a value into Html ghtml :: (Regular a, GHtml (PF a)) => a -> Html -- | The class Html converts a simple value a into Html. class Html a html :: (Html a) => a -> Html -- | The class GHtml converts a simple value a into Html. class GHtml f instance (Selector s, GTable f) => GTable (S s f) instance (GTable f, GTable g) => GTable (f :*: g) instance (Table a) => GTable (K a) instance (Constructor c, GTable f) => GTable (C c f) instance GTable I instance Table Bool instance Table Int instance Table String instance Table Float instance (Selector s, GHtml f) => GHtml (S s f) instance (GHtml f, GHtml g) => GHtml (f :+: g) instance (GHtml f, GHtml g) => GHtml (f :*: g) instance (Html a) => GHtml (K a) instance (Constructor c, GHtml f) => GHtml (C c f) instance GHtml I instance Html String instance Html Bool instance Html Int instance Html Float -- | Generic generation of JSON values. Note that the generic -- functions are only defined for record datatypes that contain a single -- constructor. -- -- The code that is generated by gto should be parseable by -- gfrom. module Generics.Regular.JSON -- | The function gfrom tries to parse a JSValue. The -- Result datatype is used for error-messages if parsing fails. gfrom :: (Regular a, GJSON (PF a)) => JSValue -> Result a -- | The function gto generates a JSValue for all types that -- are an instance of GJSON. gto :: (Regular a, GJSON (PF a)) => a -> JSValue -- | This class is used for both generation and parsing of JSON. class GJSON f instance (GJSON f) => GJSON (C c f) instance (Selector s, GJSON f) => GJSON (S s f) instance (GJSON (S s f), GJSON g) => GJSON (S s f :*: g) instance (JSON a) => GJSON (K a) instance GJSON U -- | Generic generation of formlets -- http://hackage.haskell.org/package/formlets. These functions -- are only defined for record datatypes that contain a single -- constructor. -- -- Consider the datatype Person: -- --
-- data Person = Person {
-- _name :: String
-- , _age :: Int
-- , _isMale :: Bool
-- } deriving (Show, Eq)
--
--
-- We prefix all our fields with an underscore (_), so that our
-- datatype will play nice with fclabels.
--
-- -- $(deriveAll ''Person "PFPerson") ---- --
-- type instance PF Person = PFPerson ---- -- We can construct an example person: -- --
-- chris :: Person -- chris = Person "chris" 25 True ---- --
-- personForm :: XFormlet Identity Person -- personForm = gformlet ---- -- We can print formHtml to get the Html of the form -- with the chris value already filled in: -- --
-- formHtml :: X.Html -- (_, Identity formHtml, _) = F.runFormState [] (personForm (Just chris)) --module Generics.Regular.Formlets gform :: (Regular a, GFormlet (PF a), Functor m, Applicative m, Monad m) => Maybe a -> XForm m a gformlet :: (Regular a, GFormlet (PF a), Functor m, Applicative m, Monad m) => XFormlet m a class GFormlet f type XForm m a = XHtmlForm m a type XFormlet m a = XHtmlFormlet m a -- | Generic forms almost never match the real world. If you want to change -- a generic form, you can either implement it from scratch, or use the -- projectedForm function. -- -- As an example, we will to remove the age field from the form, and -- change the _isMale field to a Yes/No choice instead of a True/False -- choice. The datatype YesNo is defined in this module. -- --
-- data PersonView = PersonView {
-- __name :: String
-- , __isMale :: YesNo
-- }
--
--
-- -- $(deriveAll ''PersonView "PFPersonView") -- type instance PF PersonView = PFPersonView ---- -- We can now use fclabels to convert back and forth between -- Person and PersonView. First, we use Template -- Haskell to generate some accessor functions: -- --
-- $(mkLabels [''Person]) ---- -- This is the bidirectional function between Person and -- PersonView. How to write such a function is explained in the -- well-documented fclabels package at -- http://hackage.haskell.org/package/fclabels. -- --
-- toView :: Person :-> PersonView -- toView = Label (PersonView <$> __name `for` name <*> __isMale `for` (boolToYesNo `iso` isMale)) ---- -- Now that we have a function with type Person :-> -- PersonView, we can render a form for personView and -- update the original person. Note that the argument is not a -- Maybe value, in contrast with the gformlet function. -- --
-- personForm' :: Person -> XForm Identity Person -- personForm' = projectedForm toView ---- --
-- formHtml' :: X.Html -- (_, Identity formHtml', _) = F.runFormState [] (personForm' chris) --projectedForm :: (Regular a, GFormlet (PF a), Applicative m, Monad m) => (b :-> a) -> b -> XForm m b class Formlet a formlet :: (Formlet a, Functor m, Applicative m, Monad m) => XFormlet m a -- | This datatype is used to display Bool values as Yes or -- No. data YesNo Yes :: YesNo No :: YesNo -- | This is an fclabels function that converts between -- Bool and YesNo values. boolToYesNo :: Bool :<->: YesNo instance Eq YesNo instance Show YesNo instance Bounded YesNo instance Enum YesNo instance Formlet YesNo instance (Selector s, GFormlet f) => GFormlet (S s f) instance (GFormlet (S s f), GFormlet g) => GFormlet (S s f :*: g) instance (Formlet a) => GFormlet (K a) instance (Constructor c, GFormlet f) => GFormlet (C c f) instance Formlet String instance Formlet Int instance Formlet Bool