module Text.HTML.TagSoup.Type(
Tag(..), Attribute, Row, Column,
isTagOpen, isTagClose, isTagText, isTagWarning,
isTagOpenName, isTagCloseName,
fromTagText, fromAttrib,
maybeTagText, maybeTagWarning,
innerText,
) where
import Data.Char
import Data.Maybe
type Attribute = (String,String)
type Row = Int
type Column = Int
data Tag =
TagOpen String [Attribute]
| TagClose String
| TagText String
| TagComment String
| TagWarning String
| TagPosition !Row !Column
deriving (Show, Eq, Ord)
isTagOpen :: Tag -> Bool
isTagOpen (TagOpen {}) = True; isTagOpen _ = False
isTagClose :: Tag -> Bool
isTagClose (TagClose {}) = True; isTagClose _ = False
isTagText :: Tag -> Bool
isTagText (TagText {}) = True; isTagText _ = False
maybeTagText :: Tag -> Maybe String
maybeTagText (TagText x) = Just x
maybeTagText _ = Nothing
fromTagText :: Tag -> String
fromTagText (TagText x) = x
fromTagText x = error ("(" ++ show x ++ ") is not a TagText")
innerText :: [Tag] -> String
innerText = concat . mapMaybe maybeTagText
isTagWarning :: Tag -> Bool
isTagWarning (TagWarning {}) = True; isTagWarning _ = False
maybeTagWarning :: Tag -> Maybe String
maybeTagWarning (TagWarning x) = Just x
maybeTagWarning _ = Nothing
fromAttrib :: String -> Tag -> String
fromAttrib att (TagOpen _ atts) = fromMaybe [] $ lookup att atts
fromAttrib _ x = error ("(" ++ show x ++ ") is not a TagOpen")
isTagOpenName :: String -> Tag -> Bool
isTagOpenName name (TagOpen n _) = n == name
isTagOpenName _ _ = False
isTagCloseName :: String -> Tag -> Bool
isTagCloseName name (TagClose n) = n == name
isTagCloseName _ _ = False