@@ -10,8 +10,6 @@ module Text.Hakyll.Internal.Template
10import Control.Applicative ((<$>))
10import Control.Applicative ((<$>))
11import Data.List (isPrefixOf)
11import Data.List (isPrefixOf)
12import Data.Char (isAlphaNum)
12import Data.Char (isAlphaNum)
14−import Control.Monad (liftM, liftM2)
15import Data.Maybe (fromMaybe)
13import Data.Maybe (fromMaybe)
16import System.FilePath ((</>))
14import System.FilePath ((</>))
17import qualified Data.Map as M
15import qualified Data.Map as M
@@ -20,13 +18,8 @@ import Text.Hakyll.Context (Context (..))
20import Text.Hakyll.HakyllMonad (Hakyll)
18import Text.Hakyll.HakyllMonad (Hakyll)
21import Text.Hakyll.Internal.Cache
19import Text.Hakyll.Internal.Cache
22import Text.Hakyll.Internal.Page
20import Text.Hakyll.Internal.Page
24−-- | Datatype used for template substitutions.
25−data Template = Chunk String Template
21+import Text.Hakyll.Internal.Template.Template
26− | Identifier String Template
22+import Text.Hakyll.Internal.Template.Hamlet
27− | EscapeCharacter Template
29− deriving (Show, Read, Eq)
31-- | Construct a @Template@ from a string.
24-- | Construct a @Template@ from a string.
32fromString :: String -> Template
25fromString :: String -> Template
@@ -48,14 +41,20 @@ readTemplate path = do
48 if isCacheMoreRecent'
41 if isCacheMoreRecent'
49 then getFromCache fileName
42 then getFromCache fileName
51− page <- unContext <$> readPage path
44+ template <- if isHamletRTFile path
52− let body = fromMaybe (error $ "No body in template " ++ fileName)
45+ then readHamletTemplate
53− (M.lookup "body" page)
46+ else readDefaultTemplate
54− template = fromString body
55 storeInCache template fileName
47 storeInCache template fileName
56 return template
48 return template
58 fileName = "templates" </> path
50 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
57+ readHamletTemplate = fromHamletRT <$> readHamletRT path
60-- | Substitutes @$identifiers@ in the given @Template@ by values from the given
59-- | Substitutes @$identifiers@ in the given @Template@ by values from the given
61-- "Context". When a key is not found, it is left as it is. You can specify
60-- "Context". When a key is not found, it is left as it is. You can specify
@@ -80,16 +79,3 @@ regularSubstitute = substitute "$$"
80-- escaped characters.
79-- escaped characters.
81finalSubstitute :: Template -> Context -> String
80finalSubstitute :: Template -> Context -> String
82finalSubstitute = substitute "$"
81finalSubstitute = substitute "$"
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)
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
95− _ -> error "Error reading template"