{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE RankNTypes #-}
module Imm.XML.Conduit (module Imm.XML.Conduit, module Imm.XML) where
import Imm.Feed
import Imm.Prelude
import Imm.XML
import Control.Monad
import Control.Monad.Fix
import Data.Conduit
import Data.XML.Types
import Text.Atom.Conduit.Parse
import Text.RSS.Conduit.Parse
import Text.RSS1.Conduit.Parse
import Text.XML.Stream.Parse
import URI.ByteString
newtype XmlParser = XmlParser (forall m . Monad m => URI -> ConduitT Event Event m ())
mkHandle :: MonadIO m => MonadCatch m => XmlParser -> Handle m
mkHandle (XmlParser preProcess) = Handle
{ parseXml = \uri bytestring -> liftIO $ runConduit $ parseLBS def bytestring .| preProcess uri .| force "Invalid feed" ((fmap Atom <$> atomFeed) `orE` (fmap Rss <$> rssDocument) `orE` (fmap Rss <$> rss1Document))
}
defaultXmlParser :: XmlParser
defaultXmlParser = XmlParser $ const $ fix $ \loop -> await >>= maybe (return ()) (yield >=> const loop)