module Text.HTML.TagSoup.Match where
import Text.HTML.TagSoup.Type (Tag(..), Attribute)
import Data.List (tails)
tagOpen :: (str -> Bool) -> ([Attribute str] -> Bool) -> Tag str -> Bool
tagOpen :: forall str.
(str -> Bool) -> ([Attribute str] -> Bool) -> Tag str -> Bool
tagOpen str -> Bool
pName [Attribute str] -> Bool
pAttrs (TagOpen str
name [Attribute str]
attrs) =
str -> Bool
pName str
name Bool -> Bool -> Bool
&& [Attribute str] -> Bool
pAttrs [Attribute str]
attrs
tagOpen str -> Bool
_ [Attribute str] -> Bool
_ Tag str
_ = Bool
False
tagClose :: (str -> Bool) -> Tag str -> Bool
tagClose :: forall str. (str -> Bool) -> Tag str -> Bool
tagClose str -> Bool
pName (TagClose str
name) = str -> Bool
pName str
name
tagClose str -> Bool
_ Tag str
_ = Bool
False
tagText :: (str -> Bool) -> Tag str -> Bool
tagText :: forall str. (str -> Bool) -> Tag str -> Bool
tagText str -> Bool
p (TagText str
text) = str -> Bool
p str
text
tagText str -> Bool
_ Tag str
_ = Bool
False
tagComment :: (str -> Bool) -> Tag str -> Bool
str -> Bool
p (TagComment str
text) = str -> Bool
p str
text
tagComment str -> Bool
_ Tag str
_ = Bool
False
tagOpenLit :: Eq str => str -> ([Attribute str] -> Bool) -> Tag str -> Bool
tagOpenLit :: forall str.
Eq str =>
str -> ([Attribute str] -> Bool) -> Tag str -> Bool
tagOpenLit str
name = (str -> Bool) -> ([Attribute str] -> Bool) -> Tag str -> Bool
forall str.
(str -> Bool) -> ([Attribute str] -> Bool) -> Tag str -> Bool
tagOpen (str
namestr -> str -> Bool
forall a. Eq a => a -> a -> Bool
==)
tagCloseLit :: Eq str => str -> Tag str -> Bool
tagCloseLit :: forall str. Eq str => str -> Tag str -> Bool
tagCloseLit str
name = (str -> Bool) -> Tag str -> Bool
forall str. (str -> Bool) -> Tag str -> Bool
tagClose (str
namestr -> str -> Bool
forall a. Eq a => a -> a -> Bool
==)
tagOpenAttrLit :: Eq str => str -> Attribute str -> Tag str -> Bool
tagOpenAttrLit :: forall str. Eq str => str -> Attribute str -> Tag str -> Bool
tagOpenAttrLit str
name Attribute str
attr =
str -> ([Attribute str] -> Bool) -> Tag str -> Bool
forall str.
Eq str =>
str -> ([Attribute str] -> Bool) -> Tag str -> Bool
tagOpenLit str
name (Attribute str -> [Attribute str] -> Bool
forall str. Eq str => (str, str) -> [(str, str)] -> Bool
anyAttrLit Attribute str
attr)
tagOpenAttrNameLit :: Eq str => str -> str -> (str -> Bool) -> Tag str -> Bool
tagOpenAttrNameLit :: forall str.
Eq str =>
str -> str -> (str -> Bool) -> Tag str -> Bool
tagOpenAttrNameLit str
tagName str
attrName str -> Bool
pAttrValue =
str -> ([Attribute str] -> Bool) -> Tag str -> Bool
forall str.
Eq str =>
str -> ([Attribute str] -> Bool) -> Tag str -> Bool
tagOpenLit str
tagName
((Attribute str -> Bool) -> [Attribute str] -> Bool
forall str. ((str, str) -> Bool) -> [(str, str)] -> Bool
anyAttr (\(str
name,str
value) -> str
namestr -> str -> Bool
forall a. Eq a => a -> a -> Bool
==str
attrName Bool -> Bool -> Bool
&& str -> Bool
pAttrValue str
value))
tagOpenNameLit :: Eq str => str -> Tag str -> Bool
tagOpenNameLit :: forall str. Eq str => str -> Tag str -> Bool
tagOpenNameLit str
name = str -> ([Attribute str] -> Bool) -> Tag str -> Bool
forall str.
Eq str =>
str -> ([Attribute str] -> Bool) -> Tag str -> Bool
tagOpenLit str
name (Bool -> [Attribute str] -> Bool
forall a b. a -> b -> a
const Bool
True)
tagCloseNameLit :: Eq str => str -> Tag str -> Bool
tagCloseNameLit :: forall str. Eq str => str -> Tag str -> Bool
tagCloseNameLit str
name = str -> Tag str -> Bool
forall str. Eq str => str -> Tag str -> Bool
tagCloseLit str
name
anyAttr :: ((str,str) -> Bool) -> [Attribute str] -> Bool
anyAttr :: forall str. ((str, str) -> Bool) -> [(str, str)] -> Bool
anyAttr = ((str, str) -> Bool) -> [(str, str)] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any
anyAttrName :: (str -> Bool) -> [Attribute str] -> Bool
anyAttrName :: forall str. (str -> Bool) -> [Attribute str] -> Bool
anyAttrName str -> Bool
p = (Attribute str -> Bool) -> [Attribute str] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (str -> Bool
p (str -> Bool) -> (Attribute str -> str) -> Attribute str -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Attribute str -> str
forall a b. (a, b) -> a
fst)
anyAttrValue :: (str -> Bool) -> [Attribute str] -> Bool
anyAttrValue :: forall str. (str -> Bool) -> [Attribute str] -> Bool
anyAttrValue str -> Bool
p = (Attribute str -> Bool) -> [Attribute str] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (str -> Bool
p (str -> Bool) -> (Attribute str -> str) -> Attribute str -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Attribute str -> str
forall a b. (a, b) -> b
snd)
anyAttrLit :: Eq str => (str,str) -> [Attribute str] -> Bool
anyAttrLit :: forall str. Eq str => (str, str) -> [(str, str)] -> Bool
anyAttrLit (str, str)
attr = ((str, str) -> Bool) -> [(str, str)] -> Bool
forall str. ((str, str) -> Bool) -> [(str, str)] -> Bool
anyAttr ((str, str)
attr(str, str) -> (str, str) -> Bool
forall a. Eq a => a -> a -> Bool
==)
anyAttrNameLit :: Eq str => str -> [Attribute str] -> Bool
anyAttrNameLit :: forall str. Eq str => str -> [Attribute str] -> Bool
anyAttrNameLit str
name = (str -> Bool) -> [Attribute str] -> Bool
forall str. (str -> Bool) -> [Attribute str] -> Bool
anyAttrName (str
namestr -> str -> Bool
forall a. Eq a => a -> a -> Bool
==)
anyAttrValueLit :: Eq str => str -> [Attribute str] -> Bool
anyAttrValueLit :: forall str. Eq str => str -> [Attribute str] -> Bool
anyAttrValueLit str
value = (str -> Bool) -> [Attribute str] -> Bool
forall str. (str -> Bool) -> [Attribute str] -> Bool
anyAttrValue (str
valuestr -> str -> Bool
forall a. Eq a => a -> a -> Bool
==)
getTagContent :: Eq str => str -> ([Attribute str] -> Bool) -> [Tag str] -> [Tag str]
getTagContent :: forall str.
Eq str =>
str -> ([Attribute str] -> Bool) -> [Tag str] -> [Tag str]
getTagContent str
name [Attribute str] -> Bool
pAttrs =
(Tag str -> Bool) -> [Tag str] -> [Tag str]
forall a. (a -> Bool) -> [a] -> [a]
takeWhile (Bool -> Bool
not (Bool -> Bool) -> (Tag str -> Bool) -> Tag str -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. str -> Tag str -> Bool
forall str. Eq str => str -> Tag str -> Bool
tagCloseLit str
name) ([Tag str] -> [Tag str])
-> ([Tag str] -> [Tag str]) -> [Tag str] -> [Tag str]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> [Tag str] -> [Tag str]
forall a. Int -> [a] -> [a]
drop Int
1 ([Tag str] -> [Tag str])
-> ([Tag str] -> [Tag str]) -> [Tag str] -> [Tag str]
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
[[Tag str]] -> [Tag str]
forall a. HasCallStack => [a] -> a
head ([[Tag str]] -> [Tag str])
-> ([Tag str] -> [[Tag str]]) -> [Tag str] -> [Tag str]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Tag str -> Bool) -> [Tag str] -> [[Tag str]]
forall {b}. (b -> Bool) -> [b] -> [[b]]
sections (str -> ([Attribute str] -> Bool) -> Tag str -> Bool
forall str.
Eq str =>
str -> ([Attribute str] -> Bool) -> Tag str -> Bool
tagOpenLit str
name [Attribute str] -> Bool
pAttrs)
where sections :: (b -> Bool) -> [b] -> [[b]]
sections b -> Bool
p = ([b] -> Bool) -> [[b]] -> [[b]]
forall a. (a -> Bool) -> [a] -> [a]
filter (b -> Bool
p (b -> Bool) -> ([b] -> b) -> [b] -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [b] -> b
forall a. HasCallStack => [a] -> a
head) ([[b]] -> [[b]]) -> ([b] -> [[b]]) -> [b] -> [[b]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [[b]] -> [[b]]
forall a. HasCallStack => [a] -> [a]
init ([[b]] -> [[b]]) -> ([b] -> [[b]]) -> [b] -> [[b]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [b] -> [[b]]
forall a. [a] -> [[a]]
tails