module Text.RSS.Extensions.Content
(
ContentModule(..)
, RssChannelExtension(ContentChannel)
, RssItemExtension(ContentItem)
, contentEncoded
, renderContentEncoded
, namespacePrefix
, namespaceURI
) where
import Text.RSS.Extensions
import Text.RSS.Types
import Conduit hiding (throwM)
import Control.Exception.Safe as Exception
import Control.Monad
import Data.Maybe
import Data.Singletons
import Data.Text (Text)
import qualified Data.Text as Text
import Data.XML.Types
import GHC.Generics
import Text.XML.Stream.Parse
import qualified Text.XML.Stream.Render as Render
import URI.ByteString
data ContentModule :: *
data instance Sing ContentModule = SContentModule
instance SingI ContentModule where sing = SContentModule
instance ParseRssExtension ContentModule where
parseRssChannelExtension = pure ContentChannel
parseRssItemExtension = ContentItem <$> (manyYield' contentEncoded =$= headDefC mempty)
instance RenderRssExtension ContentModule where
renderRssChannelExtension = const $ pure ()
renderRssItemExtension (ContentItem e) = unless (Text.null e) $ renderContentEncoded e
data instance RssChannelExtension ContentModule = ContentChannel deriving(Eq, Generic, Ord, Show)
data instance RssItemExtension ContentModule = ContentItem { itemContent :: Text }
deriving(Eq, Generic, Ord, Show)
namespacePrefix :: Text
namespacePrefix = "content"
namespaceURI :: URIRef Absolute
namespaceURI = uri where Right uri = parseURI laxURIParserOptions "http://purl.org/rss/1.0/modules/content/"
contentName :: Text -> Name
contentName string = Name string (Just "http://purl.org/rss/1.0/modules/content/") (Just namespacePrefix)
contentEncoded :: MonadThrow m => ConduitM Event o m (Maybe Text)
contentEncoded = tagIgnoreAttrs (matching (== contentName "encoded")) content
renderContentEncoded :: Monad m => Text -> Source m Event
renderContentEncoded = Render.tag (contentName "encoded") mempty . Render.content