2010-12-30 20:19:19 +00:00
|
|
|
-- | Module containing the template data structure
|
|
|
|
--
|
|
|
|
{-# LANGUAGE GeneralizedNewtypeDeriving, DeriveDataTypeable #-}
|
|
|
|
module Hakyll.Web.Template.Internal
|
|
|
|
( Template (..)
|
|
|
|
, TemplateElement (..)
|
|
|
|
) where
|
|
|
|
|
|
|
|
import Control.Applicative ((<$>))
|
|
|
|
|
|
|
|
import Data.Binary (Binary, get, getWord8, put, putWord8)
|
|
|
|
import Data.Typeable (Typeable)
|
|
|
|
|
|
|
|
import Hakyll.Core.Writable
|
|
|
|
|
|
|
|
-- | Datatype used for template substitutions.
|
|
|
|
--
|
|
|
|
newtype Template = Template
|
|
|
|
{ unTemplate :: [TemplateElement]
|
|
|
|
}
|
|
|
|
deriving (Show, Eq, Binary, Typeable)
|
|
|
|
|
|
|
|
instance Writable Template where
|
|
|
|
-- Writing a template is impossible
|
|
|
|
write _ _ = return ()
|
|
|
|
|
|
|
|
-- | Elements of a template.
|
|
|
|
--
|
|
|
|
data TemplateElement
|
|
|
|
= Chunk String
|
|
|
|
| Identifier String
|
2011-01-02 11:49:43 +00:00
|
|
|
| Escaped String
|
2010-12-30 20:19:19 +00:00
|
|
|
deriving (Show, Eq, Typeable)
|
|
|
|
|
|
|
|
instance Binary TemplateElement where
|
|
|
|
put (Chunk string) = putWord8 0 >> put string
|
|
|
|
put (Identifier key) = putWord8 1 >> put key
|
2011-01-02 11:49:43 +00:00
|
|
|
put (Escaped key) = putWord8 2 >> put key
|
2010-12-30 20:19:19 +00:00
|
|
|
|
|
|
|
get = getWord8 >>= \tag -> case tag of
|
2011-01-02 11:49:43 +00:00
|
|
|
0 -> Chunk <$> get
|
2010-12-30 20:19:19 +00:00
|
|
|
1 -> Identifier <$> get
|
2011-01-02 11:49:43 +00:00
|
|
|
2 -> Escaped <$> get
|
2010-12-30 20:19:19 +00:00
|
|
|
_ -> error "Error reading cached template"
|