Recent hamlet changes

This commit is contained in:
Michael Snoyman 2010-04-16 11:58:33 -07:00
parent 5f7668334a
commit e9a8b43595
5 changed files with 26 additions and 40 deletions

View File

@ -20,9 +20,10 @@ import Yesod.Response
import Yesod.Handler import Yesod.Handler
import Data.Convertible.Text import Data.Convertible.Text
import Data.Object import Data.Object
import Control.Arrow ((***))
data PageContent url = PageContent data PageContent url = PageContent
{ pageTitle :: IO HtmlContent { pageTitle :: HtmlContent
, pageHead :: Hamlet url IO () , pageHead :: Hamlet url IO ()
, pageBody :: Hamlet url IO () , pageBody :: Hamlet url IO ()
} }
@ -54,21 +55,18 @@ instance Monad m
%ul %ul
$forall s' s $forall s' s
%li ^s^|] %li ^s^|]
s' _ = return $ fromList $ map cs s s' _ = map cs s
convertSuccess (Mapping m) = template () where convertSuccess (Mapping m) = template () where
template :: Monad m => () -> Hamlet url m () template :: Monad m => () -> Hamlet url m ()
template = [$hamlet| template = [$hamlet|
%dl %dl
$forall pairs pair $forall pairs pair
%dt $pair.key$ %dt $pair.fst$
%dd ^pair.val^|] %dd ^pair.snd^|]
pairs _ = return $ fromList $ map go m pairs _ = map (cs *** cs) m
go (k, v) = Pair (return $ cs k) $ cs v
instance ConvertSuccess String HtmlContent where instance ConvertSuccess String HtmlContent where
convertSuccess = Unencoded . cs convertSuccess = Unencoded . cs
data Pair url m = Pair { key :: m HtmlContent, val :: Hamlet url m () }
type HtmlObject = Object String HtmlContent type HtmlObject = Object String HtmlContent
instance ConvertSuccess (Object String String) HtmlObject where instance ConvertSuccess (Object String String) HtmlObject where

View File

@ -113,9 +113,9 @@ authOpenidForm = do
simpleApplyLayout "Log in via OpenID" html simpleApplyLayout "Log in via OpenID" html
where where
urlForward _ = error "FIXME urlForward" urlForward _ = error "FIXME urlForward"
hasMessage = return . not . null hasMessage = not . null
message [] = return $ Encoded $ cs "" message [] = cs ""
message (m:_) = return $ Unencoded $ cs m message (m:_) = cs m
template = [$hamlet| template = [$hamlet|
$if hasMessage $if hasMessage
%p.message $message$ %p.message $message$

View File

@ -17,7 +17,7 @@ import Yesod.Handler hiding (badMethod)
import qualified Data.ByteString as B import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as B8 import qualified Data.ByteString.Char8 as B8
import Data.Convertible.Text import Data.Convertible.Text
import Text.Hamlet.Monad (fromList) import Control.Arrow ((***))
import Data.Maybe (fromMaybe) import Data.Maybe (fromMaybe)
import Web.Mime import Web.Mime
@ -83,7 +83,7 @@ simpleApplyLayout :: Yesod y
-> Handler y ChooseRep -> Handler y ChooseRep
simpleApplyLayout t b = do simpleApplyLayout t b = do
let pc = PageContent let pc = PageContent
{ pageTitle = return $ Unencoded $ cs t { pageTitle = cs t
, pageHead = return () , pageHead = return ()
, pageBody = b , pageBody = b
} }
@ -105,7 +105,7 @@ defaultErrorHandler NotFound = do
%p $helper$ %p $helper$
|] r |] r
where where
helper = return . Unencoded . cs . W.pathInfo helper = Unencoded . cs . W.pathInfo
defaultErrorHandler PermissionDenied = defaultErrorHandler PermissionDenied =
simpleApplyLayout "Permission Denied" $ [$hamlet| simpleApplyLayout "Permission Denied" $ [$hamlet|
%h1 Permission denied|] () %h1 Permission denied|] ()
@ -114,30 +114,21 @@ defaultErrorHandler (InvalidArgs ia) =
%h1 Invalid Arguments %h1 Invalid Arguments
%dl %dl
$forall ias pair $forall ias pair
%dt $pair.key$ %dt $pair.fst$
%dd $pair.val$ %dd $pair.snd$
|] () |] ()
where where
ias _ = return $ fromList $ map go ia ias _ = map (cs *** cs) ia
go (k, v) = Pair (return $ Unencoded $ cs k)
(return $ Unencoded $ cs v)
defaultErrorHandler (InternalError e) = defaultErrorHandler (InternalError e) =
simpleApplyLayout "Internal Server Error" $ [$hamlet| simpleApplyLayout "Internal Server Error" $ [$hamlet|
%h1 Internal Server Error %h1 Internal Server Error
%p $message$ %p $cs$
|] e |] e
where
message :: String -> IO HtmlContent
message = return . Unencoded . cs
defaultErrorHandler (BadMethod m) = defaultErrorHandler (BadMethod m) =
simpleApplyLayout "Bad Method" $ [$hamlet| simpleApplyLayout "Bad Method" $ [$hamlet|
%h1 Method Not Supported %h1 Method Not Supported
%p Method "$m'$" not supported %p Method "$cs$" not supported
|] () |] m
where
m' _ = return $ Unencoded $ cs m
data Pair m k v = Pair { key :: m k, val :: m v }
toWaiApp :: Yesod y => y -> IO W.Application toWaiApp :: Yesod y => y -> IO W.Application
toWaiApp a = do toWaiApp a = do

