yesod-colonnade (empty) → 0.1
raw patch · 4 files changed
+183/−0 lines, 4 filesdep +basedep +colonnadedep +textsetup-changed
Dependencies added: base, colonnade, text, yesod-core
Files
- LICENSE +30/−0
- Setup.hs +2/−0
- src/Yesod/Colonnade.hs +123/−0
- yesod-colonnade.cabal +28/−0
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Andrew Martin (c) 2016++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of Andrew Martin nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ src/Yesod/Colonnade.hs view
@@ -0,0 +1,123 @@+{-# LANGUAGE TemplateHaskell #-}+{-# LANGUAGE QuasiQuotes #-}++module Yesod.Colonnade+ ( table+ , listItems+ , Cell(..)+ , cell+ , stringCell+ , textCell+ , builderCell+ ) where++import Yesod.Core+import Colonnade.Types+import Data.Text (Text)+import Control.Monad+import Data.String (IsString(..))+import qualified Colonnade.Encoding as Encoding+import qualified Data.Text.Lazy as LText+import qualified Data.Text.Lazy.Builder as TBuilder++data Cell site = Cell+ { cellAttrs :: ![(Text,Text)]+ , cellContents :: !(WidgetT site IO ())+ }++instance IsString (Cell site) where+ fromString = stringCell++cell :: WidgetT site IO () -> Cell site+cell = Cell []++stringCell :: String -> Cell site+stringCell = cell . fromString++textCell :: Text -> Cell site+textCell = cell . toWidget . toHtml++builderCell :: TBuilder.Builder -> Cell site+builderCell = cell . toWidget . toHtml . LText.toStrict . TBuilder.toLazyText++-- | This determines the attributes that are added+-- to the individual @li@s by concatenating the header\'s+-- attributes with the data\'s attributes.+listItems :: Foldable f+ => (WidgetT site IO () -> WidgetT site IO ())+ -- ^ Wrapper for items, often @ul@+ -> (WidgetT site IO () -> WidgetT site IO () -> WidgetT site IO ())+ -- ^ Combines header with data+ -> Encoding Headed (Cell site) a+ -- ^ How to encode data as a row+ -> f a+ -- ^ Rows of data+ -> WidgetT site IO ()+listItems ulWrap combine enc xs =+ forM_ xs $ ulWrap . Encoding.runBothMonadic_ enc+ (\(Cell ha hc) (Cell ba bc) ->+ li (ha ++ ba) (combine hc bc)+ )++-- | If you are using the bootstrap css framework, then you may want+-- to call this with the first argument as:+--+-- > table [("class","table table-striped")] ...+table :: Foldable f+ => [(Text,Text)] -- ^ Attributes of @table@ element+ -> Encoding Headed (Cell site) a -- ^ How to encode data as a row+ -> f a -- ^ Rows of data+ -> WidgetT site IO ()+table attrs enc xs = tableEl attrs $ do+ thead [] $ Encoding.runHeaderMonadic enc (widgetFromCell th)+ tableBody enc xs++tableHeadless :: Foldable f+ => [(Text,Text)] -- ^ Attributes of @table@ element+ -> Encoding Headless (Cell site) a -- ^ How to encode data as a row+ -> f a -- ^ Rows of data+ -> WidgetT site IO ()+tableHeadless attrs enc xs = tableEl attrs $ tableBody enc xs++tableBody :: Foldable f+ => Encoding h (Cell site) a -- ^ How to encode data as a row+ -> f a -- ^ Rows of data+ -> WidgetT site IO ()+tableBody enc xs = tbody [] $ do+ forM_ xs $ \x -> do+ tr [] $ Encoding.runRowMonadic enc (widgetFromCell td) x++widgetFromCell ::+ ([(Text,Text)] -> WidgetT site IO () -> WidgetT site IO ())+ -> Cell site+ -> WidgetT site IO ()+widgetFromCell f (Cell attrs contents) =+ f attrs contents++tr,tbody,thead,tableEl,td,th,ul,li ::+ [(Text,Text)] -> WidgetT site IO () -> WidgetT site IO ()+tableEl str b = [whamlet|+ <table *{str}>^{b}+|]+thead str b = [whamlet|+ <thead *{str}>^{b}+|]+tbody str b = [whamlet|+ <tbody *{str}>^{b}+|]+tr str b = [whamlet|+ <tr *{str}>^{b}+|]+th str b = [whamlet|+ <th *{str}>^{b}+|]+td str b = [whamlet|+ <td *{str}>^{b}+|]+ul str b = [whamlet|+ <ul *{str}>^{b}+|]+li str b = [whamlet|+ <li *{str}>^{b}+|]+
+ yesod-colonnade.cabal view
@@ -0,0 +1,28 @@+name: yesod-colonnade+version: 0.1+synopsis: Helper functions for using yesod with colonnade+description: Yesod and colonnade+homepage: https://github.com/andrewthad/colonnade#readme+license: BSD3+license-file: LICENSE+author: Andrew Martin+maintainer: andrew.thaddeus@gmail.com+copyright: 2016 Andrew Martin+category: web+build-type: Simple+cabal-version: >=1.10++library+ hs-source-dirs: src+ exposed-modules:+ Yesod.Colonnade+ build-depends:+ base >= 4.7 && < 5+ , colonnade >= 0.4.6 && < 0.5+ , yesod-core >= 1.4.0 && < 1.5+ , text >= 1.0 && < 1.3+ default-language: Haskell2010++source-repository head+ type: git+ location: https://github.com/andrewthad/colonnade