From 526cd35f418fe14bb0039900a00a0e024686ee34 Mon Sep 17 00:00:00 2001 From: Roman Kuznetsov Date: Sun, 7 Jan 2018 12:17:04 +0100 Subject: Embed feed templates rather than using data-files --- lib/Hakyll/Web/Feed.hs | 41 +++++++++++++++++++++++++------------ lib/Hakyll/Web/Template.hs | 2 ++ lib/Hakyll/Web/Template/Internal.hs | 1 - 3 files changed, 30 insertions(+), 14 deletions(-) (limited to 'lib/Hakyll') diff --git a/lib/Hakyll/Web/Feed.hs b/lib/Hakyll/Web/Feed.hs index 6c6fa76..916a2fa 100644 --- a/lib/Hakyll/Web/Feed.hs +++ b/lib/Hakyll/Web/Feed.hs @@ -1,3 +1,5 @@ +{-# LANGUAGE TemplateHaskell #-} + -------------------------------------------------------------------------------- -- | A Module that allows easy rendering of RSS feeds. -- @@ -25,7 +27,6 @@ module Hakyll.Web.Feed -------------------------------------------------------------------------------- import Hakyll.Core.Compiler -import Hakyll.Core.Compiler.Internal import Hakyll.Core.Item import Hakyll.Core.Util.String (replaceAll) import Hakyll.Web.Template @@ -34,7 +35,22 @@ import Hakyll.Web.Template.List -------------------------------------------------------------------------------- -import Paths_hakyll +import Data.ByteString.Char8 (unpack) +import Data.FileEmbed (embedFile) + + +-------------------------------------------------------------------------------- +rssTemplate :: String +rssTemplate = unpack $(embedFile "data/templates/rss.xml") + +rssItemTemplate :: String +rssItemTemplate = unpack $(embedFile "data/templates/rss-item.xml") + +atomTemplate :: String +atomTemplate = unpack $(embedFile "data/templates/atom.xml") + +atomItemTemplate :: String +atomItemTemplate = unpack $(embedFile "data/templates/atom-item.xml") -------------------------------------------------------------------------------- @@ -55,15 +71,15 @@ data FeedConfiguration = FeedConfiguration -------------------------------------------------------------------------------- -- | Abstract function to render any feed. -renderFeed :: FilePath -- ^ Feed template - -> FilePath -- ^ Item template +renderFeed :: String -- ^ Default feed template + -> String -- ^ Default item template -> FeedConfiguration -- ^ Feed configuration -> Context String -- ^ Context for the items -> [Item String] -- ^ Input items -> Compiler (Item String) -- ^ Resulting item -renderFeed feedPath itemPath config itemContext items = do - feedTpl <- loadTemplate feedPath - itemTpl <- loadTemplate itemPath +renderFeed defFeed defItem config itemContext items = do + feedTpl <- readTemplateFile defFeed + itemTpl <- readTemplateFile defItem protectedItems <- mapM (applyFilter protectCDATA) items body <- makeItem =<< applyTemplateList itemTpl itemContext' protectedItems @@ -73,10 +89,6 @@ renderFeed feedPath itemPath config itemContext items = do applyFilter tr str = return $ fmap tr str protectCDATA :: String -> String protectCDATA = replaceAll "]]>" (const "]]>") - -- Auxiliary: load a template from a datafile - loadTemplate path = do - file <- compilerUnsafeIO $ getDataFileName path - unsafeReadTemplateFile file itemContext' = mconcat [ itemContext @@ -105,6 +117,9 @@ renderFeed feedPath itemPath config itemContext items = do ListField _ _ -> fail "Hakyll.Web.Feed.renderFeed: Internal error" StringField s -> return s + readTemplateFile :: String -> Compiler Template + readTemplateFile value = pure $ template $ readTemplateElems value + -------------------------------------------------------------------------------- -- | Render an RSS feed with a number of items. @@ -113,7 +128,7 @@ renderRss :: FeedConfiguration -- ^ Feed configuration -> [Item String] -- ^ Feed items -> Compiler (Item String) -- ^ Resulting feed renderRss config context = renderFeed - "templates/rss.xml" "templates/rss-item.xml" config + rssTemplate rssItemTemplate config (makeItemContext "%a, %d %b %Y %H:%M:%S UT" context) @@ -124,7 +139,7 @@ renderAtom :: FeedConfiguration -- ^ Feed configuration -> [Item String] -- ^ Feed items -> Compiler (Item String) -- ^ Resulting feed renderAtom config context = renderFeed - "templates/atom.xml" "templates/atom-item.xml" config + atomTemplate atomItemTemplate config (makeItemContext "%Y-%m-%dT%H:%M:%SZ" context) diff --git a/lib/Hakyll/Web/Template.hs b/lib/Hakyll/Web/Template.hs index 2a9684b..a436106 100644 --- a/lib/Hakyll/Web/Template.hs +++ b/lib/Hakyll/Web/Template.hs @@ -140,6 +140,8 @@ -- module Hakyll.Web.Template ( Template + , template + , readTemplateElems , templateBodyCompiler , templateCompiler , applyTemplate diff --git a/lib/Hakyll/Web/Template/Internal.hs b/lib/Hakyll/Web/Template/Internal.hs index d0e4d47..154cee6 100644 --- a/lib/Hakyll/Web/Template/Internal.hs +++ b/lib/Hakyll/Web/Template/Internal.hs @@ -200,4 +200,3 @@ unsafeReadTemplateFile :: FilePath -> Compiler Template unsafeReadTemplateFile file = do tpl <- unsafeCompiler $ readFile file pure $ template $ readTemplateElemsFile file tpl - -- cgit v1.2.3