@@ -10,8 +10,6 @@ module Text.Hakyll.Internal.Template
1010import Control.Applicative ((<$>))
1111import Data.List (isPrefixOf)
1212import Data.Char (isAlphaNum)
13−import Data.Binary
14−import Control.Monad (liftM, liftM2)
1513import Data.Maybe (fromMaybe)
1614import System.FilePath ((</>))
1715import qualified Data.Map as M
@@ -20,13 +18,8 @@ import Text.Hakyll.Context (Context (..))
2018import Text.Hakyll.HakyllMonad (Hakyll)
2119import Text.Hakyll.Internal.Cache
2220import Text.Hakyll.Internal.Page
23−
24−-- | Datatype used for template substitutions.
25−data Template = Chunk String Template
26− | Identifier String Template
27− | EscapeCharacter Template
28− | End
29− deriving (Show, Read, Eq)
21+import Text.Hakyll.Internal.Template.Template
22+import Text.Hakyll.Internal.Template.Hamlet
3023
3124-- | Construct a @Template@ from a string.
3225fromString :: String -> Template
@@ -48,14 +41,20 @@ readTemplate path = do
4841 if isCacheMoreRecent'
4942 then getFromCache fileName
5043 else do
51− page <- unContext <$> readPage path
52− let body = fromMaybe (error $ "No body in template " ++ fileName)
53− (M.lookup "body" page)
54− template = fromString body
44+ template <- if isHamletRTFile path
45+ then readHamletTemplate
46+ else readDefaultTemplate
5547 storeInCache template fileName
5648 return template
5749 where
5850 fileName = "templates" </> path
51+ readDefaultTemplate = do
52+ page <- unContext <$> readPage path
53+ let body = fromMaybe (error $ "No body in template " ++ fileName)
54+ (M.lookup "body" page)
55+ return $ fromString body
56+
57+ readHamletTemplate = fromHamletRT <$> readHamletRT path
5958
6059-- | Substitutes @$identifiers@ in the given @Template@ by values from the given
6160-- "Context". When a key is not found, it is left as it is. You can specify
@@ -80,16 +79,3 @@ regularSubstitute = substitute "$$"
8079-- escaped characters.
8180finalSubstitute :: Template -> Context -> String
8281finalSubstitute = substitute "$"
83−
84−instance Binary Template where
85− put (Chunk string template) = put (0 :: Word8) >> put string >> put template
86− put (Identifier key template) = put (1 :: Word8) >> put key >> put template
87− put (EscapeCharacter template) = put (2 :: Word8) >> put template
88− put (End) = put (3 :: Word8)
89−
90− get = do tag <- getWord8
91− case tag of 0 -> liftM2 Chunk get get
92− 1 -> liftM2 Identifier get get
93− 2 -> liftM EscapeCharacter get
94− 3 -> return End
95− _ -> error "Error reading template"