Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for module:content #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion Text/RSS.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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"

Expand Down Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions examples/htmlWhiskers.hs
Original file line number Diff line number Diff line change
@@ -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 <b>best</b> site ever!!!"
, Author "[email protected] (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!"
]
]
2 changes: 1 addition & 1 deletion rss.cabal
Original file line number Diff line number Diff line change
@@ -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
Expand Down