{-# LANGUAGE DeriveDataTypeable #-}
module Text.HTML.TagSoup.Options where
import Data.Typeable
import Text.HTML.TagSoup.Type
import Text.HTML.TagSoup.Entity
import Text.StringLike
data ParseOptions str = ParseOptions
{optTagPosition :: Bool
,optTagWarning :: Bool
,optEntityData :: (str,Bool) -> [Tag str]
,optEntityAttrib :: (str,Bool) -> (str,[Tag str])
,optTagTextMerge :: Bool
}
deriving Typeable
parseOptionsEntities :: StringLike str => (str -> Maybe str) -> ParseOptions str
parseOptionsEntities lookupEntity = ParseOptions False False entityData entityAttrib True
where
entityData x = TagText a : b
where (a,b) = entityAttrib x
entityAttrib ~(x,b) = case lookupEntity x of
Just y -> (y, [])
Nothing -> (fromChar '&' `append` x `append` fromString [';'|b]
,[TagWarning $ fromString "Unknown entity: " `append` x])
parseOptions :: StringLike str => ParseOptions str
parseOptions = parseOptionsEntities $ fmap fromString . lookupEntity . toString
parseOptionsFast :: StringLike str => ParseOptions str
parseOptionsFast = parseOptions{optTagTextMerge=False}
fmapParseOptions :: (StringLike from, StringLike to) => ParseOptions from -> ParseOptions to
fmapParseOptions (ParseOptions a b c d e) = ParseOptions a b c2 d2 e
where
c2 ~(x,y) = map (fmap castString) $ c (castString x, y)
d2 ~(x,y) = (castString r, map (fmap castString) s)
where (r,s) = d (castString x, y)