module Text.XML.Hexml.Lens
(
_children
, _contents
, ChildNode(..)
, TextContents(..)
, Attributes(..)
, AsXML(..)
) where
import Control.Lens hiding (children)
import Data.ByteString as Strict (ByteString)
import qualified Data.ByteString.Internal as Strict
import Data.ByteString.Lens
import Data.String
import qualified Data.Text as Strict
import qualified Data.Text.Encoding as Strict
import Data.Text.Lens
import qualified Foundation as F
import qualified Foundation.String as F
import qualified Foundation.Array.Internal as F
import Text.XML.Hexml
_children :: Fold Node Node
_children = folding children
_contents :: Fold Node (Either ByteString Node)
_contents = folding contents
class ChildNode s where
node :: s -> Fold Node Node
instance ChildNode String where
node name_ = folding $ flip childrenBy ( name_ ^. strictUtf8)
instance ChildNode F.String where
node name_ = folding $ flip childrenBy ( F.toList name_ ^. strictUtf8)
instance ChildNode ByteString where
node name_ = folding $ flip childrenBy name_
instance ChildNode Strict.Text where
node name_ = folding $ flip childrenBy ( name_ ^. strictTextUtf8 )
instance ChildNode Int where
node n = folding $ take 1 . drop n . children
class TextContents s where
textContents :: Fold Node s
instance TextContents ByteString where
textContents = folding contents . _Left
instance TextContents String where
textContents = textContents @ByteString . from strictUtf8
instance TextContents Strict.Text where
textContents = textContents . from strictTextUtf8
instance TextContents F.String where
textContents = textContents . foundation F.UTF8
class Attributes s where
_Attribute :: s -> Getter Node (Maybe s)
iattributes :: IndexedFold String Node s
instance Attributes ByteString where
_Attribute n = pre $ to (`attributeBy` n) . folded . to attributeValue
iattributes = ifolding (map (\ (Attribute n v) -> (n^.from strictUtf8, v)) . attributes )
instance Attributes String where
_Attribute n = pre $ to (`attributeBy` (n ^. packedChars)) . folded . to attributeValue . from strictUtf8
iattributes = iattributes @ ByteString . unpackedChars
instance Attributes Strict.Text where
_Attribute n = pre $ to (`attributeBy` (n ^. strictTextUtf8)) . folded . to attributeValue . from strictTextUtf8
iattributes = iattributes . packed
instance Attributes F.String where
_Attribute n = pre $ to (`attributeBy` (F.toList n ^. packedChars)) . folded . to attributeValue . foundation F.UTF8
iattributes = iattributes . to fromString
class AsXML s where
_XML :: Prism' s Node
instance AsXML ByteString where
_XML = prism' outer doParse where
doParse x =
case parse x of
Right n -> Just $ case children n of [y] -> y ; _ -> n
Left _ -> Nothing
instance AsXML String where
_XML = strictUtf8 . _XML @ ByteString
instance AsXML Strict.Text where
_XML = strictTextUtf8 . _XML
strictTextUtf8 :: Iso' Strict.Text Strict.ByteString
strictTextUtf8 = iso Strict.encodeUtf8 Strict.decodeUtf8
strictUtf8 :: Iso' String Strict.ByteString
strictUtf8 = packed . strictTextUtf8
foundation :: F.Encoding -> Fold Strict.ByteString F.String
foundation encoding = to (F.fromBytes encoding . fromByteString) . filtered (hasn't (_2.folded)) . _1
where
fromByteString = F.fromForeignPtr . Strict.toForeignPtr