{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE ImpredicativeTypes #-}
{-# OPTIONS_HADDOCK hide #-}
module Text.HTML.Scalpel.Internal.Select.Types (
Selector (..)
, AttributePredicate (..)
, checkPred
, AttributeName (..)
, matchKey
, anyAttrPredicate
, TagName (..)
, SelectNode (..)
, tagSelector
, anySelector
, textSelector
, toSelectNode
, SelectSettings (..)
, defaultSelectSettings
) where
import Data.Char (toLower)
import Data.String (IsString, fromString)
import qualified Text.HTML.TagSoup as TagSoup
import qualified Text.StringLike as TagSoup
import qualified Data.Text as T
data AttributeName = AnyAttribute | AttributeString String
matchKey :: TagSoup.StringLike str => AttributeName -> str -> Bool
matchKey :: forall str. StringLike str => AttributeName -> str -> Bool
matchKey (AttributeString String
s) = ((forall a. IsString a => String -> a
TagSoup.fromString forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map Char -> Char
toLower String
s) forall a. Eq a => a -> a -> Bool
==)
matchKey AttributeName
AnyAttribute = forall a b. a -> b -> a
const Bool
True
instance IsString AttributeName where
fromString :: String -> AttributeName
fromString = String -> AttributeName
AttributeString
data AttributePredicate
= MkAttributePredicate
(forall str. TagSoup.StringLike str => [TagSoup.Attribute str]
-> Bool)
checkPred :: TagSoup.StringLike str
=> AttributePredicate -> [TagSoup.Attribute str] -> Bool
checkPred :: forall str.
StringLike str =>
AttributePredicate -> [Attribute str] -> Bool
checkPred (MkAttributePredicate forall str. StringLike str => [Attribute str] -> Bool
p) = forall str. StringLike str => [Attribute str] -> Bool
p
anyAttrPredicate :: (forall str. TagSoup.StringLike str => (str, str) -> Bool)
-> AttributePredicate
anyAttrPredicate :: (forall str. StringLike str => (str, str) -> Bool)
-> AttributePredicate
anyAttrPredicate forall str. StringLike str => (str, str) -> Bool
p = (forall str. StringLike str => [Attribute str] -> Bool)
-> AttributePredicate
MkAttributePredicate forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any forall str. StringLike str => (str, str) -> Bool
p
newtype Selector = MkSelector [(SelectNode, SelectSettings)]
data SelectSettings = SelectSettings {
SelectSettings -> Maybe Int
selectSettingsDepth :: Maybe Int
}
defaultSelectSettings :: SelectSettings
defaultSelectSettings :: SelectSettings
defaultSelectSettings = SelectSettings {
selectSettingsDepth :: Maybe Int
selectSettingsDepth = forall a. Maybe a
Nothing
}
tagSelector :: String -> Selector
tagSelector :: String -> Selector
tagSelector String
tag = [(SelectNode, SelectSettings)] -> Selector
MkSelector [
(TagName -> [AttributePredicate] -> SelectNode
toSelectNode (String -> TagName
TagString String
tag) [], SelectSettings
defaultSelectSettings)
]
anySelector :: Selector
anySelector :: Selector
anySelector = [(SelectNode, SelectSettings)] -> Selector
MkSelector [([AttributePredicate] -> SelectNode
SelectAny [], SelectSettings
defaultSelectSettings)]
textSelector :: Selector
textSelector :: Selector
textSelector = [(SelectNode, SelectSettings)] -> Selector
MkSelector [(SelectNode
SelectText, SelectSettings
defaultSelectSettings)]
instance IsString Selector where
fromString :: String -> Selector
fromString = String -> Selector
tagSelector
data SelectNode = SelectNode !T.Text [AttributePredicate]
| SelectAny [AttributePredicate]
| SelectText
data TagName = AnyTag | TagString String
instance IsString TagName where
fromString :: String -> TagName
fromString = String -> TagName
TagString
toSelectNode :: TagName -> [AttributePredicate] -> SelectNode
toSelectNode :: TagName -> [AttributePredicate] -> SelectNode
toSelectNode TagName
AnyTag = [AttributePredicate] -> SelectNode
SelectAny
toSelectNode (TagString String
str) = Text -> [AttributePredicate] -> SelectNode
SelectNode forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. IsString a => String -> a
TagSoup.fromString forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map Char -> Char
toLower String
str