@@ -1,3 +1,5 @@
1+{-# LANGUAGE TemplateHaskell #-}
1--------------------------------------------------------------------------------
3--------------------------------------------------------------------------------
2-- | A Module that allows easy rendering of RSS feeds.
4-- | A Module that allows easy rendering of RSS feeds.
@@ -25,7 +27,6 @@ module Hakyll.Web.Feed
26--------------------------------------------------------------------------------
28--------------------------------------------------------------------------------
27import Hakyll.Core.Compiler
29import Hakyll.Core.Compiler
28−import Hakyll.Core.Compiler.Internal
29import Hakyll.Core.Item
30import Hakyll.Core.Item
30import Hakyll.Core.Util.String (replaceAll)
31import Hakyll.Core.Util.String (replaceAll)
31import Hakyll.Web.Template
32import Hakyll.Web.Template
@@ -34,7 +35,22 @@ import Hakyll.Web.Template.List
36--------------------------------------------------------------------------------
37--------------------------------------------------------------------------------
37−import Paths_hakyll
38+import Data.ByteString.Char8 (unpack)
39+import Data.FileEmbed (embedFile)
42+--------------------------------------------------------------------------------
44+rssTemplate = unpack $(embedFile "data/templates/rss.xml")
46+rssItemTemplate :: String
47+rssItemTemplate = unpack $(embedFile "data/templates/rss-item.xml")
49+atomTemplate :: String
50+atomTemplate = unpack $(embedFile "data/templates/atom.xml")
52+atomItemTemplate :: String
53+atomItemTemplate = unpack $(embedFile "data/templates/atom-item.xml")
40--------------------------------------------------------------------------------
56--------------------------------------------------------------------------------
@@ -55,15 +71,15 @@ data FeedConfiguration = FeedConfiguration
56--------------------------------------------------------------------------------
72--------------------------------------------------------------------------------
57-- | Abstract function to render any feed.
73-- | Abstract function to render any feed.
58−renderFeed :: FilePath -- ^ Feed template
74+renderFeed :: String -- ^ Default feed template
59− -> FilePath -- ^ Item template
75+ -> String -- ^ Default item template
60 -> FeedConfiguration -- ^ Feed configuration
76 -> FeedConfiguration -- ^ Feed configuration
61 -> Context String -- ^ Context for the items
77 -> Context String -- ^ Context for the items
62 -> [Item String] -- ^ Input items
78 -> [Item String] -- ^ Input items
63 -> Compiler (Item String) -- ^ Resulting item
79 -> Compiler (Item String) -- ^ Resulting item
64−renderFeed feedPath itemPath config itemContext items = do
80+renderFeed defFeed defItem config itemContext items = do
65− feedTpl <- loadTemplate feedPath
81+ feedTpl <- readTemplateFile defFeed
66− itemTpl <- loadTemplate itemPath
82+ itemTpl <- readTemplateFile defItem
68 protectedItems <- mapM (applyFilter protectCDATA) items
84 protectedItems <- mapM (applyFilter protectCDATA) items
69 body <- makeItem =<< applyTemplateList itemTpl itemContext' protectedItems
85 body <- makeItem =<< applyTemplateList itemTpl itemContext' protectedItems
@@ -73,10 +89,6 @@ renderFeed feedPath itemPath config itemContext items = do
73 applyFilter tr str = return $ fmap tr str
89 applyFilter tr str = return $ fmap tr str
74 protectCDATA :: String -> String
90 protectCDATA :: String -> String
75 protectCDATA = replaceAll "]]>" (const "]]>")
91 protectCDATA = replaceAll "]]>" (const "]]>")
76− -- Auxiliary: load a template from a datafile
77− loadTemplate path = do
78− file <- compilerUnsafeIO $ getDataFileName path
79− unsafeReadTemplateFile file
81 itemContext' = mconcat
93 itemContext' = mconcat
82 [ itemContext
94 [ itemContext
@@ -105,6 +117,9 @@ renderFeed feedPath itemPath config itemContext items = do
105 ListField _ _ -> fail "Hakyll.Web.Feed.renderFeed: Internal error"
117 ListField _ _ -> fail "Hakyll.Web.Feed.renderFeed: Internal error"
106 StringField s -> return s
118 StringField s -> return s
120+ readTemplateFile :: String -> Compiler Template
121+ readTemplateFile value = pure $ template $ readTemplateElems value
109--------------------------------------------------------------------------------
124--------------------------------------------------------------------------------
110-- | Render an RSS feed with a number of items.
125-- | Render an RSS feed with a number of items.
@@ -113,7 +128,7 @@ renderRss :: FeedConfiguration -- ^ Feed configuration
113 -> [Item String] -- ^ Feed items
128 -> [Item String] -- ^ Feed items
114 -> Compiler (Item String) -- ^ Resulting feed
129 -> Compiler (Item String) -- ^ Resulting feed
115renderRss config context = renderFeed
130renderRss config context = renderFeed
116− "templates/rss.xml" "templates/rss-item.xml" config
131+ rssTemplate rssItemTemplate config
117 (makeItemContext "%a, %d %b %Y %H:%M:%S UT" context)
132 (makeItemContext "%a, %d %b %Y %H:%M:%S UT" context)
@@ -124,7 +139,7 @@ renderAtom :: FeedConfiguration -- ^ Feed configuration
124 -> [Item String] -- ^ Feed items
139 -> [Item String] -- ^ Feed items
125 -> Compiler (Item String) -- ^ Resulting feed
140 -> Compiler (Item String) -- ^ Resulting feed
126renderAtom config context = renderFeed
141renderAtom config context = renderFeed
127− "templates/atom.xml" "templates/atom-item.xml" config
142+ atomTemplate atomItemTemplate config
128 (makeItemContext "%Y-%m-%dT%H:%M:%SZ" context)
143 (makeItemContext "%Y-%m-%dT%H:%M:%SZ" context)