{-# LANGUAGE TemplateHaskell #-}
module Hakyll.Web.Feed
( FeedConfiguration (..)
, renderRss
, renderAtom
, renderRssWithTemplates
, renderAtomWithTemplates
) where
import Hakyll.Core.Compiler
import Hakyll.Core.Item
import Hakyll.Core.Util.String (replaceAll)
import Hakyll.Web.Template
import Hakyll.Web.Template.Context
import Hakyll.Web.Template.List
import Data.FileEmbed (makeRelativeToProject, embedFile)
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
rssTemplate :: String
rssTemplate = T.unpack $
T.decodeUtf8 $(makeRelativeToProject "data/templates/rss.xml" >>= embedFile)
rssItemTemplate :: String
rssItemTemplate = T.unpack $
T.decodeUtf8 $(makeRelativeToProject "data/templates/rss-item.xml" >>= embedFile)
atomTemplate :: String
atomTemplate = T.unpack $
T.decodeUtf8 $(makeRelativeToProject "data/templates/atom.xml" >>= embedFile)
atomItemTemplate :: String
atomItemTemplate = T.unpack $
T.decodeUtf8 $(makeRelativeToProject "data/templates/atom-item.xml" >>= embedFile)
data FeedConfiguration = FeedConfiguration
{
feedTitle :: String
,
feedDescription :: String
,
feedAuthorName :: String
,
feedAuthorEmail :: String
,
feedRoot :: String
} deriving (Show, Eq)
renderFeed :: String
-> String
-> FeedConfiguration
-> Context String
-> [Item String]
-> Compiler (Item String)
renderFeed defFeed defItem config itemContext items = do
feedTpl <- readTemplateFile defFeed
itemTpl <- readTemplateFile defItem
protectedItems <- mapM (applyFilter protectCDATA) items
body <- makeItem =<< applyTemplateList itemTpl itemContext' protectedItems
applyTemplate feedTpl feedContext body
where
applyFilter :: (Monad m,Functor f) => (String -> String) -> f String -> m (f String)
applyFilter tr str = return $ fmap tr str
protectCDATA :: String -> String
protectCDATA = replaceAll "]]>" (const "]]>")
itemContext' = mconcat
[ itemContext
, constField "root" (feedRoot config)
, constField "authorName" (feedAuthorName config)
, constField "authorEmail" (feedAuthorEmail config)
]
feedContext = mconcat
[ bodyField "body"
, constField "title" (feedTitle config)
, constField "description" (feedDescription config)
, constField "authorName" (feedAuthorName config)
, constField "authorEmail" (feedAuthorEmail config)
, constField "root" (feedRoot config)
, urlField "url"
, updatedField
, missingField
]
updatedField = field "updated" $ \_ -> case items of
[] -> return "Unknown"
(x : _) -> unContext itemContext' "updated" [] x >>= \cf -> case cf of
ListField _ _ -> fail "Hakyll.Web.Feed.renderFeed: Internal error"
StringField s -> return s
readTemplateFile :: String -> Compiler Template
readTemplateFile value = pure $ template $ readTemplateElems value
renderRssWithTemplates ::
String
-> String
-> FeedConfiguration
-> Context String
-> [Item String]
-> Compiler (Item String)
renderRssWithTemplates feedTemplate itemTemplate config context = renderFeed
feedTemplate itemTemplate config
(makeItemContext "%a, %d %b %Y %H:%M:%S UT" context)
renderAtomWithTemplates ::
String
-> String
-> FeedConfiguration
-> Context String
-> [Item String]
-> Compiler (Item String)
renderAtomWithTemplates feedTemplate itemTemplate config context = renderFeed
feedTemplate itemTemplate config
(makeItemContext "%Y-%m-%dT%H:%M:%SZ" context)
renderRss :: FeedConfiguration
-> Context String
-> [Item String]
-> Compiler (Item String)
renderRss = renderRssWithTemplates rssTemplate rssItemTemplate
renderAtom :: FeedConfiguration
-> Context String
-> [Item String]
-> Compiler (Item String)
renderAtom = renderAtomWithTemplates atomTemplate atomItemTemplate
makeItemContext :: String -> Context a -> Context a
makeItemContext fmt context = mconcat
[dateField "published" fmt, context, dateField "updated" fmt]