module Text.XML.HaXml.Combinators
(
CFilter
, keep, none, children, childrenBy, position
, elm, txt, tag, attr, attrval, tagWith
, find, iffind, ifTxt
, o, union, cat, andThen
, (|>|), with, without
, (/>), (</), et
, path
, deep, deepest, multi
, when, guards, chip, inplace, recursivelyInPlace, foldXml
, mkElem, mkElemAttr, literal, cdata, replaceTag, replaceAttrs, addAttribute
, ThenElse(..), (?>)
, LabelFilter
, oo, x
, numbered, interspersed, tagged, attributed, textlabelled, extracted
) where
import Text.XML.HaXml.Types
import Text.XML.HaXml.Namespaces
import Data.Maybe (fromMaybe)
infixl 6 `with`, `without`
infixr 5 `o`, `oo`, `union`, `andThen`
infixl 5 />, </, |>|
infixr 4 `when`, `guards`
infixr 3 ?>, :>
type CFilter i = Content i -> [Content i]
keep :: a->[a]
keep :: forall a. a -> [a]
keep a
x = [a
x]
none :: a->[b]
none :: forall a b. a -> [b]
none a
_ = []
children :: CFilter i
children :: forall i. CFilter i
children (CElem (Elem QName
_ [Attribute]
_ [Content i]
cs) i
_) = [Content i]
cs
children Content i
_ = []
position :: Int -> CFilter i -> CFilter i
position :: forall i. Int -> CFilter i -> CFilter i
position Int
n CFilter i
f = (\[Content i]
cs-> [[Content i]
cs[Content i] -> Int -> Content i
forall a. HasCallStack => [a] -> Int -> a
!!Int
n]) ([Content i] -> [Content i]) -> CFilter i -> CFilter i
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CFilter i
f
elm, txt :: CFilter i
tag :: String -> CFilter i
attr :: String -> CFilter i
attrval :: Attribute -> CFilter i
tagWith :: (String->Bool) -> CFilter i
elm :: forall i. CFilter i
elm x :: Content i
x@(CElem Element i
_ i
_) = [Content i
x]
elm Content i
_ = []
txt :: forall i. CFilter i
txt x :: Content i
x@(CString Bool
_ CharData
_ i
_) = [Content i
x]
txt x :: Content i
x@(CRef Reference
_ i
_) = [Content i
x]
txt Content i
_ = []
tag :: forall i. CharData -> CFilter i
tag CharData
t x :: Content i
x@(CElem (Elem QName
n [Attribute]
_ [Content i]
_) i
_) | CharData
tCharData -> CharData -> Bool
forall a. Eq a => a -> a -> Bool
==QName -> CharData
printableName QName
n = [Content i
x]
tag CharData
_ Content i
_ = []
tagWith :: forall i. (CharData -> Bool) -> CFilter i
tagWith CharData -> Bool
p x :: Content i
x@(CElem (Elem QName
n [Attribute]
_ [Content i]
_) i
_) | CharData -> Bool
p (QName -> CharData
printableName QName
n) = [Content i
x]
tagWith CharData -> Bool
_ Content i
_ = []
attr :: forall i. CharData -> CFilter i
attr CharData
n x :: Content i
x@(CElem (Elem QName
_ [Attribute]
as [Content i]
_) i
_) | CharData
n CharData -> [CharData] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` (Attribute -> CharData) -> [Attribute] -> [CharData]
forall a b. (a -> b) -> [a] -> [b]
map (QName -> CharData
printableName(QName -> CharData)
-> (Attribute -> QName) -> Attribute -> CharData
forall b c a. (b -> c) -> (a -> b) -> a -> c
.Attribute -> QName
forall a b. (a, b) -> a
fst) [Attribute]
as = [Content i
x]
attr CharData
_ Content i
_ = []
attrval :: forall i. Attribute -> CFilter i
attrval Attribute
av x :: Content i
x@(CElem (Elem QName
_ [Attribute]
as [Content i]
_) i
_) | Attribute
av Attribute -> [Attribute] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Attribute]
as = [Content i
x]
attrval Attribute
_ Content i
_ = []
find :: String -> (String->CFilter i) -> CFilter i
find :: forall i. CharData -> (CharData -> CFilter i) -> CFilter i
find CharData
key CharData -> CFilter i
cont c :: Content i
c@(CElem (Elem QName
_ [Attribute]
as [Content i]
_) i
_) = CharData -> CFilter i
cont (AttValue -> CharData
forall a. Show a => a -> CharData
show (QName -> [Attribute] -> AttValue
forall {a} {c}. Eq a => a -> [(a, c)] -> c
lookfor (CharData -> QName
N CharData
key) [Attribute]
as)) Content i
c
where lookfor :: a -> [(a, c)] -> c
lookfor a
x = c -> Maybe c -> c
forall a. a -> Maybe a -> a
fromMaybe (CharData -> c
forall a. HasCallStack => CharData -> a
error (CharData
"missing attribute: "CharData -> CharData -> CharData
forall a. [a] -> [a] -> [a]
++CharData
key)) (Maybe c -> c) -> ([(a, c)] -> Maybe c) -> [(a, c)] -> c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> [(a, c)] -> Maybe c
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup a
x
iffind :: String -> (String->CFilter i) -> CFilter i -> CFilter i
iffind :: forall i.
CharData -> (CharData -> CFilter i) -> CFilter i -> CFilter i
iffind CharData
key CharData -> CFilter i
yes CFilter i
no c :: Content i
c@(CElem (Elem QName
_ [Attribute]
as [Content i]
_) i
_) =
case QName -> [Attribute] -> Maybe AttValue
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup (CharData -> QName
N CharData
key) [Attribute]
as of
Maybe AttValue
Nothing -> CFilter i
no Content i
c
(Just v :: AttValue
v@(AttValue [Either CharData Reference]
_)) -> CharData -> CFilter i
yes (AttValue -> CharData
forall a. Show a => a -> CharData
show AttValue
v) Content i
c
iffind CharData
_key CharData -> CFilter i
_yes CFilter i
no Content i
other = CFilter i
no Content i
other
ifTxt :: (String->CFilter i) -> CFilter i -> CFilter i
ifTxt :: forall i. (CharData -> CFilter i) -> CFilter i -> CFilter i
ifTxt CharData -> CFilter i
yes CFilter i
_no c :: Content i
c@(CString Bool
_ CharData
s i
_) = CharData -> CFilter i
yes CharData
s Content i
c
ifTxt CharData -> CFilter i
_yes CFilter i
no Content i
c = CFilter i
no Content i
c
data ThenElse a = a :> a
(?>) :: (a->[b]) -> ThenElse (a->[b]) -> (a->[b])
a -> [b]
p ?> :: forall a b. (a -> [b]) -> ThenElse (a -> [b]) -> a -> [b]
?> (a -> [b]
f :> a -> [b]
g) = \a
c-> if (Bool -> Bool
not(Bool -> Bool) -> (a -> Bool) -> a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
.[b] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null([b] -> Bool) -> (a -> [b]) -> a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
.a -> [b]
p) a
c then a -> [b]
f a
c else a -> [b]
g a
c
o :: CFilter i -> CFilter i -> CFilter i
CFilter i
f o :: forall i. CFilter i -> CFilter i -> CFilter i
`o` CFilter i
g = CFilter i -> [Content i] -> [Content i]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap CFilter i
f ([Content i] -> [Content i]) -> CFilter i -> CFilter i
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CFilter i
g
union :: (a->[b]) -> (a->[b]) -> (a->[b])
union :: forall a b. (a -> [b]) -> (a -> [b]) -> a -> [b]
union = ([b] -> [b] -> [b]) -> (a -> [b]) -> (a -> [b]) -> a -> [b]
forall a b d c. (a -> b -> d) -> (c -> a) -> (c -> b) -> c -> d
lift [b] -> [b] -> [b]
forall a. [a] -> [a] -> [a]
(++)
where
lift :: (a->b->d) -> (c->a) -> (c->b) -> c -> d
lift :: forall a b d c. (a -> b -> d) -> (c -> a) -> (c -> b) -> c -> d
lift a -> b -> d
f c -> a
g c -> b
h c
x = a -> b -> d
f (c -> a
g c
x) (c -> b
h c
x)
cat :: [a->[b]] -> (a->[b])
cat :: forall a b. [a -> [b]] -> a -> [b]
cat [] = [b] -> a -> [b]
forall a b. a -> b -> a
const []
cat [a -> [b]]
fs = ((a -> [b]) -> (a -> [b]) -> a -> [b]) -> [a -> [b]] -> a -> [b]
forall a. (a -> a -> a) -> [a] -> a
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldr1 (a -> [b]) -> (a -> [b]) -> a -> [b]
forall a b. (a -> [b]) -> (a -> [b]) -> a -> [b]
union [a -> [b]]
fs
andThen :: (a->c) -> (c->a->b) -> (a->b)
andThen :: forall a c b. (a -> c) -> (c -> a -> b) -> a -> b
andThen a -> c
f c -> a -> b
g a
x = c -> a -> b
g (a -> c
f a
x) a
x
childrenBy :: CFilter i -> CFilter i
childrenBy :: forall i. CFilter i -> CFilter i
childrenBy CFilter i
f = CFilter i
f CFilter i -> CFilter i -> CFilter i
forall i. CFilter i -> CFilter i -> CFilter i
`o` CFilter i
forall i. CFilter i
children
(|>|) :: (a->[b]) -> (a->[b]) -> (a->[b])
a -> [b]
f |>| :: forall a b. (a -> [b]) -> (a -> [b]) -> a -> [b]
|>| a -> [b]
g = \a
x-> let fx :: [b]
fx = a -> [b]
f a
x in if [b] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [b]
fx then a -> [b]
g a
x else [b]
fx
with :: CFilter i -> CFilter i -> CFilter i
CFilter i
f with :: forall i. CFilter i -> CFilter i -> CFilter i
`with` CFilter i
g = (Content i -> Bool) -> [Content i] -> [Content i]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not(Bool -> Bool) -> (Content i -> Bool) -> Content i -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
.[Content i] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null([Content i] -> Bool) -> CFilter i -> Content i -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
.CFilter i
g) ([Content i] -> [Content i]) -> CFilter i -> CFilter i
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CFilter i
f
without :: CFilter i -> CFilter i -> CFilter i
CFilter i
f without :: forall i. CFilter i -> CFilter i -> CFilter i
`without` CFilter i
g = (Content i -> Bool) -> [Content i] -> [Content i]
forall a. (a -> Bool) -> [a] -> [a]
filter ([Content i] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null([Content i] -> Bool) -> CFilter i -> Content i -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
.CFilter i
g) ([Content i] -> [Content i]) -> CFilter i -> CFilter i
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CFilter i
f
(/>) :: CFilter i -> CFilter i -> CFilter i
CFilter i
f /> :: forall i. CFilter i -> CFilter i -> CFilter i
/> CFilter i
g = CFilter i
g CFilter i -> CFilter i -> CFilter i
forall i. CFilter i -> CFilter i -> CFilter i
`o` CFilter i
forall i. CFilter i
children CFilter i -> CFilter i -> CFilter i
forall i. CFilter i -> CFilter i -> CFilter i
`o` CFilter i
f
(</) :: CFilter i -> CFilter i -> CFilter i
CFilter i
f </ :: forall i. CFilter i -> CFilter i -> CFilter i
</ CFilter i
g = CFilter i
f CFilter i -> CFilter i -> CFilter i
forall i. CFilter i -> CFilter i -> CFilter i
`with` (CFilter i
g CFilter i -> CFilter i -> CFilter i
forall i. CFilter i -> CFilter i -> CFilter i
`o` CFilter i
forall i. CFilter i
children)
et :: (String->CFilter i) -> CFilter i -> CFilter i
et :: forall i. (CharData -> CFilter i) -> CFilter i -> CFilter i
et CharData -> CFilter i
f CFilter i
g = (CharData -> CFilter i
f (CharData -> CFilter i) -> LabelFilter i CharData -> CFilter i
forall a i. (a -> CFilter i) -> LabelFilter i a -> CFilter i
`oo` CFilter i -> LabelFilter i CharData
forall i. CFilter i -> LabelFilter i CharData
tagged CFilter i
forall i. CFilter i
elm)
CFilter i -> CFilter i -> CFilter i
forall a b. (a -> [b]) -> (a -> [b]) -> a -> [b]
|>|
(CFilter i
g CFilter i -> CFilter i -> CFilter i
forall i. CFilter i -> CFilter i -> CFilter i
`o` CFilter i
forall i. CFilter i
txt)
path :: [CFilter i] -> CFilter i
path :: forall i. [CFilter i] -> CFilter i
path [CFilter i]
fs = (CFilter i -> CFilter i -> CFilter i)
-> CFilter i -> [CFilter i] -> CFilter i
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr ((CFilter i -> CFilter i -> CFilter i)
-> CFilter i -> CFilter i -> CFilter i
forall a b c. (a -> b -> c) -> b -> a -> c
flip CFilter i -> CFilter i -> CFilter i
forall i. CFilter i -> CFilter i -> CFilter i
o) CFilter i
forall a. a -> [a]
keep [CFilter i]
fs
deep, deepest, multi :: CFilter i -> CFilter i
deep :: forall i. CFilter i -> CFilter i
deep CFilter i
f = CFilter i
f CFilter i -> CFilter i -> CFilter i
forall a b. (a -> [b]) -> (a -> [b]) -> a -> [b]
|>| (CFilter i -> CFilter i
forall i. CFilter i -> CFilter i
deep CFilter i
f CFilter i -> CFilter i -> CFilter i
forall i. CFilter i -> CFilter i -> CFilter i
`o` CFilter i
forall i. CFilter i
children)
deepest :: forall i. CFilter i -> CFilter i
deepest CFilter i
f = (CFilter i -> CFilter i
forall i. CFilter i -> CFilter i
deepest CFilter i
f CFilter i -> CFilter i -> CFilter i
forall i. CFilter i -> CFilter i -> CFilter i
`o` CFilter i
forall i. CFilter i
children) CFilter i -> CFilter i -> CFilter i
forall a b. (a -> [b]) -> (a -> [b]) -> a -> [b]
|>| CFilter i
f
multi :: forall i. CFilter i -> CFilter i
multi CFilter i
f = CFilter i
f CFilter i -> CFilter i -> CFilter i
forall a b. (a -> [b]) -> (a -> [b]) -> a -> [b]
`union` (CFilter i -> CFilter i
forall i. CFilter i -> CFilter i
multi CFilter i
f CFilter i -> CFilter i -> CFilter i
forall i. CFilter i -> CFilter i -> CFilter i
`o` CFilter i
forall i. CFilter i
children)
when :: CFilter i -> CFilter i -> CFilter i
guards :: CFilter i -> CFilter i -> CFilter i
CFilter i
f when :: forall i. CFilter i -> CFilter i -> CFilter i
`when` CFilter i
g = CFilter i
g CFilter i -> ThenElse (CFilter i) -> CFilter i
forall a b. (a -> [b]) -> ThenElse (a -> [b]) -> a -> [b]
?> CFilter i
f CFilter i -> CFilter i -> ThenElse (CFilter i)
forall a. a -> a -> ThenElse a
:> CFilter i
forall a. a -> [a]
keep
CFilter i
g guards :: forall i. CFilter i -> CFilter i -> CFilter i
`guards` CFilter i
f = CFilter i
g CFilter i -> ThenElse (CFilter i) -> CFilter i
forall a b. (a -> [b]) -> ThenElse (a -> [b]) -> a -> [b]
?> CFilter i
f CFilter i -> CFilter i -> ThenElse (CFilter i)
forall a. a -> a -> ThenElse a
:> CFilter i
forall a b. a -> [b]
none
chip :: CFilter i -> CFilter i
chip :: forall i. CFilter i -> CFilter i
chip CFilter i
f (CElem (Elem QName
n [Attribute]
as [Content i]
cs) i
i) = [ Element i -> i -> Content i
forall i. Element i -> i -> Content i
CElem (QName -> [Attribute] -> [Content i] -> Element i
forall i. QName -> [Attribute] -> [Content i] -> Element i
Elem QName
n [Attribute]
as (CFilter i -> [Content i] -> [Content i]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap CFilter i
f [Content i]
cs)) i
i ]
chip CFilter i
_f Content i
c = [Content i
c]
inplace :: CFilter i -> CFilter i
inplace :: forall i. CFilter i -> CFilter i
inplace CFilter i
f c :: Content i
c@(CElem (Elem QName
name [Attribute]
as [Content i]
_) i
i) = [ Element i -> i -> Content i
forall i. Element i -> i -> Content i
CElem (QName -> [Attribute] -> [Content i] -> Element i
forall i. QName -> [Attribute] -> [Content i] -> Element i
Elem QName
name [Attribute]
as (CFilter i
f Content i
c)) i
i ]
inplace CFilter i
_f Content i
c = [Content i
c]
recursivelyInPlace :: CFilter i -> CFilter i
recursivelyInPlace :: forall i. CFilter i -> CFilter i
recursivelyInPlace CFilter i
f = CFilter i -> CFilter i
forall i. CFilter i -> CFilter i
inplace (CFilter i -> CFilter i
forall i. CFilter i -> CFilter i
recursivelyInPlace CFilter i
f CFilter i -> CFilter i -> CFilter i
forall i. CFilter i -> CFilter i -> CFilter i
`o` CFilter i
f)
foldXml :: CFilter i -> CFilter i
foldXml :: forall i. CFilter i -> CFilter i
foldXml CFilter i
f = CFilter i
f CFilter i -> CFilter i -> CFilter i
forall i. CFilter i -> CFilter i -> CFilter i
`o` CFilter i -> CFilter i
forall i. CFilter i -> CFilter i
chip (CFilter i -> CFilter i
forall i. CFilter i -> CFilter i
foldXml CFilter i
f)
mkElem :: String -> [CFilter i] -> CFilter i
mkElem :: forall i. CharData -> [CFilter i] -> CFilter i
mkElem CharData
h [CFilter i]
cfs Content i
t = [ Element i -> i -> Content i
forall i. Element i -> i -> Content i
CElem (QName -> [Attribute] -> [Content i] -> Element i
forall i. QName -> [Attribute] -> [Content i] -> Element i
Elem (CharData -> QName
N CharData
h) [] ([CFilter i] -> CFilter i
forall a b. [a -> [b]] -> a -> [b]
cat [CFilter i]
cfs Content i
t)) i
forall a. HasCallStack => a
undefined ]
mkElemAttr :: String -> [(String,CFilter i)] -> [CFilter i] -> CFilter i
mkElemAttr :: forall i.
CharData -> [(CharData, CFilter i)] -> [CFilter i] -> CFilter i
mkElemAttr CharData
h [(CharData, CFilter i)]
as [CFilter i]
cfs = \Content i
t-> [ Element i -> i -> Content i
forall i. Element i -> i -> Content i
CElem (QName -> [Attribute] -> [Content i] -> Element i
forall i. QName -> [Attribute] -> [Content i] -> Element i
Elem (CharData -> QName
N CharData
h) (((CharData, CFilter i) -> Attribute)
-> [(CharData, CFilter i)] -> [Attribute]
forall a b. (a -> b) -> [a] -> [b]
map (Content i -> (CharData, CFilter i) -> Attribute
forall {i}. Content i -> (CharData, CFilter i) -> Attribute
attr Content i
t) [(CharData, CFilter i)]
as) ([CFilter i] -> CFilter i
forall a b. [a -> [b]] -> a -> [b]
cat [CFilter i]
cfs Content i
t))
i
forall a. HasCallStack => a
undefined ]
where attr :: Content i -> (CharData, CFilter i) -> Attribute
attr Content i
t (CharData
n,CFilter i
vf) =
let v :: CharData
v = [CharData] -> CharData
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [ CharData
s | (CString Bool
_ CharData
s i
_) <- (CFilter i -> CFilter i
forall i. CFilter i -> CFilter i
deep CFilter i
forall i. CFilter i
txt CFilter i -> CFilter i -> CFilter i
forall i. CFilter i -> CFilter i -> CFilter i
`o` CFilter i
vf) Content i
t ]
in (CharData -> QName
N CharData
n, [Either CharData Reference] -> AttValue
AttValue [CharData -> Either CharData Reference
forall a b. a -> Either a b
Left CharData
v])
literal :: String -> CFilter i
literal :: forall i. CharData -> CFilter i
literal CharData
s = [Content i] -> Content i -> [Content i]
forall a b. a -> b -> a
const [Bool -> CharData -> i -> Content i
forall i. Bool -> CharData -> i -> Content i
CString Bool
False CharData
s i
forall a. HasCallStack => a
undefined]
cdata :: String -> CFilter i
cdata :: forall i. CharData -> CFilter i
cdata CharData
s = [Content i] -> Content i -> [Content i]
forall a b. a -> b -> a
const [Bool -> CharData -> i -> Content i
forall i. Bool -> CharData -> i -> Content i
CString Bool
True CharData
s i
forall a. HasCallStack => a
undefined]
replaceTag :: String -> CFilter i
replaceTag :: forall i. CharData -> CFilter i
replaceTag CharData
n (CElem (Elem QName
_ [Attribute]
as [Content i]
cs) i
i) = [Element i -> i -> Content i
forall i. Element i -> i -> Content i
CElem (QName -> [Attribute] -> [Content i] -> Element i
forall i. QName -> [Attribute] -> [Content i] -> Element i
Elem (CharData -> QName
N CharData
n) [Attribute]
as [Content i]
cs) i
i]
replaceTag CharData
_ Content i
_ = []
replaceAttrs :: [(String,String)] -> CFilter i
replaceAttrs :: forall i. [(CharData, CharData)] -> CFilter i
replaceAttrs [(CharData, CharData)]
as (CElem (Elem QName
n [Attribute]
_ [Content i]
cs) i
i) = [Element i -> i -> Content i
forall i. Element i -> i -> Content i
CElem (QName -> [Attribute] -> [Content i] -> Element i
forall i. QName -> [Attribute] -> [Content i] -> Element i
Elem QName
n [Attribute]
as' [Content i]
cs) i
i]
where as' :: [Attribute]
as' = ((CharData, CharData) -> Attribute)
-> [(CharData, CharData)] -> [Attribute]
forall a b. (a -> b) -> [a] -> [b]
map (\(CharData
n,CharData
v)-> (CharData -> QName
N CharData
n, [Either CharData Reference] -> AttValue
AttValue [CharData -> Either CharData Reference
forall a b. a -> Either a b
Left CharData
v])) [(CharData, CharData)]
as
replaceAttrs [(CharData, CharData)]
_ Content i
_ = []
addAttribute :: String -> String -> CFilter a
addAttribute :: forall a. CharData -> CharData -> CFilter a
addAttribute CharData
name CharData
val (CElem (Elem QName
n [Attribute]
as [Content a]
cs) a
i) =
[Element a -> a -> Content a
forall i. Element i -> i -> Content i
CElem (QName -> [Attribute] -> [Content a] -> Element a
forall i. QName -> [Attribute] -> [Content i] -> Element i
Elem QName
n (Attribute
aAttribute -> [Attribute] -> [Attribute]
forall a. a -> [a] -> [a]
:[Attribute]
as) [Content a]
cs) a
i]
where a :: Attribute
a = (CharData -> QName
N CharData
name, [Either CharData Reference] -> AttValue
AttValue [CharData -> Either CharData Reference
forall a b. a -> Either a b
Left CharData
val])
addAttribute CharData
_ CharData
_ Content a
_ = []
type LabelFilter i a = Content i -> [(a,Content i)]
oo :: (a->CFilter i) -> LabelFilter i a -> CFilter i
a -> CFilter i
f oo :: forall a i. (a -> CFilter i) -> LabelFilter i a -> CFilter i
`oo` LabelFilter i a
g = ((a, Content i) -> [Content i]) -> [(a, Content i)] -> [Content i]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap ((a -> CFilter i) -> (a, Content i) -> [Content i]
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry a -> CFilter i
f) ([(a, Content i)] -> [Content i]) -> LabelFilter i a -> CFilter i
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LabelFilter i a
g
x :: (CFilter i->LabelFilter i a) -> (CFilter i->LabelFilter i b) ->
(CFilter i->LabelFilter i (a,b))
CFilter i -> LabelFilter i a
f x :: forall i a b.
(CFilter i -> LabelFilter i a)
-> (CFilter i -> LabelFilter i b)
-> CFilter i
-> LabelFilter i (a, b)
`x` CFilter i -> LabelFilter i b
g = \CFilter i
cf Content i
c-> let gs :: [b]
gs = ((b, Content i) -> b) -> [(b, Content i)] -> [b]
forall a b. (a -> b) -> [a] -> [b]
map (b, Content i) -> b
forall a b. (a, b) -> a
fst (CFilter i -> LabelFilter i b
g CFilter i
cf Content i
c)
fs :: [a]
fs = ((a, Content i) -> a) -> [(a, Content i)] -> [a]
forall a b. (a -> b) -> [a] -> [b]
map (a, Content i) -> a
forall a b. (a, b) -> a
fst (CFilter i -> LabelFilter i a
f CFilter i
cf Content i
c)
in [(a, b)] -> [Content i] -> [((a, b), Content i)]
forall a b. [a] -> [b] -> [(a, b)]
zip ([a] -> [b] -> [(a, b)]
forall a b. [a] -> [b] -> [(a, b)]
zip [a]
fs [b]
gs) (CFilter i
cf Content i
c)
numbered :: CFilter i -> LabelFilter i Int
numbered :: forall i. CFilter i -> LabelFilter i Int
numbered CFilter i
f = [Int] -> [Content i] -> [(Int, Content i)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
1..] ([Content i] -> [(Int, Content i)])
-> CFilter i -> Content i -> [(Int, Content i)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CFilter i
f
interspersed :: String -> CFilter i -> String -> LabelFilter i String
interspersed :: forall i.
CharData -> CFilter i -> CharData -> LabelFilter i CharData
interspersed CharData
a CFilter i
f CharData
b =
(\[Content i]
xs-> [CharData] -> [Content i] -> [(CharData, Content i)]
forall a b. [a] -> [b] -> [(a, b)]
zip (Int -> CharData -> [CharData]
forall a. Int -> a -> [a]
replicate ([Content i] -> Int
forall {a}. [a] -> Int
len [Content i]
xs) CharData
a [CharData] -> [CharData] -> [CharData]
forall a. [a] -> [a] -> [a]
++ [CharData
b]) [Content i]
xs) ([Content i] -> [(CharData, Content i)])
-> CFilter i -> Content i -> [(CharData, Content i)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CFilter i
f
where
len :: [a] -> Int
len [] = Int
0
len [a]
xs = [a] -> Int
forall {a}. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
xs Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1
tagged :: CFilter i -> LabelFilter i String
tagged :: forall i. CFilter i -> LabelFilter i CharData
tagged CFilter i
f = (Content i -> CharData) -> CFilter i -> LabelFilter i CharData
forall i a. (Content i -> a) -> CFilter i -> LabelFilter i a
extracted Content i -> CharData
forall {i}. Content i -> CharData
name CFilter i
f
where name :: Content i -> CharData
name (CElem (Elem QName
n [Attribute]
_ [Content i]
_) i
_) = QName -> CharData
printableName QName
n
name Content i
_ = CharData
""
attributed :: String -> CFilter i -> LabelFilter i String
attributed :: forall i. CharData -> CFilter i -> LabelFilter i CharData
attributed CharData
key CFilter i
f = (Content i -> CharData) -> CFilter i -> LabelFilter i CharData
forall i a. (Content i -> a) -> CFilter i -> LabelFilter i a
extracted Content i -> CharData
forall {i}. Content i -> CharData
att CFilter i
f
where att :: Content i -> CharData
att (CElem (Elem QName
_ [Attribute]
as [Content i]
_) i
_) =
case QName -> [Attribute] -> Maybe AttValue
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup (CharData -> QName
N CharData
key) [Attribute]
as of
Maybe AttValue
Nothing -> CharData
""
(Just v :: AttValue
v@(AttValue [Either CharData Reference]
_)) -> AttValue -> CharData
forall a. Show a => a -> CharData
show AttValue
v
att Content i
_ = CharData
""
textlabelled :: CFilter i -> LabelFilter i (Maybe String)
textlabelled :: forall i. CFilter i -> LabelFilter i (Maybe CharData)
textlabelled CFilter i
f = (Content i -> Maybe CharData)
-> CFilter i -> LabelFilter i (Maybe CharData)
forall i a. (Content i -> a) -> CFilter i -> LabelFilter i a
extracted Content i -> Maybe CharData
forall {i}. Content i -> Maybe CharData
text CFilter i
f
where text :: Content i -> Maybe CharData
text (CString Bool
_ CharData
s i
_) = CharData -> Maybe CharData
forall a. a -> Maybe a
Just CharData
s
text Content i
_ = Maybe CharData
forall a. Maybe a
Nothing
extracted :: (Content i->a) -> CFilter i -> LabelFilter i a
Content i -> a
proj CFilter i
f = (Content i -> [(a, Content i)]) -> [Content i] -> [(a, Content i)]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (\Content i
c->[(Content i -> a
proj Content i
c, Content i
c)]) ([Content i] -> [(a, Content i)])
-> CFilter i -> Content i -> [(a, Content i)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CFilter i
f