@ -24,11 +24,14 @@ compressNodes (NodeContent (ContentText x) : NodeContent (ContentText y) : z) =
compressNodes $ NodeContent ( ContentText $ x ` Text . append ` y ) : z
compressNodes $ NodeContent ( ContentText $ x ` Text . append ` y ) : z
compressNodes ( x : xs ) = x : compressNodes xs
compressNodes ( x : xs ) = x : compressNodes xs
elementFromEvent s :: R . MonadThrow m => C . Sink Event m Element
elements :: R . MonadThrow m => C . Conduit Event m Element
elementFromEvent s = do
elements = do
x <- CL . peek
x <- C . await
case x of
case x of
Just ( EventBeginElement n as ) -> goE n as
Just ( EventBeginElement n as ) -> do
goE n as >>= C . yield
elements
Nothing -> return ()
_ -> lift $ R . monadThrow $ InvalidEventStream $ " not an element: " ++ show x
_ -> lift $ R . monadThrow $ InvalidEventStream $ " not an element: " ++ show x
where
where
many' f =
many' f =
@ -37,25 +40,22 @@ elementFromEvents = do
go front = do
go front = do
x <- f
x <- f
case x of
case x of
Nothing -> return $ front []
Left x -> return $ ( x , front [] )
Just y -> go ( front . ( : ) y )
Right y -> go ( front . ( : ) y )
dropReturn x = CL . drop 1 >> return x
goE n as = do
goE n as = do
CL . drop 1
( y , ns ) <- many' goN
ns <- many' goN
y <- CL . head
if y == Just ( EventEndElement n )
if y == Just ( EventEndElement n )
then return $ Element n as $ compressNodes ns
then return $ Element n as $ compressNodes ns
else lift $ R . monadThrow $ InvalidEventStream $ " Missing end element for " ++ show n ++ " , got: " ++ show y
else lift $ R . monadThrow $ InvalidEventStream $ " Missing end element for " ++ show n ++ " , got: " ++ show y
goN = do
goN = do
x <- CL . peek
x <- await
case x of
case x of
Just ( EventBeginElement n as ) -> ( Jus t . NodeElement ) <$> goE n as
Just ( EventBeginElement n as ) -> ( Righ t . NodeElement ) <$> goE n as
Just ( EventInstruction i ) -> d ropR eturn $ Jus t $ NodeInstruction i
Just ( EventInstruction i ) -> return $ Righ t $ NodeInstruction i
Just ( EventContent c ) -> d ropR eturn $ Jus t $ NodeContent c
Just ( EventContent c ) -> return $ Righ t $ NodeContent c
Just ( EventComment t ) -> d ropR eturn $ Jus t $ NodeComment t
Just ( EventComment t ) -> return $ Righ t $ NodeComment t
Just ( EventCDATA t ) -> d ropR eturn $ Jus t $ NodeContent $ ContentText t
Just ( EventCDATA t ) -> return $ Righ t $ NodeContent $ ContentText t
_ -> return Nothing
_ -> return $ Left x
openElementToEvents :: Element -> [ Event ]
openElementToEvents :: Element -> [ Event ]