View File

@ -15,21 +15,18 @@ mkYesod "Ham" [$parseRoutes|
instance Yesod Ham where instance Yesod Ham where
approot _ = "http://localhost:3000" approot _ = "http://localhost:3000"
data NextLink m = NextLink { nextLink :: m HamRoutes } data NextLink = NextLink { nextLink :: HamRoutes }
nl :: Monad m => HamRoutes -> NextLink m template :: Monad m => NextLink -> Hamlet HamRoutes m ()
nl = NextLink . return
template :: Monad m => NextLink m -> Hamlet HamRoutes m ()
template = [$hamlet| template = [$hamlet|
%a!href=@nextLink@ Next page %a!href=@nextLink@ Next page
|] |]
getHomepage :: Handler Ham RepHtml getHomepage :: Handler Ham RepHtml
getHomepage = hamletToRepHtml $ template $ nl $ Another 1 getHomepage = hamletToRepHtml $ template $ NextLink $ Another 1
getAnother :: Integer -> Handler Ham RepHtml getAnother :: Integer -> Handler Ham RepHtml
getAnother i = hamletToRepHtml $ template $ nl next getAnother i = hamletToRepHtml $ template $ NextLink next
where where
next = case i of next = case i of
5 -> Homepage 5 -> Homepage

View File

@ -36,13 +36,13 @@ template = [$hamlet|
|] |]
data TempArgs url m = TempArgs data TempArgs url m = TempArgs
{ hasYaml :: m Bool { hasYaml :: Bool
, yaml :: Hamlet url m () , yaml :: Hamlet url m ()
} }
getHomepage :: Handler PY RepHtml getHomepage :: Handler PY RepHtml
getHomepage = hamletToRepHtml getHomepage = hamletToRepHtml
$ template $ TempArgs (return False) (return ()) $ template $ TempArgs False (return ())
--FIXMEpostHomepage :: Handler PY RepHtmlJson --FIXMEpostHomepage :: Handler PY RepHtmlJson
postHomepage :: Handler PY RepHtml postHomepage :: Handler PY RepHtml
@ -53,13 +53,13 @@ postHomepage = do
Nothing -> invalidArgs [("yaml", "Missing input")] Nothing -> invalidArgs [("yaml", "Missing input")]
Just x -> return x Just x -> return x
so <- liftIO $ decode $ B.concat $ L.toChunks $ fileContent fi so <- liftIO $ decode $ B.concat $ L.toChunks $ fileContent fi
{- {- FIXME
let ho' = fmap Text to let ho' = fmap Text to
templateHtmlJson "pretty-yaml" ho' $ \ho -> templateHtmlJson "pretty-yaml" ho' $ \ho ->
return . setHtmlAttrib "yaml" (Scalar $ cs ho :: HtmlObject) return . setHtmlAttrib "yaml" (Scalar $ cs ho :: HtmlObject)
-} -}
let ho = cs (so :: StringObject) :: HtmlObject let ho = cs (so :: StringObject) :: HtmlObject
hamletToRepHtml $ template $ TempArgs (return True) (cs ho) hamletToRepHtml $ template $ TempArgs True (cs ho)
main :: IO () main :: IO ()
main = do main = do