diff options
author | Jasper Van der Jeugt <jaspervdj@gmail.com> | 2011-02-09 18:11:24 +0100 |
---|---|---|
committer | Jasper Van der Jeugt <jaspervdj@gmail.com> | 2011-02-09 18:11:24 +0100 |
commit | 002cf4de32db979d515c2a9cdcd8c8f42859a797 (patch) | |
tree | 6992a3f05e693116ae6802ef48448a5a03aded1e /src/Hakyll/Web/Template/Read | |
parent | 7da7e0b96c245a14122896c24dcee52f038e583a (diff) | |
download | hakyll-002cf4de32db979d515c2a9cdcd8c8f42859a797.tar.gz |
Add hamlet templates and restructure tests
Diffstat (limited to 'src/Hakyll/Web/Template/Read')
-rw-r--r-- | src/Hakyll/Web/Template/Read/Hakyll.hs | 36 | ||||
-rw-r--r-- | src/Hakyll/Web/Template/Read/Hamlet.hs | 50 |
2 files changed, 86 insertions, 0 deletions
diff --git a/src/Hakyll/Web/Template/Read/Hakyll.hs b/src/Hakyll/Web/Template/Read/Hakyll.hs new file mode 100644 index 0000000..fbbfee2 --- /dev/null +++ b/src/Hakyll/Web/Template/Read/Hakyll.hs @@ -0,0 +1,36 @@ +-- | Read templates in Hakyll's native format +-- +module Hakyll.Web.Template.Read.Hakyll + ( readTemplate + ) where + +import Data.List (isPrefixOf) +import Data.Char (isAlphaNum) +import Data.Maybe (fromMaybe) + +import Hakyll.Web.Template.Internal + +-- | Construct a @Template@ from a string. +-- +readTemplate :: String -> Template +readTemplate = Template . readTemplate' + where + readTemplate' [] = [] + readTemplate' string + | "$$" `isPrefixOf` string = + Escaped : readTemplate' (drop 2 string) + | "$" `isPrefixOf` string = + case readIdentifier (drop 1 string) of + Just (key, rest) -> Identifier key : readTemplate' rest + Nothing -> Chunk "$" : readTemplate' (drop 1 string) + | otherwise = + let (chunk, rest) = break (== '$') string + in Chunk chunk : readTemplate' rest + + -- Parse an identifier into (identifier, rest) if it's valid, and return + -- Nothing otherwise + readIdentifier string = + let (identifier, rest) = span isAlphaNum string + in if not (null identifier) && "$" `isPrefixOf` rest + then Just (identifier, drop 1 rest) + else Nothing diff --git a/src/Hakyll/Web/Template/Read/Hamlet.hs b/src/Hakyll/Web/Template/Read/Hamlet.hs new file mode 100644 index 0000000..1c9bbf6 --- /dev/null +++ b/src/Hakyll/Web/Template/Read/Hamlet.hs @@ -0,0 +1,50 @@ +-- | Read templates in the hamlet format +-- +{-# LANGUAGE MultiParamTypeClasses #-} +module Hakyll.Web.Template.Read.Hamlet + ( readHamletTemplate + , readHamletTemplateWith + ) where + +import Control.Monad.Trans (liftIO) +import System.FilePath (takeExtension) + +import Text.Hamlet (HamletSettings (..), defaultHamletSettings) +import Text.Hamlet.RT +import Control.Failure + +import Hakyll.Web.Template.Internal + +-- | Read a hamlet template using the default settings +-- +readHamletTemplate :: String -> Template +readHamletTemplate = readHamletTemplateWith defaultHamletSettings + +-- | Read a hamlet template using the specified settings +-- +readHamletTemplateWith :: HamletSettings -> String -> Template +readHamletTemplateWith settings string = + let result = parseHamletRT settings string + in case result of + Just hamlet -> fromHamletRT hamlet + Nothing -> error + "Hakyll.Web.Template.Read.Hamlet.readHamletTemplateWith: \ + \Could not parse Hamlet file" + +-- | Convert a 'HamletRT' to a 'Template' +-- +fromHamletRT :: HamletRT -- ^ Hamlet runtime template + -> Template -- ^ Hakyll template +fromHamletRT (HamletRT sd) = Template $ map fromSimpleDoc sd + where + fromSimpleDoc :: SimpleDoc -> TemplateElement + fromSimpleDoc (SDRaw chunk) = Chunk chunk + fromSimpleDoc (SDVar [var]) = Identifier var + fromSimpleDoc (SDVar _) = error + "Hakyll.Web.Template.Read.Hamlet.fromHamletRT: \ + \Hakyll does not support '.' in identifier names when using \ + \hamlet templates." + fromSimpleDoc _ = error + "Hakyll.Web.Template.Read.Hamlet.fromHamletRT: \ + \Only simple $key$ identifiers are allowed when using hamlet \ + \templates." |