From bb5e9bd5238b3d0640ac2174e50c8d7a997805d9 Mon Sep 17 00:00:00 2001 From: Deian Stefan Date: Sat, 23 Mar 2013 15:55:59 -0700 Subject: [PATCH] support for module:content --- Text/RSS.hs | 18 +++++++++++++++++- examples/htmlWhiskers.hs | 28 ++++++++++++++++++++++++++++ rss.cabal | 2 +- 3 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 examples/htmlWhiskers.hs diff --git a/Text/RSS.hs b/Text/RSS.hs index e5d61c2..17318ad 100644 --- a/Text/RSS.hs +++ b/Text/RSS.hs @@ -77,6 +77,7 @@ type Item = [ItemElem] type Title = String type Link = URI type Description = String +type ContentEncoded = String type Width = Int type Height = Int type Email = String @@ -114,6 +115,7 @@ data ChannelElem = Language String data ItemElem = Title Title | Link Link | Description Description + | ContentEncoded String | Author Email | Category (Maybe Domain) String | Comments URI @@ -128,16 +130,26 @@ data Weekday = Sunday | Monday | Tuesday | Wednesday | Thursday | Friday | Saturday deriving (Eq, Ord, Enum, Bounded, Ix, Read, Show) +-- | Namespace declaration for the modules:content +xmlnsContentURL :: String +xmlnsContentURL = "http://purl.org/rss/1.0/modules/content/" + -- | Converts RSS to XML. rssToXML :: RSS -> CFilter () rssToXML (RSS title link description celems items) = - mkElemAttr "rss" [("version",literal "2.0")] + mkElemAttr "rss" ([("version",literal "2.0")] ++ xmlnsContent) [mkElem "channel" ([mkTitle title, mkLink link, mkDescription description, mkDocs] ++ map mkChannelElem celems ++ map mkItem items)] + where haveContentEncoded = or $ map (any isContentEncoded) items + isContentEncoded (ContentEncoded _) = True + isContentEncoded _ = False + xmlnsContent = if haveContentEncoded + then [("xmlns:content", literal xmlnsContentURL)] + else [] -- | Render XML as a string. showXML :: CFilter () -> String @@ -161,6 +173,9 @@ mkLink = mkSimple "link" . show mkDescription :: Description -> CFilter () mkDescription str = mkElem "description" [cdata str] +mkContentEncoded :: ContentEncoded -> CFilter () +mkContentEncoded str = mkElem "content:encoded" [cdata str] + mkDocs :: CFilter () mkDocs = mkSimple "docs" "http://www.rssboard.org/rss-specification" @@ -217,6 +232,7 @@ mkItemElem :: ItemElem -> CFilter () mkItemElem (Title t) = mkTitle t mkItemElem (Link l) = mkLink l mkItemElem (Description d) = mkDescription d +mkItemElem (ContentEncoded c) = mkContentEncoded c mkItemElem (Author e) = mkElem "author" [literal e] mkItemElem (Category md str) = mkCategory md str mkItemElem (Comments uri) = mkSimple "comments" $ show uri diff --git a/examples/htmlWhiskers.hs b/examples/htmlWhiskers.hs new file mode 100644 index 0000000..229046b --- /dev/null +++ b/examples/htmlWhiskers.hs @@ -0,0 +1,28 @@ +module Main where + +import Text.RSS + +import Data.Maybe +import Network.URI +import System.IO.Unsafe +import Data.Time.Clock + +main = putStrLn . showXML . rssToXML . rss =<< getCurrentTime + +rss :: UTCTime -> RSS +rss t = RSS "my whiskers" + (fromJust (parseURI "http://www.n-heptane.com")) + "they are very pointy and luxurious" + [] + [[ Title "yea-haw" + , Link (fromJust (parseURI "http://www.n-heptane.com/")) + , Description "the best site ever!!!" + , ContentEncoded "the best site ever!!!" + , Author "jeremy@n-heptane.com (Jeremy Shaw)" + , Category Nothing "meow" + , Enclosure (fromJust (parseURI "http://www.n-heptane.com/newpics/alice.gif")) 7333 "image/jpeg" + , Guid True "whee babayyyy!" + , PubDate t + , Source (fromJust (parseURI "http://www.google.com/")) "The best search engine eva!" + ] + ] diff --git a/rss.cabal b/rss.cabal index f83c739..802621d 100644 --- a/rss.cabal +++ b/rss.cabal @@ -1,5 +1,5 @@ Name: rss -Version: 3000.2.0.2 +Version: 3000.2.1.0 Cabal-version: >=1.6 Build-type: Simple Copyright: Jeremy Shaw 2004, Bjorn Bringert 2004-2006