module Yu.Core.View
(
respondPost
, respondResourceT
, respondResourceB
, respondStatic
,
module Yu.Core.View.Internal
, module Yu.Core.View.Query
) where
import Control.Monad.Writer.Lazy
import Data.Maybe
import Data.Monoid
import Network.HTTP.Types (status301)
import Text.Blaze.Html (Html, preEscapedToHtml)
import Yesod.Core
import Yesod.Core.Handler
import Yesod.Core.Widget
import Yu.Core.Model (ResT (..))
import Yu.Core.View.Internal
import Yu.Core.View.Query
import Yu.Import.ByteString (ByteString)
import qualified Yu.Import.ByteString as B
import Yu.Import.Text (Text)
import qualified Yu.Import.Text as T
import Yu.Utils.Handler
respondPost :: (Yesod a, Hamletic a (HandlerT a IO))
=> ResT
-> Html
-> HandlerT a IO TypedContent
respondPost res@ResT{..} rawBody = do
willRaw <- getRaw
isRaw <- null <$> lookupHeader "YuRAW"
body <- if willRaw == isRaw then return rawBody
else defaultLayout $ withHtml rawBody res
respondSource "text/html" $ do
sendChunkHtml body
sendFlush
withTags :: [Text]
-> WidgetT site IO ()
withTags tags = let tagsJ = toJSON tags in toWidget [julius|tags=#{tagsJ};|]
withSummary :: Maybe Html
-> WidgetT site IO ()
withSummary (Just summaryHtml) = [whamlet|<summary id=sum>#{summaryHtml}|]
withSummary _ = return ()
withWhose :: Maybe Text
-> WidgetT site IO ()
withWhose (Just whose) = let w = showJs whose in toWidget [julius|author=#{w};|]
withWhose _ = return ()
withHtml :: Html
-> ResT
-> WidgetT site IO ()
withHtml body ResT{..} = do
setTitle $ toHtml rTitle
withSummary $ preEscapedToHtml <$> rSummary
[whamlet|#{body}|]
withWhose rWhose
withTags rTags
respondResourceT :: (Yesod a, Hamletic a (HandlerT a IO))
=> ResT
-> Text
-> HandlerT a IO TypedContent
respondResourceT ResT{..} text = do
respondSource (fromMaybe "" $ fmap T.encodeUtf8 rMIME) $ do
sendChunkText text
sendFlush
respondResourceB :: (Yesod a, Hamletic a (HandlerT a IO))
=> ResT
-> ByteString
-> HandlerT a IO TypedContent
respondResourceB ResT{..} bin = do
respondSource (fromMaybe "" $ fmap T.encodeUtf8 rMIME) $ do
sendChunkBS bin
sendFlush
respondStatic :: (Yesod a, Hamletic a (HandlerT a IO))
=> ResT
-> Text
-> HandlerT a IO TypedContent
respondStatic _ url = redirectWith status301 url