digestive-functors 0.1.0.0 → 0.1.0.1
raw patch · 4 files changed
+55/−3 lines, 4 filesPVP: minor bump suggested
API additions: PVP suggests at least a minor version bump
API changes (from Hackage documentation)
+ Text.Digestive.Forms: inputChoices :: (Monad m, Functor m, FormInput i f, Monoid v, Eq a) => (FormId -> String -> Bool -> a -> v) -> [a] -> [a] -> Form m i e v [a]
+ Text.Digestive.Forms: inputText :: (Monad m, Functor m, FormInput i f) => (FormId -> Maybe Text -> v) -> Maybe Text -> Form m i e v Text
+ Text.Digestive.Forms.Html: viewHtml :: Monad m => a -> Form m i e (FormHtml a) ()
+ Text.Digestive.Transform: required :: Monad m => e -> Transformer m e (Maybe a) a
Files
- digestive-functors.cabal +1/−1
- src/Text/Digestive/Forms.hs +38/−2
- src/Text/Digestive/Forms/Html.hs +9/−0
- src/Text/Digestive/Transform.hs +7/−0
digestive-functors.cabal view
@@ -1,5 +1,5 @@ Name: digestive-functors-Version: 0.1.0.0+Version: 0.1.0.1 Synopsis: A general way to consume input using applicative functors Description: Digestive functors is a library to generate and process HTML forms. You can find an introduction here:
src/Text/Digestive/Forms.hs view
@@ -3,9 +3,11 @@ module Text.Digestive.Forms ( FormInput (..) , inputString+ , inputText , inputRead , inputBool , inputChoice+ , inputChoices , inputFile , inputList ) where@@ -14,10 +16,10 @@ import Control.Monad (mplus) import Control.Monad.State (put, get) import Data.Monoid (Monoid, mappend, mconcat)-import Data.Maybe (fromMaybe, listToMaybe)+import Data.Maybe (fromMaybe, listToMaybe, mapMaybe) import Data.Text (Text)-import qualified Data.Text as T (pack)+import qualified Data.Text as T (pack, empty) import Text.Digestive.Common import Text.Digestive.Types@@ -61,6 +63,15 @@ toView _ inp def = (getInputString =<< inp) `mplus` def toResult = Ok . fromMaybe "" . (getInputString =<<) +inputText :: (Monad m, Functor m, FormInput i f)+ => (FormId -> Maybe Text -> v) -- ^ View constructor+ -> Maybe Text -- ^ Default value+ -> Form m i e v Text -- ^ Resulting form+inputText = input toView toResult+ where+ toView _ inp def = (getInputText =<< inp) `mplus` def+ toResult = Ok . fromMaybe T.empty . (getInputText =<<)+ inputRead :: (Monad m, Functor m, FormInput i f, Read a, Show a) => (FormId -> Maybe String -> v) -- ^ View constructor -> e -- ^ Error when no read@@ -98,6 +109,31 @@ where ids id' = map (((show id' ++ "-") ++) . show) [1 .. length choices] toView' id' inp key x = toView id' key (inp == x) x++-- An input element that allows multiple selections, such as+-- checkboxes or multiple-select boxes.+--+-- When multiple results are submitted, they should all have the same+-- name attribute.+inputChoices :: (Monad m, Functor m, FormInput i f, Monoid v, Eq a)+ => (FormId -> String -> Bool -> a -> v) -- ^ Choice constructor+ -> [a] -- ^ Default choices+ -> [a] -- ^ Choices+ -> Form m i e v [a] -- ^ Resulting form+inputChoices toView defaults choices = Form $ do+ inputKeys <- maybe [] getInputStrings <$> getFormInput+ id' <- getFormId+ formInput <- isFormInput+ let -- Find the actual input, based on the key, or use the default input+ inps = if formInput + then mapMaybe (\inputKey -> lookup inputKey $ zip (ids id') choices) inputKeys+ else defaults+ -- Apply the toView' function to all choices+ view' = mconcat $ zipWith (toView' id' inps) (ids id') choices+ return (View (const view'), Ok inps)+ where+ ids id' = map (((show id' ++ "-") ++) . show) [1 .. length choices]+ toView' id' inps key x = toView id' key (x `elem` inps) x inputFile :: (Monad m, Functor m, FormInput i f) => (FormId -> v) -- ^ View constructor
src/Text/Digestive/Forms/Html.hs view
@@ -6,6 +6,7 @@ , FormHtml (..) , createFormHtml , createFormHtmlWith+ , viewHtml , applyClasses , defaultHtmlConfig , emptyHtmlConfig@@ -18,6 +19,8 @@ import Control.Applicative ((<*>), pure) import Control.Arrow ((&&&)) +import Text.Digestive.Types (Form, view)+ -- | Settings for classes in generated HTML. -- data FormHtmlConfig = FormHtmlConfig@@ -69,6 +72,12 @@ -- createFormHtmlWith :: FormEncType -> (FormHtmlConfig -> a) -> FormHtml a createFormHtmlWith = FormHtml++-- | A shortcut for inserting HTML to the view, defined as a combination of+-- 'view' and 'createFormHtml'+--+viewHtml :: Monad m => a -> Form m i e (FormHtml a) ()+viewHtml = view . createFormHtml . const -- | Apply all classes to an HTML element. If no classes are found, nothing -- happens.
src/Text/Digestive/Transform.hs view
@@ -7,6 +7,7 @@ , transformEither , transformEitherM , transformRead+ , required ) where import Prelude hiding ((.), id)@@ -82,3 +83,9 @@ transformRead error' = transformEither $ \str -> case readsPrec 1 str of [(x, "")] -> Right x _ -> Left error'++-- | A transformer that converts 'Maybe a' to 'a'.+required :: (Monad m) => + e -- ^ error to return if value is 'Nothing'+ -> Transformer m e (Maybe a) a+required err = transformEither $ maybe (Left err) Right