module Text.Haiji.Types
( Template(..)
, render
, Environment
, autoEscape
, Escape
, escapeBy
, rawEscape
, htmlEscape
, ToLT
, toLT
) where
import Control.Monad.Trans.Reader
import Data.Default
import qualified Data.Text as T
import qualified Data.Text.Lazy as LT
data Escape = Escape { unEscape :: LT.Text -> LT.Text }
escapeBy :: LT.Text -> Escape -> LT.Text
escapeBy = flip unEscape
rawEscape :: Escape
rawEscape = Escape id
htmlEscape :: Escape
htmlEscape = Escape (LT.concatMap replace) where
replace '&' = "&"
replace '"' = """
replace '\'' = "'"
replace '<' = "<"
replace '>' = ">"
replace h = LT.singleton h
data Environment =
Environment
{ autoEscape :: Bool
} deriving (Eq, Show)
instance Default Environment where
def = Environment { autoEscape = True
}
newtype Template dict = Template { unTmpl :: Reader dict LT.Text }
render :: Template dict -> dict -> LT.Text
render = runReader . unTmpl
class ToLT a where toLT :: a -> LT.Text
instance ToLT String where toLT = LT.pack
instance ToLT T.Text where toLT = LT.fromStrict
instance ToLT LT.Text where toLT = id
instance ToLT Int where toLT = toLT . show
instance ToLT Integer where toLT = toLT . show