Use macro to chose quasyquote syntax. Reduce clutter caused by

This commit is contained in:
Alexey Khudyakov 2011-06-05 01:24:54 +04:00
parent 0221258fec
commit 7cd2f9f6c4
10 changed files with 47 additions and 103 deletions

View File

@ -22,6 +22,8 @@ module Yesod.Auth
, requireAuth , requireAuth
) where ) where
#include "qq.h"
import Yesod.Core import Yesod.Core
import Yesod.Persist import Yesod.Persist
import Yesod.Json import Yesod.Json
@ -91,11 +93,7 @@ mkYesodSub "Auth"
[ ClassP ''YesodAuth [VarT $ mkName "master"] [ ClassP ''YesodAuth [VarT $ mkName "master"]
] ]
#define STRINGS *Texts #define STRINGS *Texts
#if GHC7 [QQ(parseRoutes)|
[parseRoutes|
#else
[$parseRoutes|
#endif
/check CheckR GET /check CheckR GET
/login LoginR GET /login LoginR GET
/logout LogoutR GET POST /logout LogoutR GET POST
@ -117,13 +115,7 @@ setCreds doRedirects creds = do
case authRoute y of case authRoute y of
Nothing -> do Nothing -> do
rh <- defaultLayout rh <- defaultLayout
#if GHC7 [QQ(hamlet)| <h1>Invalid login |]
[hamlet|
#else
[$hamlet|
#endif
<h1>Invalid login
|]
sendResponse rh sendResponse rh
Just ar -> do Just ar -> do
setMessageI Msg.InvalidLogin setMessageI Msg.InvalidLogin
@ -145,11 +137,7 @@ getCheckR = do
addHtml $ html creds) (json' creds) addHtml $ html creds) (json' creds)
where where
html creds = html creds =
#if GHC7 [QQ(hamlet)|
[hamlet|
#else
[$hamlet|
#endif
<h1>Authentication Status <h1>Authentication Status
$maybe _ <- creds $maybe _ <- creds
<p>Logged in. <p>Logged in.

View File

@ -8,6 +8,8 @@ module Yesod.Auth.Dummy
( authDummy ( authDummy
) where ) where
#include "qq.h"
import Yesod.Auth import Yesod.Auth
import Yesod.Form (runInputPost, textField, ireq) import Yesod.Form (runInputPost, textField, ireq)
import Yesod.Handler (notFound) import Yesod.Handler (notFound)
@ -23,11 +25,7 @@ authDummy =
dispatch _ _ = notFound dispatch _ _ = notFound
url = PluginR "dummy" [] url = PluginR "dummy" []
login authToMaster = login authToMaster =
#if GHC7 [QQ(hamlet)|
[hamlet|
#else
[$hamlet|
#endif
<form method="post" action="@{authToMaster url}"> <form method="post" action="@{authToMaster url}">
\Your new identifier is: \Your new identifier is:
<input type="text" name="ident"> <input type="text" name="ident">

View File

@ -14,6 +14,8 @@ module Yesod.Auth.Email
, setpassR , setpassR
) where ) where
#include "qq.h"
import Network.Mail.Mime (randomString) import Network.Mail.Mime (randomString)
import Yesod.Auth import Yesod.Auth
import System.Random import System.Random
@ -81,11 +83,7 @@ authEmail =
AuthPlugin "email" dispatch $ \tm -> do AuthPlugin "email" dispatch $ \tm -> do
y <- lift getYesod y <- lift getYesod
l <- lift languages l <- lift languages
#if GHC7 [QQ(whamlet)|
[whamlet|
#else
[$whamlet|
#endif
<form method="post" action="@{tm loginR}"> <form method="post" action="@{tm loginR}">
<table> <table>
<tr> <tr>
@ -119,11 +117,7 @@ getRegisterR = do
defaultLayout $ do defaultLayout $ do
setTitleI Msg.RegisterLong setTitleI Msg.RegisterLong
addWidget addWidget
#if GHC7 [QQ(whamlet)|
[whamlet|
#else
[$whamlet|
#endif
<p>_{Msg.EnterEmail} <p>_{Msg.EnterEmail}
<form method="post" action="@{toMaster registerR}"> <form method="post" action="@{toMaster registerR}">
<label for="email">_{Msg.Email} <label for="email">_{Msg.Email}
@ -154,13 +148,7 @@ postRegisterR = do
defaultLayout $ do defaultLayout $ do
setTitleI Msg.ConfirmationEmailSentTitle setTitleI Msg.ConfirmationEmailSentTitle
addWidget addWidget
#if GHC7 [QQ(whamlet)| <p>_{Msg.ConfirmationEmailSent email} |]
[whamlet|
#else
[$whamlet|
#endif
<p>_{Msg.ConfirmationEmailSent email}
|]
getVerifyR :: YesodAuthEmail m getVerifyR :: YesodAuthEmail m
=> AuthEmailId m -> Text -> GHandler Auth m RepHtml => AuthEmailId m -> Text -> GHandler Auth m RepHtml
@ -181,13 +169,7 @@ getVerifyR lid key = do
defaultLayout $ do defaultLayout $ do
setTitleI Msg.InvalidKey setTitleI Msg.InvalidKey
addWidget addWidget
#if GHC7 [QQ(whamlet)| <p>_{Msg.InvalidKey} |]
[whamlet|
#else
[$whamlet|
#endif
<p>_{Msg.InvalidKey}
|]
postLoginR :: YesodAuthEmail master => GHandler Auth master () postLoginR :: YesodAuthEmail master => GHandler Auth master ()
postLoginR = do postLoginR = do
@ -226,11 +208,7 @@ getPasswordR = do
defaultLayout $ do defaultLayout $ do
setTitleI Msg.SetPassTitle setTitleI Msg.SetPassTitle
addWidget addWidget
#if GHC7 [QQ(whamlet)|
[whamlet|
#else
[$whamlet|
#endif
<h3>_{Msg.SetPass} <h3>_{Msg.SetPass}
<form method="post" action="@{toMaster setpassR}"> <form method="post" action="@{toMaster setpassR}">
<table> <table>

View File

@ -6,6 +6,8 @@ module Yesod.Auth.Facebook
, facebookUrl , facebookUrl
) where ) where
#include "qq.h"
import Yesod.Auth import Yesod.Auth
import qualified Web.Authenticate.Facebook as Facebook import qualified Web.Authenticate.Facebook as Facebook
import Data.Aeson import Data.Aeson
@ -56,11 +58,7 @@ authFacebook cid secret perms =
render <- lift getUrlRender render <- lift getUrlRender
let fb = Facebook.Facebook cid secret $ render $ tm url let fb = Facebook.Facebook cid secret $ render $ tm url
let furl = Facebook.getForwardUrl fb $ perms let furl = Facebook.getForwardUrl fb $ perms
#if GHC7 [QQ(whamlet)|
[whamlet|
#else
[$whamlet|
#endif
<p> <p>
<a href="#{furl}">_{Msg.Facebook} <a href="#{furl}">_{Msg.Facebook}
|] |]

View File

@ -64,6 +64,8 @@ module Yesod.Auth.HashDB
, migrateUsers , migrateUsers
) where ) where
#include "qq.h"
import Yesod.Persist import Yesod.Persist
import Yesod.Handler import Yesod.Handler
import Yesod.Form import Yesod.Form
@ -82,11 +84,7 @@ sha1String = showDigest . sha1 . pack
-- | Generate data base instances for a valid user -- | Generate data base instances for a valid user
share2 mkPersist (mkMigrate "migrateUsers") share2 mkPersist (mkMigrate "migrateUsers")
#if GHC7 [QQ(persist)|
[persist|
#else
[$persist|
#endif
User User
username Text Eq username Text Eq
password Text password Text
@ -128,13 +126,7 @@ postLoginR = do
then setCreds True $ Creds "hashdb" (fromMaybe "" mu) [] then setCreds True $ Creds "hashdb" (fromMaybe "" mu) []
else do else do
setMessage setMessage
#if GHC7 [QQ(hamlet)| Invalid username/password |]
[hamlet|
#else
[$hamlet|
#endif
Invalid username/password
|]
toMaster <- getRouteToMaster toMaster <- getRouteToMaster
redirect RedirectTemporary $ toMaster LoginR redirect RedirectTemporary $ toMaster LoginR
@ -158,14 +150,7 @@ getAuthIdHashDB authR creds = do
-- user exists -- user exists
Just (uid, _) -> return $ Just uid Just (uid, _) -> return $ Just uid
Nothing -> do Nothing -> do
setMessage setMessage [QQ(hamlet)| User not found |]
#if GHC7
[hamlet|
#else
[$hamlet|
#endif
User not found
|]
redirect RedirectTemporary $ authR LoginR redirect RedirectTemporary $ authR LoginR
-- | Prompt for username and password, validate that against a database -- | Prompt for username and password, validate that against a database
@ -175,11 +160,7 @@ authHashDB :: (YesodAuth y,
PersistBackend (YesodDB y (GGHandler Auth y IO))) PersistBackend (YesodDB y (GGHandler Auth y IO)))
=> AuthPlugin y => AuthPlugin y
authHashDB = AuthPlugin "hashdb" dispatch $ \tm -> authHashDB = AuthPlugin "hashdb" dispatch $ \tm ->
#if GHC7 [QQ(hamlet)|
[hamlet|
#else
[$hamlet|
#endif
<div id="header"> <div id="header">
<h1>Login <h1>Login

View File

@ -6,6 +6,9 @@ module Yesod.Auth.OAuth
, authTwitter , authTwitter
, twitterUrl , twitterUrl
) where ) where
#include "qq.h"
import Yesod.Auth import Yesod.Auth
import Yesod.Form import Yesod.Form
import Yesod.Handler import Yesod.Handler
@ -66,13 +69,7 @@ authOAuth name ident reqUrl accUrl authUrl key sec = AuthPlugin name dispatch lo
render <- lift getUrlRender render <- lift getUrlRender
let oaUrl = render $ tm $ oauthUrl name let oaUrl = render $ tm $ oauthUrl name
addHtml addHtml
#if GHC7 [QQ(hamlet)| <a href=#{oaUrl}>Login with #{name} |]
[hamlet|
#else
[$hamlet|
#endif
<a href=#{oaUrl}>Login with #{name}
|]
authTwitter :: YesodAuth m => authTwitter :: YesodAuth m =>
String -- ^ Consumer Key String -- ^ Consumer Key

View File

@ -6,6 +6,8 @@ module Yesod.Auth.OpenId
, forwardUrl , forwardUrl
) where ) where
#include "qq.h"
import Yesod.Auth import Yesod.Auth
import qualified Web.Authenticate.OpenId as OpenId import qualified Web.Authenticate.OpenId as OpenId
import Control.Monad.Attempt import Control.Monad.Attempt
@ -34,20 +36,12 @@ authOpenId =
ident <- lift newIdent ident <- lift newIdent
y <- lift getYesod y <- lift getYesod
addCassius addCassius
#if GHC7 [QQ(cassius)|##{ident}
[cassius|##{ident}
#else
[$cassius|##{ident}
#endif
background: #fff url(http://www.myopenid.com/static/openid-icon-small.gif) no-repeat scroll 0pt 50%; background: #fff url(http://www.myopenid.com/static/openid-icon-small.gif) no-repeat scroll 0pt 50%;
padding-left: 18px; padding-left: 18px;
|] |]
l <- lift languages l <- lift languages
#if GHC7 [QQ(whamlet)|
[whamlet|
#else
[$whamlet|
#endif
<form method="get" action="@{tm forwardUrl}"> <form method="get" action="@{tm forwardUrl}">
<label for="#{ident}">OpenID: # <label for="#{ident}">OpenID: #
<input id="#{ident}" type="text" name="#{name}" value="http://"> <input id="#{ident}" type="text" name="#{name}" value="http://">

View File

@ -5,6 +5,8 @@ module Yesod.Auth.Rpxnow
( authRpxnow ( authRpxnow
) where ) where
#include "qq.h"
import Yesod.Auth import Yesod.Auth
import qualified Web.Authenticate.Rpxnow as Rpxnow import qualified Web.Authenticate.Rpxnow as Rpxnow
import Control.Monad (mplus) import Control.Monad (mplus)
@ -27,11 +29,7 @@ authRpxnow app apiKey =
login tm = do login tm = do
let url = {- FIXME urlEncode $ -} tm $ PluginR "rpxnow" [] let url = {- FIXME urlEncode $ -} tm $ PluginR "rpxnow" []
addHamlet addHamlet
#if GHC7 [QQ(hamlet)|
[hamlet|
#else
[$hamlet|
#endif
<iframe src="http://#{app}.rpxnow.com/openid/embed?token_url=@{url}" scrolling="no" frameBorder="no" allowtransparency="true" style="width:400px;height:240px"> <iframe src="http://#{app}.rpxnow.com/openid/embed?token_url=@{url}" scrolling="no" frameBorder="no" allowtransparency="true" style="width:400px;height:240px">
|] |]
dispatch _ [] = do dispatch _ [] = do

10
include/qq.h Normal file
View File

@ -0,0 +1,10 @@
-- CPP macro which choses which quasyquotes syntax to use depending
-- on GHC version.
--
-- QQ stands for quasyquote.
#if GHC7
# define QQ(x) x
#else
# define QQ(x) $x
#endif

View File

@ -53,6 +53,8 @@ library
Yesod.Auth.HashDB Yesod.Auth.HashDB
Yesod.Auth.Message Yesod.Auth.Message
ghc-options: -Wall ghc-options: -Wall
include-dirs:
include
source-repository head source-repository head
type: git type: git