on master help sign in
Embed feed templates rather than using data-files
commit526cd35f418fe14bb0039900a00a0e024686ee34 authorRoman Kuznetsov <roman@kuznero.com> dateSun 7 Jan 2018, 12:17 (9 years ago) parent480da30
4 files changed+36−19
hakyll.cabal+6−5 lib/Hakyll/Web/Feed.hs+28−13 lib/Hakyll/Web/Template.hs+2 lib/Hakyll/Web/Template/Internal.hs−1
hakyll.cabal+6−5
@@ -56,10 +56,6 @@ Data-files:
5656 example/index.html
5757 example/about.rst
5858 example/contact.markdown
59− templates/atom-item.xml
60− templates/atom.xml
61− templates/rss-item.xml
62− templates/rss.xml
6359
6460Extra-source-files:
6561 CHANGELOG.md
@@ -77,6 +73,10 @@ Extra-source-files:
7773 tests/data/strip.html.out
7874 tests/data/template.html
7975 tests/data/template.html.out
76+ data/templates/atom-item.xml
77+ data/templates/atom.xml
78+ data/templates/rss-item.xml
79+ data/templates/rss.xml
8080
8181Source-Repository head
8282 Type: git
@@ -190,7 +190,8 @@ Library
190190 unordered-containers >= 0.2 && < 0.3,
191191 vector >= 0.11 && < 0.13,
192192 yaml >= 0.8.11 && < 0.9,
193− optparse-applicative >= 0.12 && < 0.15
193+ optparse-applicative >= 0.12 && < 0.15,
194+ file-embed >= 0.0.10.1 && < 0.0.11
194195
195196 If flag(previewServer)
196197 Build-depends:
lib/Hakyll/Web/Feed.hs+28−13
@@ -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 "]]&gt;")
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
lib/Hakyll/Web/Template.hs+2
@@ -140,6 +140,8 @@
140140--
141141module Hakyll.Web.Template
142142 ( Template
143+ , template
144+ , readTemplateElems
143145 , templateBodyCompiler
144146 , templateCompiler
145147 , applyTemplate
lib/Hakyll/Web/Template/Internal.hs−1
@@ -200,4 +200,3 @@ unsafeReadTemplateFile :: FilePath -> Compiler Template
200200unsafeReadTemplateFile file = do
201201 tpl <- unsafeCompiler $ readFile file
202202 pure $ template $ readTemplateElemsFile file tpl
203−