{-# LANGUAGE FlexibleInstances #-}
module Text.Pandoc.XML.Light.Proc
(
strContent
, onlyElems
, elChildren
, onlyText
, findChildren
, filterChildren
, filterChildrenName
, findChild
, filterChild
, filterChildName
, findElement
, filterElement
, filterElementName
, findElements
, filterElements
, filterElementsName
, findAttr
, lookupAttr
, lookupAttrBy
, findAttrBy
) where
import Data.Text (Text)
import Data.Maybe (listToMaybe)
import Data.List(find)
import Text.Pandoc.XML.Light.Types
strContent :: Element -> Text
strContent :: Element -> Text
strContent = [Text] -> Text
forall a. Monoid a => [a] -> a
mconcat ([Text] -> Text) -> (Element -> [Text]) -> Element -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (CData -> Text) -> [CData] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map CData -> Text
cdData ([CData] -> [Text]) -> (Element -> [CData]) -> Element -> [Text]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Content] -> [CData]
onlyText ([Content] -> [CData])
-> (Element -> [Content]) -> Element -> [CData]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Element -> [Content]
elContent
onlyElems :: [Content] -> [Element]
onlyElems :: [Content] -> [Element]
onlyElems [Content]
xs = [ Element
x | Elem Element
x <- [Content]
xs ]
elChildren :: Element -> [Element]
elChildren :: Element -> [Element]
elChildren Element
e = [ Element
x | Elem Element
x <- Element -> [Content]
elContent Element
e ]
onlyText :: [Content] -> [CData]
onlyText :: [Content] -> [CData]
onlyText [Content]
xs = [ CData
x | Text CData
x <- [Content]
xs ]
findChildren :: QName -> Element -> [Element]
findChildren :: QName -> Element -> [Element]
findChildren QName
q Element
e = (Element -> Bool) -> Element -> [Element]
filterChildren ((QName
q QName -> QName -> Bool
forall a. Eq a => a -> a -> Bool
==) (QName -> Bool) -> (Element -> QName) -> Element -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Element -> QName
elName) Element
e
filterChildren :: (Element -> Bool) -> Element -> [Element]
filterChildren :: (Element -> Bool) -> Element -> [Element]
filterChildren Element -> Bool
p Element
e = (Element -> Bool) -> [Element] -> [Element]
forall a. (a -> Bool) -> [a] -> [a]
filter Element -> Bool
p ([Content] -> [Element]
onlyElems (Element -> [Content]
elContent Element
e))
filterChildrenName :: (QName -> Bool) -> Element -> [Element]
filterChildrenName :: (QName -> Bool) -> Element -> [Element]
filterChildrenName QName -> Bool
p Element
e = (Element -> Bool) -> [Element] -> [Element]
forall a. (a -> Bool) -> [a] -> [a]
filter (QName -> Bool
p(QName -> Bool) -> (Element -> QName) -> Element -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
.Element -> QName
elName) ([Content] -> [Element]
onlyElems (Element -> [Content]
elContent Element
e))
findChild :: QName -> Element -> Maybe Element
findChild :: QName -> Element -> Maybe Element
findChild QName
q Element
e = [Element] -> Maybe Element
forall a. [a] -> Maybe a
listToMaybe (QName -> Element -> [Element]
findChildren QName
q Element
e)
filterChild :: (Element -> Bool) -> Element -> Maybe Element
filterChild :: (Element -> Bool) -> Element -> Maybe Element
filterChild Element -> Bool
p Element
e = [Element] -> Maybe Element
forall a. [a] -> Maybe a
listToMaybe ((Element -> Bool) -> Element -> [Element]
filterChildren Element -> Bool
p Element
e)
filterChildName :: (QName -> Bool) -> Element -> Maybe Element
filterChildName :: (QName -> Bool) -> Element -> Maybe Element
filterChildName QName -> Bool
p Element
e = [Element] -> Maybe Element
forall a. [a] -> Maybe a
listToMaybe ((QName -> Bool) -> Element -> [Element]
filterChildrenName QName -> Bool
p Element
e)
findElement :: QName -> Element -> Maybe Element
findElement :: QName -> Element -> Maybe Element
findElement QName
q Element
e = [Element] -> Maybe Element
forall a. [a] -> Maybe a
listToMaybe (QName -> Element -> [Element]
findElements QName
q Element
e)
filterElement :: (Element -> Bool) -> Element -> Maybe Element
filterElement :: (Element -> Bool) -> Element -> Maybe Element
filterElement Element -> Bool
p Element
e = [Element] -> Maybe Element
forall a. [a] -> Maybe a
listToMaybe ((Element -> Bool) -> Element -> [Element]
filterElements Element -> Bool
p Element
e)
filterElementName :: (QName -> Bool) -> Element -> Maybe Element
filterElementName :: (QName -> Bool) -> Element -> Maybe Element
filterElementName QName -> Bool
p Element
e = [Element] -> Maybe Element
forall a. [a] -> Maybe a
listToMaybe ((QName -> Bool) -> Element -> [Element]
filterElementsName QName -> Bool
p Element
e)
findElements :: QName -> Element -> [Element]
findElements :: QName -> Element -> [Element]
findElements QName
qn Element
e = (QName -> Bool) -> Element -> [Element]
filterElementsName (QName
qnQName -> QName -> Bool
forall a. Eq a => a -> a -> Bool
==) Element
e
filterElements :: (Element -> Bool) -> Element -> [Element]
filterElements :: (Element -> Bool) -> Element -> [Element]
filterElements Element -> Bool
p Element
e
| Element -> Bool
p Element
e = [Element
e]
| Bool
otherwise = (Element -> [Element]) -> [Element] -> [Element]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap ((Element -> Bool) -> Element -> [Element]
filterElements Element -> Bool
p) ([Element] -> [Element]) -> [Element] -> [Element]
forall a b. (a -> b) -> a -> b
$ [Content] -> [Element]
onlyElems ([Content] -> [Element]) -> [Content] -> [Element]
forall a b. (a -> b) -> a -> b
$ Element -> [Content]
elContent Element
e
filterElementsName :: (QName -> Bool) -> Element -> [Element]
filterElementsName :: (QName -> Bool) -> Element -> [Element]
filterElementsName QName -> Bool
p Element
e = (Element -> Bool) -> Element -> [Element]
filterElements (QName -> Bool
p(QName -> Bool) -> (Element -> QName) -> Element -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
.Element -> QName
elName) Element
e
findAttr :: QName -> Element -> Maybe Text
findAttr :: QName -> Element -> Maybe Text
findAttr QName
x Element
e = QName -> [Attr] -> Maybe Text
lookupAttr QName
x (Element -> [Attr]
elAttribs Element
e)
lookupAttr :: QName -> [Attr] -> Maybe Text
lookupAttr :: QName -> [Attr] -> Maybe Text
lookupAttr QName
x = (QName -> Bool) -> [Attr] -> Maybe Text
lookupAttrBy (QName
x QName -> QName -> Bool
forall a. Eq a => a -> a -> Bool
==)
lookupAttrBy :: (QName -> Bool) -> [Attr] -> Maybe Text
lookupAttrBy :: (QName -> Bool) -> [Attr] -> Maybe Text
lookupAttrBy QName -> Bool
p [Attr]
as = Attr -> Text
attrVal (Attr -> Text) -> Maybe Attr -> Maybe Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` (Attr -> Bool) -> [Attr] -> Maybe Attr
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find (QName -> Bool
p (QName -> Bool) -> (Attr -> QName) -> Attr -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Attr -> QName
attrKey) [Attr]
as
findAttrBy :: (QName -> Bool) -> Element -> Maybe Text
findAttrBy :: (QName -> Bool) -> Element -> Maybe Text
findAttrBy QName -> Bool
p Element
e = (QName -> Bool) -> [Attr] -> Maybe Text
lookupAttrBy QName -> Bool
p (Element -> [Attr]
elAttribs Element
e)