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 @@ -23,10 +23,6 @@ module Hakyll.Web.Feed ) where --------------------------------------------------------------------------------- -import Control.Monad ((<=<)) - - -------------------------------------------------------------------------------- import Hakyll.Core.Compiler import Hakyll.Core.Compiler.Internal @@ -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 @@ -- >

-- > $for(counts)-$ -- > $count$ --- > $-sep-$... +-- > $-sep$... -- > $-endfor$ -- >

-- @@ -148,6 +148,7 @@ module Hakyll.Web.Template , loadAndApplyTemplate , applyAsTemplate , readTemplate + , unsafeReadTemplateFile ) where @@ -188,24 +189,31 @@ instance IsString Template where fromString = readTemplate +-------------------------------------------------------------------------------- +-- | 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 (:[]) diff --git a/web/tutorials/01-installation.markdown b/web/tutorials/01-installation.markdown index a8ffc00..b8dff9b 100644 --- a/web/tutorials/01-installation.markdown +++ b/web/tutorials/01-installation.markdown @@ -32,13 +32,13 @@ content and a generic configuration. If `hakyll-init` is not found, you should make sure your stack bin path (usually `$HOME/.local/bin`) is in your `$PATH`. You can check your stack local -bin path by running `stack path --local-bin-path`. +bin path by running `stack path --local-bin`. The file `site.hs` holds the configuration of your site, as an executable haskell program. We can compile and run it like this: $ cd my-site - $ stack init # Optional, if you haven't used stack before + $ stack init # creates stack.yaml file based on my-site.cabal $ stack build $ stack exec site build diff --git a/web/tutorials/external-live-reload.md b/web/tutorials/external-live-reload.md new file mode 100644 index 0000000..38f1c96 --- /dev/null +++ b/web/tutorials/external-live-reload.md @@ -0,0 +1,6 @@ +--- +title: Live Reloading with Hakyll +author: Ben Kolera +url: 'http://benkolera.com/posts/2015-09-14-hakyll_livereload.html' +external: true +---