summaryrefslogtreecommitdiff
path: root/src/Hakyll/Web
diff options
context:
space:
mode:
Diffstat (limited to 'src/Hakyll/Web')
-rw-r--r--src/Hakyll/Web/Feed.hs12
-rw-r--r--src/Hakyll/Web/Template.hs27
-rw-r--r--src/Hakyll/Web/Template/Internal.hs20
3 files changed, 40 insertions, 19 deletions
diff --git a/src/Hakyll/Web/Feed.hs b/src/Hakyll/Web/Feed.hs
index 8598f8a..f40fa8a 100644
--- a/src/Hakyll/Web/Feed.hs
+++ b/src/Hakyll/Web/Feed.hs
@@ -24,10 +24,6 @@ module Hakyll.Web.Feed
--------------------------------------------------------------------------------
-import Control.Monad ((<=<))
-
-
---------------------------------------------------------------------------------
import Hakyll.Core.Compiler
import Hakyll.Core.Compiler.Internal
import Hakyll.Core.Item
@@ -65,14 +61,16 @@ renderFeed :: FilePath -- ^ Feed template
-> [Item String] -- ^ Input items
-> Compiler (Item String) -- ^ Resulting item
renderFeed feedPath itemPath config itemContext items = do
- feedTpl <- compilerUnsafeIO $ loadTemplate feedPath
- itemTpl <- compilerUnsafeIO $ loadTemplate itemPath
+ feedTpl <- loadTemplate feedPath
+ itemTpl <- loadTemplate itemPath
body <- makeItem =<< applyTemplateList itemTpl itemContext' items
applyTemplate feedTpl feedContext body
where
-- Auxiliary: load a template from a datafile
- loadTemplate = fmap readTemplate . readFile <=< getDataFileName
+ loadTemplate path = do
+ file <- compilerUnsafeIO $ getDataFileName path
+ unsafeReadTemplateFile file
itemContext' = mconcat
[ itemContext
diff --git a/src/Hakyll/Web/Template.hs b/src/Hakyll/Web/Template.hs
index 13d5d35..4a8d94c 100644
--- a/src/Hakyll/Web/Template.hs
+++ b/src/Hakyll/Web/Template.hs
@@ -128,7 +128,7 @@
-- > <p>
-- > $for(counts)-$
-- > $count$
--- > $-sep-$...
+-- > $-sep$...
-- > $-endfor$
-- > </p>
--
@@ -148,6 +148,7 @@ module Hakyll.Web.Template
, loadAndApplyTemplate
, applyAsTemplate
, readTemplate
+ , unsafeReadTemplateFile
) where
@@ -189,23 +190,30 @@ instance IsString Template where
--------------------------------------------------------------------------------
+-- | Wrap the constructor to ensure trim is called.
+template :: [TemplateElement] -> Template
+template = Template . trim
+
+
+--------------------------------------------------------------------------------
readTemplate :: String -> Template
readTemplate = Template . trim . readTemplateElems
-
--------------------------------------------------------------------------------
-- | Read a template, without metadata header
templateBodyCompiler :: Compiler (Item Template)
templateBodyCompiler = cached "Hakyll.Web.Template.templateBodyCompiler" $ do
item <- getResourceBody
- return $ fmap readTemplate item
+ file <- getResourceFilePath
+ return $ fmap (template . readTemplateElemsFile file) item
--------------------------------------------------------------------------------
-- | Read complete file contents as a template
templateCompiler :: Compiler (Item Template)
templateCompiler = cached "Hakyll.Web.Template.templateCompiler" $ do
item <- getResourceString
- return $ fmap readTemplate item
+ file <- getResourceFilePath
+ return $ fmap (template . readTemplateElemsFile file) item
--------------------------------------------------------------------------------
@@ -317,5 +325,14 @@ applyAsTemplate :: Context String -- ^ Context
-> Item String -- ^ Item and template
-> Compiler (Item String) -- ^ Resulting item
applyAsTemplate context item =
- let tpl = readTemplate $ itemBody item
+ let tpl = template $ readTemplateElemsFile file (itemBody item)
+ file = toFilePath $ itemIdentifier item
in applyTemplate tpl context item
+
+
+--------------------------------------------------------------------------------
+unsafeReadTemplateFile :: FilePath -> Compiler Template
+unsafeReadTemplateFile file = do
+ tpl <- unsafeCompiler $ readFile file
+ pure $ template $ readTemplateElemsFile file tpl
+
diff --git a/src/Hakyll/Web/Template/Internal.hs b/src/Hakyll/Web/Template/Internal.hs
index 6a9947f..15266a0 100644
--- a/src/Hakyll/Web/Template/Internal.hs
+++ b/src/Hakyll/Web/Template/Internal.hs
@@ -7,6 +7,7 @@ module Hakyll.Web.Template.Internal
, TemplateElement (..)
, templateElems
, readTemplateElems
+ , readTemplateElemsFile
) where
@@ -108,7 +109,12 @@ instance Binary TemplateExpr where
--------------------------------------------------------------------------------
readTemplateElems :: String -> [TemplateElement]
-readTemplateElems input = case P.parse templateElems "" input of
+readTemplateElems = readTemplateElemsFile "{literal}"
+
+
+--------------------------------------------------------------------------------
+readTemplateElemsFile :: FilePath -> String -> [TemplateElement]
+readTemplateElemsFile file input = case P.parse templateElems file input of
Left err -> error $ "Cannot parse template: " ++ show err
Right t -> t
@@ -116,12 +122,12 @@ readTemplateElems input = case P.parse templateElems "" input of
--------------------------------------------------------------------------------
templateElems :: P.Parser [TemplateElement]
templateElems = mconcat <$> P.many (P.choice [ lift chunk
- , lift escaped
- , conditional
- , for
- , partial
- , expr
- ])
+ , lift escaped
+ , conditional
+ , for
+ , partial
+ , expr
+ ])
where lift = fmap (:[])