{-# LANGUAGE MultiParamTypeClasses, TypeSynonymInstances #-}
module Encoding.Html
    ( entityOfName
    , entityToName
    ) where

import qualified Data.Map as Map
import Data.Char
import Encoding
import Text.XHtml.Transitional hiding (content)

instance Encoding Html String where
    encode = stringToHtml
    decode = error $ "decode Html"	-- This is not meaningful.

{-
instance Monoid Html where
    mempty = noHtml
    mappend = concatHtml
-}

entityOfName name = Map.lookup name entityMap
entityToName entity = Map.lookup entity unicodeMap

entityMap = Map.fromList entityTable
unicodeMap = Map.fromList (map (\ (a, b) -> (b, a)) entityTable)

-- |Table of conversions from HTML entity names to unicode characters.
-- This does not include the conversions that stringToHtml does: amp,
-- lt, gt, and quot.
entityTable = [ ("equiv", chr 0x2261)
              , ("asymp", chr 0x2248)		-- 	\approx 	 	
              , ("prop", chr 0x221d)		-- 	\propto 	 	
              , ("sim", chr 0x223c)		-- 	\sim 	 	
              , ("ne", chr 0x2260)		-- 	\neq 	 	
	      , ("not", chr 172)			-- '¬',\neg 	 	
              , ("and", chr 0x2227)		-- 	\wedge 	 	
              , ("or", chr 0x2228)		-- 	\vee 	 	
              , ("oplus", chr 0x2295)		-- 	\oplus 	 	
              , ("exist", chr 0x2203)		-- 	\exists 	 	
              , ("forall", chr 0x2200)		-- 	\forall 	 	
              , ("cap", chr 0x2229)		-- 	\cap 	 	
              , ("cup", chr 0x222a)		-- 	\cup 	 	
              , ("sup", chr 0x2283)		-- 	\supset 	 	
              , ("sub", chr 0x2282)		-- 	\subset 	 	
              , ("empty", chr 0x2205)		-- 	\emptyset 	 	
              , ("isin", chr 0x2208)		-- 	\in 	 	
              , ("notin", chr 0x2209)		-- 	\notin 	 	
              , ("prime", chr 0x2032)		-- 	\prime 	 	
              , ("rfloor", chr 0x230b)		-- 	\rfloor 	 	
              , ("infin", chr 0x221e)		-- 	\infty 	 	
              , ("eacute", chr 233)		-- 'é',	\'e
	      , ("egrave", chr 232)		-- 'è', \`e 	 	
	      , ("ecirc", chr 234)		-- 'ê', \^e 	 	
	      , ("euml", chr 235) 		-- 'ë'
	      , ("ntilde", chr 241) 		-- 'ñ', \~n 	 	
	      , ("aring", chr 229) 		-- 'å', \aa 	 	
	      , ("iuml", chr 239) 		-- 'ï',	
	      , ("ccedil", chr 231) 		-- 'ç', \c{c} 	 	
	      , ("szlig", chr 223) 		-- 'ß', \ss 	 	
	      , ("aelig", chr 230) 		-- 'æ', \ae 	 	also \AE, \oe and \OE
	      , ("pound", chr 163)		-- '£'	\pounds 	 	
              , ("euro", chr 0x20ac)]		-- \euro
