diff options
Diffstat (limited to 'src/Hakyll/Web/Template/Read.hs')
-rw-r--r-- | src/Hakyll/Web/Template/Read.hs | 44 |
1 files changed, 38 insertions, 6 deletions
diff --git a/src/Hakyll/Web/Template/Read.hs b/src/Hakyll/Web/Template/Read.hs index 421b7e9..f8583ff 100644 --- a/src/Hakyll/Web/Template/Read.hs +++ b/src/Hakyll/Web/Template/Read.hs @@ -1,10 +1,42 @@ --- | Re-exports all different template reading modules --- +-------------------------------------------------------------------------------- +-- | Read templates in Hakyll's native format module Hakyll.Web.Template.Read ( readTemplate - , readHamletTemplate - , readHamletTemplateWith ) where -import Hakyll.Web.Template.Read.Hakyll -import Hakyll.Web.Template.Read.Hamlet + +-------------------------------------------------------------------------------- +import Data.List (isPrefixOf) +import Data.Char (isAlphaNum) + + +-------------------------------------------------------------------------------- +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 readKey (drop 1 string) of + Just (key, rest) -> Key key : readTemplate' rest + Nothing -> Chunk "$" : readTemplate' (drop 1 string) + | otherwise = + let (chunk, rest) = break (== '$') string + in Chunk chunk : readTemplate' rest + + -- Parse an key into (key, rest) if it's valid, and return + -- Nothing otherwise + readKey string = + let (key, rest) = span validKeyChar string + in if not (null key) && "$" `isPrefixOf` rest + then Just (key, drop 1 rest) + else Nothing + + validKeyChar x = isAlphaNum x || x == ' ' |