module Text.Feed.Import
( parseFeedFromFile
, parseFeedString
, parseFeedSource
, readRSS2
, readRSS1
, readAtom
) where
import Text.Atom.Feed.Import as Atom
import Text.RSS.Import as RSS
import Text.RSS1.Import as RSS1
import Text.Feed.Types
import Text.XML.Light as XML
import Text.XML.Light.Lexer ( XmlSource )
import Control.Monad
#if MIN_VERSION_utf8_string(1,0,0)
import Codec.Binary.UTF8.String (decodeString)
import System.IO (IOMode(..), hGetContents, openBinaryFile )
utf8readFile :: FilePath -> IO String
utf8readFile fp = fmap decodeString (hGetContents =<< openBinaryFile fp ReadMode)
#else
import System.IO.UTF8 as UTF8 ( readFile )
utf8readFile :: FilePath -> IO String
utf8readFile = UTF8.readFile
#endif
parseFeedFromFile :: FilePath -> IO Feed
parseFeedFromFile fp = do
ls <- utf8readFile fp
case parseFeedString ls of
Nothing -> fail ("parseFeedFromFile: not a well-formed XML content in: " ++ fp)
Just f -> return f
parseFeedWithParser :: XmlSource s => (s -> Maybe Element) -> s -> Maybe Feed
parseFeedWithParser parser str =
case parser str of
Nothing -> Nothing
Just e ->
readAtom e `mplus`
readRSS2 e `mplus`
readRSS1 e `mplus`
Just (XMLFeed e)
parseFeedString :: String -> Maybe Feed
parseFeedString = parseFeedSource
parseFeedSource :: XmlSource s => s -> Maybe Feed
parseFeedSource = parseFeedWithParser parseXMLDoc
readRSS2 :: XML.Element -> Maybe Feed
readRSS2 e = fmap RSSFeed $ RSS.elementToRSS e
readRSS1 :: XML.Element -> Maybe Feed
readRSS1 e = fmap RSS1Feed $ RSS1.elementToFeed e
readAtom :: XML.Element -> Maybe Feed
readAtom e = fmap AtomFeed $ Atom.elementFeed e