diff options
Diffstat (limited to 'src/Text/Hakyll/Internal/Template/Template.hs')
-rw-r--r-- | src/Text/Hakyll/Internal/Template/Template.hs | 43 |
1 files changed, 23 insertions, 20 deletions
diff --git a/src/Text/Hakyll/Internal/Template/Template.hs b/src/Text/Hakyll/Internal/Template/Template.hs index 0fb2d09..49373fd 100644 --- a/src/Text/Hakyll/Internal/Template/Template.hs +++ b/src/Text/Hakyll/Internal/Template/Template.hs @@ -1,31 +1,34 @@ -- | Module containing the template data structure. -- +{-# LANGUAGE GeneralizedNewtypeDeriving #-} module Text.Hakyll.Internal.Template.Template ( Template (..) + , TemplateElement (..) ) where -import Control.Monad (liftM, liftM2) -import Data.Word (Word8) +import Control.Applicative ((<$>)) -import Data.Binary (Binary, get, put, getWord8) +import Data.Binary (Binary, get, getWord8, put, putWord8) -- | Datatype used for template substitutions. -- -data Template = Chunk String Template - | Identifier String Template - | EscapeCharacter Template - | End - deriving (Show, Read, Eq) - -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) +newtype Template = Template { unTemplate :: [TemplateElement] } + deriving (Show, Eq, Binary) - 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" +-- | Elements of a template. +-- +data TemplateElement = Chunk String + | Identifier String + | EscapeCharacter + deriving (Show, Eq) + +instance Binary TemplateElement where + put (Chunk string) = putWord8 0 >> put string + put (Identifier key) = putWord8 1 >> put key + put (EscapeCharacter) = putWord8 2 + + get = getWord8 >>= \tag -> + case tag of 0 -> Chunk <$> get + 1 -> Identifier <$> get + 2 -> return EscapeCharacter + _ -> error "Error reading cached template" |