summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Presence/NestingXML.hs43
1 files changed, 43 insertions, 0 deletions
diff --git a/Presence/NestingXML.hs b/Presence/NestingXML.hs
new file mode 100644
index 00000000..220e82a0
--- /dev/null
+++ b/Presence/NestingXML.hs
@@ -0,0 +1,43 @@
1module NestingXML where
2
3import Data.Conduit
4import Data.XML.Types
5import Control.Monad.Reader
6
7type NestingXML o m a = ReaderT Int (ConduitM Event o m) a
8
9runNestingXML :: NestingXML o m a -> Int -> ConduitM Event o m a
10runNestingXML = runReaderT
11
12nesting :: Monad m => NestingXML o m Int
13nesting = ask
14
15awaitXML :: Monad m => NestingXML o m (Maybe Event)
16awaitXML = do
17 xml <- lift await
18 let f = case xml of
19 Just (EventBeginElement _ _) -> (+1)
20 Just (EventEndElement _) -> (subtract 1)
21 _ -> id
22 local f (return xml)
23
24
25awaitCloser :: Monad m => Int -> NestingXML o m ()
26awaitCloser lvl = do
27 fix $ \loop -> do
28 awaitXML
29 lvl' <- nesting
30 when (lvl' >= lvl) loop
31
32nextElement :: Monad m => NestingXML o m (Maybe Event)
33nextElement = do
34 lvl <- nesting
35 fix $ \loop -> do
36 xml <- awaitXML
37 case xml of
38 Nothing -> return Nothing
39 Just (EventBeginElement _ _) -> return xml
40 Just _ -> do
41 lvl' <- nesting
42 if (lvl'>=lvl) then loop
43 else return Nothing