{-# LANGUAGE DeriveDataTypeable, FlexibleContexts, FlexibleInstances, GeneralizedNewtypeDeriving, MultiParamTypeClasses, OverloadedStrings, RecordWildCards, TemplateHaskell, TypeFamilies, TypeSynonymInstances, OverloadedStrings #-} {-# OPTIONS_GHC -F -pgmFhsx2hs #-} module Tiny.TemplateAuth ( appTemplateHSP , appTemplateBlaze ) where import qualified Data.Text.Lazy as Lazy (pack, Text) import Happstack.Auth (AuthProfileURL(AuthURL), AuthURL(A_AddAuth, A_CreateAccount, A_Login, A_Logout), getUserId, UserId(unUserId), AuthState, ProfileState) import Happstack.Foundation (Attr(..), EmbedAsAttr(..), EmbedAsChild(..), fromStringLit, Happstack, liftM, Response, ToMessage(toResponse), XMLGen(..), XMLGenerator, XMLGenT(XMLGenT), AcidState, HasAcidState(getAcidState), XML) import qualified Happstack.Server.HSP.HTML as HSP (defaultTemplate) import qualified Text.Blaze.Html as Blaze (Html) import Tiny.RouteAuth (App, App', Route(CSS, NewPaste, U_AuthProfile, ViewRecent)) ------------------------------------------------------------------------------ -- appTemplate ------------------------------------------------------------------------------ -- | This renders the common parts of the application web pages, -- embedding the headers and body arguments in the page. appTemplateHSP :: (EmbedAsChild App' headers, EmbedAsChild App' body, EmbedAsAttr App' (Attr Lazy.Text Route)) => Lazy.Text -- ^ page title -> headers -- ^ extra headers to add to \
tag -> body -- ^ contents of \ tag -> App Response appTemplateHSP ttl moreHdrs bdy = do acidAuth <- getAcidState acidProfile <- getAcidState liftM toResponse (XMLGenT $ appTemplate acidAuth acidProfile ttl moreHdrs bdy) -- | Stick our usual app template into a state where the auth -- functions, which are Blaze based, are OK with it. appTemplateBlaze :: (Happstack m, EmbedAsAttr m (Attr Lazy.Text Route), XMLGenerator m, EmbedAsChild m Blaze.Html, StringType m ~ Lazy.Text, XMLType m ~ XML) => AcidState AuthState -> AcidState ProfileState -> Lazy.Text -> Blaze.Html -> Blaze.Html -> m Response appTemplateBlaze aa ap t h b = liftM toResponse (appTemplate aa ap t h b) -- | page template function -- -- There are two forms here because we need to make it work for both -- our usual happstack-foundation/HSP stuff, and for happstack-auth, -- which is Blaze rather than HSP based. So we make the base -- template, and then make two versions that give it each of the two -- contexts it needs. appTemplate :: ( EmbedAsChild m headers , EmbedAsChild m body , EmbedAsAttr m (Attr Lazy.Text Route) , XMLGenerator m , Happstack m , StringType m ~ Lazy.Text) => AcidState AuthState -> AcidState ProfileState -> Lazy.Text -- ^ page title -> headers -- ^ extra headers to add to \ tag -> body -- ^ contents of \ tag -> m (XMLType m) appTemplate acidAuth acidProfile ttl moreHdrs bdy = HSP.defaultTemplate ttl <%><% moreHdrs %>%> $ <%>