{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
module Hakyll.Web.Feed
( FeedConfiguration (..)
, renderRss
, renderAtom
, renderJson
, renderRssWithTemplates
, renderAtomWithTemplates
, renderJsonWithTemplates
) 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)
import System.FilePath ((</>))
import Text.Printf (printf)
rssTemplate :: Template
=
$(makeRelativeToProject ("data" </> "templates" </> "rss.xml")
>>= embedTemplate)
rssItemTemplate :: Template
=
$(makeRelativeToProject ("data" </> "templates" </> "rss-item.xml")
>>= embedTemplate)
atomTemplate :: Template
atomTemplate :: Template
atomTemplate =
$(makeRelativeToProject ("data" </> "templates" </> "atom.xml")
>>= embedTemplate)
atomItemTemplate :: Template
atomItemTemplate :: Template
atomItemTemplate =
$(makeRelativeToProject ("data" </> "templates" </> "atom-item.xml")
>>= embedTemplate)
jsonTemplate :: Template
jsonTemplate :: Template
jsonTemplate =
$(makeRelativeToProject ("data" </> "templates" </> "feed.json")
>>= embedTemplate)
jsonItemTemplate :: Template
jsonItemTemplate :: Template
jsonItemTemplate =
$(makeRelativeToProject ("data" </> "templates" </> "feed-item.json")
>>= embedTemplate)
data FeedConfiguration = FeedConfiguration
{
FeedConfiguration -> String
feedTitle :: String
,
FeedConfiguration -> String
feedDescription :: String
,
FeedConfiguration -> String
feedAuthorName :: String
,
FeedConfiguration -> String
feedAuthorEmail :: String
,
FeedConfiguration -> String
feedRoot :: String
} deriving (Int -> FeedConfiguration -> ShowS
[FeedConfiguration] -> ShowS
FeedConfiguration -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [FeedConfiguration] -> ShowS
$cshowList :: [FeedConfiguration] -> ShowS
show :: FeedConfiguration -> String
$cshow :: FeedConfiguration -> String
showsPrec :: Int -> FeedConfiguration -> ShowS
$cshowsPrec :: Int -> FeedConfiguration -> ShowS
Show, FeedConfiguration -> FeedConfiguration -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: FeedConfiguration -> FeedConfiguration -> Bool
$c/= :: FeedConfiguration -> FeedConfiguration -> Bool
== :: FeedConfiguration -> FeedConfiguration -> Bool
$c== :: FeedConfiguration -> FeedConfiguration -> Bool
Eq)
data FeedType = XmlFeed | JsonFeed
deriving (Int -> FeedType -> ShowS
[FeedType] -> ShowS
FeedType -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [FeedType] -> ShowS
$cshowList :: [FeedType] -> ShowS
show :: FeedType -> String
$cshow :: FeedType -> String
showsPrec :: Int -> FeedType -> ShowS
$cshowsPrec :: Int -> FeedType -> ShowS
Show, FeedType -> FeedType -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: FeedType -> FeedType -> Bool
$c/= :: FeedType -> FeedType -> Bool
== :: FeedType -> FeedType -> Bool
$c== :: FeedType -> FeedType -> Bool
Eq)
renderFeed :: FeedType
-> Template
-> Template
-> FeedConfiguration
-> Context String
-> [Item String]
-> Compiler (Item String)
renderFeed :: FeedType
-> Template
-> Template
-> FeedConfiguration
-> Context String
-> [Item String]
-> Compiler (Item String)
renderFeed FeedType
feedType Template
feedTpl Template
itemTpl FeedConfiguration
config Context String
itemContext [Item String]
items = do
[Item String]
protectedItems <-
case FeedType
feedType of
FeedType
XmlFeed -> forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (forall (m :: * -> *) (f :: * -> *).
(Monad m, Functor f) =>
ShowS -> f String -> m (f String)
applyFilter ShowS
protectCDATA) [Item String]
items
FeedType
JsonFeed -> forall (f :: * -> *) a. Applicative f => a -> f a
pure [Item String]
items
let itemDelim :: String
itemDelim = case FeedType
feedType of
FeedType
XmlFeed -> String
""
FeedType
JsonFeed -> String
", "
Item String
body <- forall a. a -> Compiler (Item a)
makeItem forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall a.
String -> Template -> Context a -> [Item a] -> Compiler String
applyJoinTemplateList String
itemDelim Template
itemTpl Context String
itemContext' [Item String]
protectedItems
forall a. Template -> Context a -> Item a -> Compiler (Item String)
applyTemplate Template
feedTpl Context String
feedContext Item String
body
where
applyFilter :: (Monad m,Functor f) => (String -> String) -> f String -> m (f String)
applyFilter :: forall (m :: * -> *) (f :: * -> *).
(Monad m, Functor f) =>
ShowS -> f String -> m (f String)
applyFilter ShowS
tr f String
str = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ShowS
tr f String
str
protectCDATA :: String -> String
protectCDATA :: ShowS
protectCDATA = String -> ShowS -> ShowS
replaceAll String
"]]>" (forall a b. a -> b -> a
const String
"]]>")
itemContext' :: Context String
itemContext' = forall a. Monoid a => [a] -> a
mconcat
[ forall {a}. Context a -> Context a
escapeDescription Context String
itemContext
, forall a. String -> String -> Context a
constField String
"root" (FeedConfiguration -> String
feedRoot FeedConfiguration
config)
, forall a. String -> String -> Context a
constField String
"authorName" (FeedConfiguration -> String
feedAuthorName FeedConfiguration
config)
, forall {a}. Context a
emailField
]
feedContext :: Context String
feedContext = forall a. Monoid a => [a] -> a
mconcat
[ String -> Context String
bodyField String
"body"
, forall a. String -> String -> Context a
constField String
"title" (FeedConfiguration -> String
feedTitle FeedConfiguration
config)
, forall a. String -> String -> Context a
constField String
"description" (FeedConfiguration -> String
feedDescription FeedConfiguration
config)
, forall a. String -> String -> Context a
constField String
"authorName" (FeedConfiguration -> String
feedAuthorName FeedConfiguration
config)
, forall {a}. Context a
emailField
, forall a. String -> String -> Context a
constField String
"root" (FeedConfiguration -> String
feedRoot FeedConfiguration
config)
, forall a. String -> Context a
urlField String
"url"
, forall {a}. Context a
updatedField
, forall {a}. Context a
missingField
]
updatedField :: Context a
updatedField = forall a. String -> (Item a -> Compiler String) -> Context a
field String
"updated" forall a b. (a -> b) -> a -> b
$ \Item a
_ -> case [Item String]
items of
[] -> forall (m :: * -> *) a. Monad m => a -> m a
return String
"Unknown"
(Item String
x : [Item String]
_) -> forall a.
Context a -> String -> [String] -> Item a -> Compiler ContextField
unContext Context String
itemContext' String
"updated" [] Item String
x forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \ContextField
cf -> case ContextField
cf of
StringField String
s -> forall (m :: * -> *) a. Monad m => a -> m a
return String
s
ContextField
_ -> forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Hakyll.Web.Feed.renderFeed: Internal error"
emailField :: Context a
emailField = case FeedConfiguration -> String
feedAuthorEmail FeedConfiguration
config of
String
"" -> forall {a}. Context a
missingField
String
email -> forall a. String -> String -> Context a
constField String
"authorEmail" String
email
escapeDescription :: Context a -> Context a
escapeDescription = case FeedType
feedType of
FeedType
XmlFeed -> forall a. a -> a
id
FeedType
JsonFeed -> forall a. (String -> Bool) -> ShowS -> Context a -> Context a
mapContextBy (forall a. Eq a => a -> a -> Bool
== String
"description") ShowS
escapeString
renderRssWithTemplates ::
Template
-> Template
-> FeedConfiguration
-> Context String
-> [Item String]
-> Compiler (Item String)
Template
feedTemplate Template
itemTemplate FeedConfiguration
config Context String
context = FeedType
-> Template
-> Template
-> FeedConfiguration
-> Context String
-> [Item String]
-> Compiler (Item String)
renderFeed
FeedType
XmlFeed Template
feedTemplate Template
itemTemplate FeedConfiguration
config
(forall a. String -> Context a -> Context a
makeItemContext String
"%a, %d %b %Y %H:%M:%S UT" Context String
context)
renderAtomWithTemplates ::
Template
-> Template
-> FeedConfiguration
-> Context String
-> [Item String]
-> Compiler (Item String)
renderAtomWithTemplates :: Template
-> Template
-> FeedConfiguration
-> Context String
-> [Item String]
-> Compiler (Item String)
renderAtomWithTemplates Template
feedTemplate Template
itemTemplate FeedConfiguration
config Context String
context = FeedType
-> Template
-> Template
-> FeedConfiguration
-> Context String
-> [Item String]
-> Compiler (Item String)
renderFeed
FeedType
XmlFeed Template
feedTemplate Template
itemTemplate FeedConfiguration
config
(forall a. String -> Context a -> Context a
makeItemContext String
"%Y-%m-%dT%H:%M:%SZ" Context String
context)
renderJsonWithTemplates ::
Template
-> Template
-> FeedConfiguration
-> Context String
-> [Item String]
-> Compiler (Item String)
renderJsonWithTemplates :: Template
-> Template
-> FeedConfiguration
-> Context String
-> [Item String]
-> Compiler (Item String)
renderJsonWithTemplates Template
feedTemplate Template
itemTemplate FeedConfiguration
config Context String
context = FeedType
-> Template
-> Template
-> FeedConfiguration
-> Context String
-> [Item String]
-> Compiler (Item String)
renderFeed
FeedType
JsonFeed Template
feedTemplate Template
itemTemplate FeedConfiguration
config
(forall a. String -> Context a -> Context a
makeItemContext String
"%Y-%m-%dT%H:%M:%SZ" Context String
context)
renderRss :: FeedConfiguration
-> Context String
-> [Item String]
-> Compiler (Item String)
= Template
-> Template
-> FeedConfiguration
-> Context String
-> [Item String]
-> Compiler (Item String)
renderRssWithTemplates Template
rssTemplate Template
rssItemTemplate
renderAtom :: FeedConfiguration
-> Context String
-> [Item String]
-> Compiler (Item String)
renderAtom :: FeedConfiguration
-> Context String -> [Item String] -> Compiler (Item String)
renderAtom = Template
-> Template
-> FeedConfiguration
-> Context String
-> [Item String]
-> Compiler (Item String)
renderAtomWithTemplates Template
atomTemplate Template
atomItemTemplate
renderJson :: FeedConfiguration
-> Context String
-> [Item String]
-> Compiler (Item String)
renderJson :: FeedConfiguration
-> Context String -> [Item String] -> Compiler (Item String)
renderJson = Template
-> Template
-> FeedConfiguration
-> Context String
-> [Item String]
-> Compiler (Item String)
renderJsonWithTemplates Template
jsonTemplate Template
jsonItemTemplate
makeItemContext :: String -> Context a -> Context a
makeItemContext :: forall a. String -> Context a -> Context a
makeItemContext String
fmt Context a
context = forall a. Monoid a => [a] -> a
mconcat
[Context a
context, forall a. String -> String -> Context a
dateField String
"published" String
fmt, forall a. String -> String -> Context a
dateField String
"updated" String
fmt]
escapeString :: String -> String
escapeString :: ShowS
escapeString = forall a b c. (a -> b -> c) -> b -> a -> c
flip String -> ShowS
escapeString' String
""
where
escapeString' :: String -> ShowS
escapeString' :: String -> ShowS
escapeString' [] String
s = String
s
escapeString' (Char
'"' : String
cs) String
s = String -> ShowS
showString String
"\\\"" (String -> ShowS
escapeString' String
cs String
s)
escapeString' (Char
'\\' : String
cs) String
s = String -> ShowS
showString String
"\\\\" (String -> ShowS
escapeString' String
cs String
s)
escapeString' (Char
c : String
cs) String
s
| Char
c forall a. Ord a => a -> a -> Bool
< Char
' ' = Char -> ShowS
escapeChar Char
c (String -> ShowS
escapeString' String
cs String
s)
| Bool
otherwise = Char -> ShowS
showChar Char
c (String -> ShowS
escapeString' String
cs String
s)
escapeChar :: Char -> ShowS
escapeChar :: Char -> ShowS
escapeChar = String -> ShowS
showString forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall r. PrintfType r => String -> r
printf String
"\\u%04X"