{-# LANGUAGE FlexibleInstances, UndecidableInstances #-}
{-# LANGUAGE DeriveDataTypeable #-}
module Hyper.Internal (
Graphic(..), string, html,
Display(..),
displayIO,
) where
import Control.DeepSeq
import Data.List (isPrefixOf)
import Data.Typeable
import qualified Data.Text as T
import qualified Data.Text.Lazy as TL
import qualified Text.Blaze.Html as H
import qualified Text.Blaze.Html.Renderer.Text as H
data Graphic = Graphic { gHtml :: T.Text } deriving (Typeable)
instance NFData Graphic where rnf g = rnf (gHtml g)
string :: String -> Graphic
string = Graphic . TL.toStrict . H.renderHtml . H.toHtml
html :: T.Text -> Graphic
html = Graphic
class Display a where
display :: a -> Graphic
instance Display () where display x = x `seq` fromShow x
instance Display Graphic where display = id
instance Display Bool where display = fromShow
instance Display Double where display = fromShow
instance Display Integer where display = fromShow
instance Display Int where display = fromShow
instance Display String where display = fromShow
instance Display [Int] where display = displayList
instance Display [String] where display = displayList
fromShow :: Show a => a -> Graphic
fromShow = string . show
displayList :: Show a => [a] -> Graphic
displayList = fromShow
class DisplayIO a where
displayIO :: a -> IO Graphic
instance Display a => DisplayIO (IO a) where
displayIO = fmap display
instance Display a => DisplayIO a where
displayIO = return . display