module Text.HTML.TagSoup.HT.Tag where
import Data.Char (toLower, toUpper, )
import Data.Maybe (mapMaybe, )
type Attribute char = (String,[char])
data T char =
Open String [Attribute char]
| Close String
| Text [char]
| Comment String
| Special String String
| Processing String (Processing char)
| Warning String
deriving (Show, Eq, Ord)
data Processing char =
KnownProcessing [Attribute char]
| UnknownProcessing String
deriving (Show, Eq, Ord)
isOpen :: T char -> Bool
isOpen tag = case tag of (Open {}) -> True; _ -> False
maybeOpen :: T char -> Maybe (String, [Attribute char])
maybeOpen tag = case tag of Open name attrs -> Just (name, attrs); _ -> Nothing
isClose :: T char -> Bool
isClose tag = case tag of (Close {}) -> True; _ -> False
maybeClose :: T char -> Maybe String
maybeClose tag = case tag of Close x -> Just x; _ -> Nothing
isText :: T char -> Bool
isText tag = case tag of (Text {}) -> True; _ -> False
maybeText :: T char -> Maybe [char]
maybeText tag = case tag of Text x -> Just x; _ -> Nothing
innerText :: [T char] -> [char]
innerText = concat . mapMaybe maybeText
isComment :: T char -> Bool
isComment tag = case tag of (Comment {}) -> True; _ -> False
maybeComment :: T char -> Maybe String
maybeComment tag = case tag of Comment x -> Just x; _ -> Nothing
isSpecial :: T char -> Bool
isSpecial tag = case tag of (Special {}) -> True; _ -> False
maybeSpecial :: T char -> Maybe (String, String)
maybeSpecial tag = case tag of Special name content -> Just (name, content); _ -> Nothing
isProcessing :: T char -> Bool
isProcessing tag = case tag of (Processing {}) -> True; _ -> False
maybeProcessing :: T char -> Maybe (String, Processing char)
maybeProcessing tag = case tag of Processing target instr -> Just (target, instr); _ -> Nothing
isWarning :: T char -> Bool
isWarning tag = case tag of (Warning {}) -> True; _ -> False
maybeWarning :: T char -> Maybe String
maybeWarning tag = case tag of Warning x -> Just x; _ -> Nothing
canonicalizeSoup :: [T char] -> [T char]
canonicalizeSoup =
map canonicalize
canonicalize :: T char -> T char
canonicalize t =
case t of
Open name attrs -> Open (map toLower name) attrs
Close name -> Close (map toLower name)
Special name info -> Special (map toUpper name) info
_ -> t
textFromCData :: T Char -> T Char
textFromCData t =
case t of
Special "[CDATA[" text -> Text text
_ -> t