{-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE MultiParamTypeClasses #-} --------------------------------------------------------- -- -- Module : Yesod.Helpers.AtomFeed -- Copyright : Michael Snoyman -- License : BSD3 -- -- Maintainer : Michael Snoyman -- Stability : Stable -- Portability : portable -- -- Generating atom news feeds. -- --------------------------------------------------------- module Yesod.Helpers.AtomFeed ( AtomFeed (..) , AtomFeedEntry (..) , AtomFeedResponse (..) , atomFeed ) where import Yesod import Data.Text.Lazy (Text) import qualified Data.Text.Lazy as TL import Data.Time.Clock import Web.Encodings data AtomFeedResponse = AtomFeedResponse AtomFeed Approot atomFeed :: YesodApproot y => AtomFeed -> Handler y AtomFeedResponse atomFeed f = do y <- getYesod return $ AtomFeedResponse f $ approot y data AtomFeed = AtomFeed { atomTitle :: String , atomLinkSelf :: Location , atomLinkHome :: Location , atomUpdated :: UTCTime , atomEntries :: [AtomFeedEntry] } instance HasReps AtomFeedResponse where reps = [ (TypeAtom, return . cs) ] data AtomFeedEntry = AtomFeedEntry { atomEntryLink :: Location , atomEntryUpdated :: UTCTime , atomEntryTitle :: String , atomEntryContent :: Html } instance ConvertSuccess AtomFeedResponse Content where convertSuccess = (cs :: Text -> Content) . cs instance ConvertSuccess AtomFeedResponse Text where convertSuccess (AtomFeedResponse f ar) = TL.concat [ cs "\n" , cs "" , cs "" , encodeHtml $ cs $ atomTitle f , cs "" , cs "" , cs "" , cs "" , cs $ formatW3 $ atomUpdated f , cs "" , cs "" , encodeHtml $ cs $ showLocation ar $ atomLinkHome f , cs "" , TL.concat $ map cs $ zip (atomEntries f) $ repeat ar , cs "" ] instance ConvertSuccess (AtomFeedEntry, Approot) Text where convertSuccess (e, ar) = TL.concat [ cs "" , cs "" , encodeHtml $ cs $ showLocation ar $ atomEntryLink e , cs "" , cs "" , cs "" , cs $ formatW3 $ atomEntryUpdated e , cs "" , cs "" , encodeHtml $ cs $ atomEntryTitle e , cs "" , cs "" , cs "" ]