module Text.Hakyll.Feed
( FeedConfiguration (..)
, renderRss
, renderAtom
) where
import Control.Arrow ((>>>), second)
import Control.Monad.Reader (liftIO)
import Data.Maybe (fromMaybe)
import qualified Data.Map as M
import Text.Hakyll.Context (Context (..))
import Text.Hakyll.CreateContext (createListing)
import Text.Hakyll.ContextManipulations (renderDate)
import Text.Hakyll.HakyllMonad (Hakyll)
import Text.Hakyll.Render (render, renderChain)
import Text.Hakyll.HakyllAction
import Paths_hakyll
data FeedConfiguration = FeedConfiguration
{
feedUrl :: String
,
feedTitle :: String
,
feedDescription :: String
,
feedAuthorName :: String
}
createFeed :: FeedConfiguration
-> [HakyllAction () Context]
-> FilePath
-> FilePath
-> HakyllAction () Context
createFeed configuration renderables template itemTemplate =
listing >>> render template
where
listing = createListing (feedUrl configuration)
[itemTemplate] renderables additional
additional = map (second $ Left . ($ configuration))
[ ("title", feedTitle)
, ("description", feedDescription)
, ("authorName", feedAuthorName)
] ++ updated
updated = let action = createHakyllAction $ return . fromMaybe "foo"
. M.lookup "timestamp" . unContext
toTuple r = ("timestamp", Right $ r >>> action)
in map toTuple $ take 1 renderables
renderFeed :: FeedConfiguration
-> [HakyllAction () Context]
-> FilePath
-> FilePath
-> Hakyll ()
renderFeed configuration renderables template itemTemplate = do
template' <- liftIO $ getDataFileName template
itemTemplate' <- liftIO $ getDataFileName itemTemplate
let renderFeed' = createFeed configuration renderables
template' itemTemplate'
renderChain [] renderFeed'
renderRss :: FeedConfiguration
-> [HakyllAction () Context]
-> Hakyll ()
renderRss configuration renderables =
renderFeed configuration (map (>>> renderRssDate) renderables)
"templates/rss.xml" "templates/rss-item.xml"
where
renderRssDate = renderDate "timestamp" "%a, %d %b %Y %H:%M:%S UT"
"No date found."
renderAtom :: FeedConfiguration
-> [HakyllAction () Context]
-> Hakyll ()
renderAtom configuration renderables =
renderFeed configuration (map (>>> renderAtomDate) renderables)
"templates/atom.xml" "templates/atom-item.xml"
where
renderAtomDate = renderDate "timestamp" "%Y-%m-%dT%H:%M:%SZ"
"No date found."