on master help sign in
Embed feed templates rather than using data-files
526cd35Roman Kuznetsov
hakyll.cabal+6−5
@@ -56,10 +56,6 @@ Data-files:
56 example/index.html
56 example/index.html
57 example/about.rst
57 example/about.rst
58 example/contact.markdown
58 example/contact.markdown
59− templates/atom-item.xml
60− templates/atom.xml
61− templates/rss-item.xml
62− templates/rss.xml
63
59
64Extra-source-files:
60Extra-source-files:
65 CHANGELOG.md
61 CHANGELOG.md
@@ -77,6 +73,10 @@ Extra-source-files:
77 tests/data/strip.html.out
73 tests/data/strip.html.out
78 tests/data/template.html
74 tests/data/template.html
79 tests/data/template.html.out
75 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
80
80
81Source-Repository head
81Source-Repository head
82 Type: git
82 Type: git
@@ -190,7 +190,8 @@ Library
190 unordered-containers >= 0.2 && < 0.3,
190 unordered-containers >= 0.2 && < 0.3,
191 vector >= 0.11 && < 0.13,
191 vector >= 0.11 && < 0.13,
192 yaml >= 0.8.11 && < 0.9,
192 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
194
195
195 If flag(previewServer)
196 If flag(previewServer)
196 Build-depends:
197 Build-depends:
lib/Hakyll/Web/Feed.hs+28−13
@@ -1,3 +1,5 @@
1+{-# LANGUAGE TemplateHaskell #-}
2+
1--------------------------------------------------------------------------------
3--------------------------------------------------------------------------------
2-- | A Module that allows easy rendering of RSS feeds.
4-- | A Module that allows easy rendering of RSS feeds.
3--
5--
@@ -25,7 +27,6 @@ module Hakyll.Web.Feed
25
27
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
34
35
35
36
36--------------------------------------------------------------------------------
37--------------------------------------------------------------------------------
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")
38
54
39
55
40--------------------------------------------------------------------------------
56--------------------------------------------------------------------------------
@@ -55,15 +71,15 @@ data FeedConfiguration = FeedConfiguration
55
71
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
67
83
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 "]]&gt;")
91 protectCDATA = replaceAll "]]>" (const "]]&gt;")
76− -- Auxiliary: load a template from a datafile
77− loadTemplate path = do
78− file <- compilerUnsafeIO $ getDataFileName path
79− unsafeReadTemplateFile file
80
92
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
107
119
120+ readTemplateFile :: String -> Compiler Template
121+ readTemplateFile value = pure $ template $ readTemplateElems value
122+
108
123
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)
118
133
119
134
@@ -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)
129
144
130
145
lib/Hakyll/Web/Template.hs+2
@@ -140,6 +140,8 @@
140--
140--
141module Hakyll.Web.Template
141module Hakyll.Web.Template
142 ( Template
142 ( Template
143+ , template
144+ , readTemplateElems
143 , templateBodyCompiler
145 , templateBodyCompiler
144 , templateCompiler
146 , templateCompiler
145 , applyTemplate
147 , applyTemplate
lib/Hakyll/Web/Template/Internal.hs−1
@@ -200,4 +200,3 @@ unsafeReadTemplateFile :: FilePath -> Compiler Template
200unsafeReadTemplateFile file = do
200unsafeReadTemplateFile file = do
201 tpl <- unsafeCompiler $ readFile file
201 tpl <- unsafeCompiler $ readFile file
202 pure $ template $ readTemplateElemsFile file tpl
202 pure $ template $ readTemplateElemsFile file tpl
203−