diff options
author | Jasper Van der Jeugt <jaspervdj@gmail.com> | 2011-02-11 15:34:34 +0100 |
---|---|---|
committer | Jasper Van der Jeugt <jaspervdj@gmail.com> | 2011-02-11 15:34:34 +0100 |
commit | 60cda32b72ce35e4c7c797be91badcb20afbe887 (patch) | |
tree | 984b0c0013f268fc98dc394224503b6b86eeddcd /src/Hakyll/Web/Template.hs | |
parent | dd81433d74579848e6853eb5a1500535c378c808 (diff) | |
download | hakyll-60cda32b72ce35e4c7c797be91badcb20afbe887.tar.gz |
Add transparent reading of hamlet/hakyll templates
Diffstat (limited to 'src/Hakyll/Web/Template.hs')
-rw-r--r-- | src/Hakyll/Web/Template.hs | 30 |
1 files changed, 29 insertions, 1 deletions
diff --git a/src/Hakyll/Web/Template.hs b/src/Hakyll/Web/Template.hs index 06794e8..00c1a27 100644 --- a/src/Hakyll/Web/Template.hs +++ b/src/Hakyll/Web/Template.hs @@ -2,11 +2,19 @@ module Hakyll.Web.Template ( Template , applyTemplate , applySelf + , templateRead + , templateReadWith ) where +import Control.Arrow import Data.Maybe (fromMaybe) import qualified Data.Map as M +import System.FilePath (takeExtension) +import Text.Hamlet (HamletSettings, defaultHamletSettings) + +import Hakyll.Core.Compiler +import Hakyll.Core.Identifier import Hakyll.Web.Template.Internal import Hakyll.Web.Template.Read import Hakyll.Web.Page @@ -20,7 +28,7 @@ applyTemplate template page = fmap (const $ substitute =<< unTemplate template) page where substitute (Chunk chunk) = chunk - substitute (Identifier key) = + substitute (Key key) = fromMaybe ('$' : key) $ M.lookup key $ toMap page substitute (Escaped) = "$" @@ -29,3 +37,23 @@ applyTemplate template page = -- applySelf :: Page String -> Page String applySelf page = applyTemplate (readTemplate $ pageBody page) page + +-- | Read a template. If the extension of the file we're compiling is +-- @.hml@ or @.hamlet@, it will be considered as a Hamlet template, and parsed +-- as such. +-- +templateRead :: Compiler a Template +templateRead = templateReadWith defaultHamletSettings + +-- | Version of 'templateRead' that enables custom settings. +-- +templateReadWith :: HamletSettings -> Compiler a Template +templateReadWith settings = + getIdentifier &&& getResourceString >>^ uncurry read' + where + read' identifier string = + if takeExtension (toFilePath identifier) `elem` [".hml", ".hamlet"] + -- Hamlet template + then readHamletTemplateWith settings string + -- Hakyll template + else readTemplate string |