summaryrefslogtreecommitdiff
path: root/src/Text/Hakyll/Internal/Template.hs
diff options
context:
space:
mode:
authorJasper Van der Jeugt <jaspervdj@gmail.com>2010-08-05 18:41:15 +0200
committerJasper Van der Jeugt <jaspervdj@gmail.com>2010-08-05 18:41:15 +0200
commit69ca4255f07b147082614af34a883b557f456a74 (patch)
treef90471aaba81b3208dbee2bd8e921c70c3318f87 /src/Text/Hakyll/Internal/Template.hs
parent2c8d76cd97e670fdfd216e2288b8cc6633287046 (diff)
downloadhakyll-69ca4255f07b147082614af34a883b557f456a74.tar.gz
Hamlet support (experimental)
Diffstat (limited to 'src/Text/Hakyll/Internal/Template.hs')
-rw-r--r--src/Text/Hakyll/Internal/Template.hs38
1 files changed, 12 insertions, 26 deletions
diff --git a/src/Text/Hakyll/Internal/Template.hs b/src/Text/Hakyll/Internal/Template.hs
index bd8db2c..54636ee 100644
--- a/src/Text/Hakyll/Internal/Template.hs
+++ b/src/Text/Hakyll/Internal/Template.hs
@@ -10,8 +10,6 @@ module Text.Hakyll.Internal.Template
import Control.Applicative ((<$>))
import Data.List (isPrefixOf)
import Data.Char (isAlphaNum)
-import Data.Binary
-import Control.Monad (liftM, liftM2)
import Data.Maybe (fromMaybe)
import System.FilePath ((</>))
import qualified Data.Map as M
@@ -20,13 +18,8 @@ import Text.Hakyll.Context (Context (..))
import Text.Hakyll.HakyllMonad (Hakyll)
import Text.Hakyll.Internal.Cache
import Text.Hakyll.Internal.Page
-
--- | Datatype used for template substitutions.
-data Template = Chunk String Template
- | Identifier String Template
- | EscapeCharacter Template
- | End
- deriving (Show, Read, Eq)
+import Text.Hakyll.Internal.Template.Template
+import Text.Hakyll.Internal.Template.Hamlet
-- | Construct a @Template@ from a string.
fromString :: String -> Template
@@ -48,14 +41,20 @@ readTemplate path = do
if isCacheMoreRecent'
then getFromCache fileName
else do
- page <- unContext <$> readPage path
- let body = fromMaybe (error $ "No body in template " ++ fileName)
- (M.lookup "body" page)
- template = fromString body
+ template <- if isHamletRTFile path
+ then readHamletTemplate
+ else readDefaultTemplate
storeInCache template fileName
return template
where
fileName = "templates" </> path
+ readDefaultTemplate = do
+ page <- unContext <$> readPage path
+ let body = fromMaybe (error $ "No body in template " ++ fileName)
+ (M.lookup "body" page)
+ return $ fromString body
+
+ readHamletTemplate = fromHamletRT <$> readHamletRT path
-- | Substitutes @$identifiers@ in the given @Template@ by values from the given
-- "Context". When a key is not found, it is left as it is. You can specify
@@ -80,16 +79,3 @@ regularSubstitute = substitute "$$"
-- escaped characters.
finalSubstitute :: Template -> Context -> String
finalSubstitute = substitute "$"
-
-instance Binary Template where
- put (Chunk string template) = put (0 :: Word8) >> put string >> put template
- put (Identifier key template) = put (1 :: Word8) >> put key >> put template
- put (EscapeCharacter template) = put (2 :: Word8) >> put template
- put (End) = put (3 :: Word8)
-
- get = do tag <- getWord8
- case tag of 0 -> liftM2 Chunk get get
- 1 -> liftM2 Identifier get get
- 2 -> liftM EscapeCharacter get
- 3 -> return End
- _ -> error "Error reading template"