@@ -1,3 +1,5 @@
1+{-# LANGUAGE TemplateHaskell #-}
2+
13--------------------------------------------------------------------------------
24-- | A Module that allows easy rendering of RSS feeds.
35--
@@ -25,7 +27,6 @@ module Hakyll.Web.Feed
2527
2628--------------------------------------------------------------------------------
2729import Hakyll.Core.Compiler
28−import Hakyll.Core.Compiler.Internal
2930import Hakyll.Core.Item
3031import Hakyll.Core.Util.String (replaceAll)
3132import Hakyll.Web.Template
@@ -34,7 +35,22 @@ import Hakyll.Web.Template.List
3435
3536
3637--------------------------------------------------------------------------------
37−import Paths_hakyll
38+import Data.ByteString.Char8 (unpack)
39+import Data.FileEmbed (embedFile)
40+
41+
42+--------------------------------------------------------------------------------
43+rssTemplate :: String
44+rssTemplate = unpack $(embedFile "data/templates/rss.xml")
45+
46+rssItemTemplate :: String
47+rssItemTemplate = unpack $(embedFile "data/templates/rss-item.xml")
48+
49+atomTemplate :: String
50+atomTemplate = unpack $(embedFile "data/templates/atom.xml")
51+
52+atomItemTemplate :: String
53+atomItemTemplate = unpack $(embedFile "data/templates/atom-item.xml")
3854
3955
4056--------------------------------------------------------------------------------
@@ -55,15 +71,15 @@ data FeedConfiguration = FeedConfiguration
5571
5672--------------------------------------------------------------------------------
5773-- | Abstract function to render any feed.
58−renderFeed :: FilePath -- ^ Feed template
59− -> FilePath -- ^ Item template
74+renderFeed :: String -- ^ Default feed template
75+ -> String -- ^ Default item template
6076 -> FeedConfiguration -- ^ Feed configuration
6177 -> Context String -- ^ Context for the items
6278 -> [Item String] -- ^ Input items
6379 -> Compiler (Item String) -- ^ Resulting item
64−renderFeed feedPath itemPath config itemContext items = do
65− feedTpl <- loadTemplate feedPath
66− itemTpl <- loadTemplate itemPath
80+renderFeed defFeed defItem config itemContext items = do
81+ feedTpl <- readTemplateFile defFeed
82+ itemTpl <- readTemplateFile defItem
6783
6884 protectedItems <- mapM (applyFilter protectCDATA) items
6985 body <- makeItem =<< applyTemplateList itemTpl itemContext' protectedItems
@@ -73,10 +89,6 @@ renderFeed feedPath itemPath config itemContext items = do
7389 applyFilter tr str = return $ fmap tr str
7490 protectCDATA :: String -> String
7591 protectCDATA = replaceAll "]]>" (const "]]>")
76− -- Auxiliary: load a template from a datafile
77− loadTemplate path = do
78− file <- compilerUnsafeIO $ getDataFileName path
79− unsafeReadTemplateFile file
8092
8193 itemContext' = mconcat
8294 [ itemContext
@@ -105,6 +117,9 @@ renderFeed feedPath itemPath config itemContext items = do
105117 ListField _ _ -> fail "Hakyll.Web.Feed.renderFeed: Internal error"
106118 StringField s -> return s
107119
120+ readTemplateFile :: String -> Compiler Template
121+ readTemplateFile value = pure $ template $ readTemplateElems value
122+
108123
109124--------------------------------------------------------------------------------
110125-- | Render an RSS feed with a number of items.
@@ -113,7 +128,7 @@ renderRss :: FeedConfiguration -- ^ Feed configuration
113128 -> [Item String] -- ^ Feed items
114129 -> Compiler (Item String) -- ^ Resulting feed
115130renderRss config context = renderFeed
116− "templates/rss.xml" "templates/rss-item.xml" config
131+ rssTemplate rssItemTemplate config
117132 (makeItemContext "%a, %d %b %Y %H:%M:%S UT" context)
118133
119134
@@ -124,7 +139,7 @@ renderAtom :: FeedConfiguration -- ^ Feed configuration
124139 -> [Item String] -- ^ Feed items
125140 -> Compiler (Item String) -- ^ Resulting feed
126141renderAtom config context = renderFeed
127− "templates/atom.xml" "templates/atom-item.xml" config
142+ atomTemplate atomItemTemplate config
128143 (makeItemContext "%Y-%m-%dT%H:%M:%SZ" context)
129144
130145