1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
|
--------------------------------------------------------------------
-- |
-- Module : Text.Atom.Feed.Export
-- Copyright : (c) Galois, Inc. 2008,
-- (c) Sigbjorn Finne 2009-
-- License : BSD3
--
-- Maintainer: Sigbjorn Finne <sof@forkIO.com>
-- Stability : provisional
-- Portability:: portable
-- Description: Convert from Atom to XML
--
-- Convert from Atom to XML
--
--------------------------------------------------------------------
module Text.Atom.Feed.Export where
import Text.XML.Light as XML
import Text.Atom.Feed
atom_prefix :: Maybe String
atom_prefix = Nothing -- Just "atom"
atom_thr_prefix :: Maybe String
atom_thr_prefix = Just "thr"
atomNS :: String
atomNS = "http://www.w3.org/2005/Atom"
atomThreadNS :: String
atomThreadNS = "http://purl.org/syndication/thread/1.0"
xmlns_atom :: Attr
xmlns_atom = Attr qn atomNS
where
qn = case atom_prefix of
Nothing -> QName { qName = "xmlns"
, qURI = Nothing
, qPrefix = Nothing
}
Just s -> QName { qName = s
, qURI = Nothing -- XXX: is this ok?
, qPrefix = Just "xmlns"
}
xmlns_atom_thread :: Attr
xmlns_atom_thread = Attr qn atomThreadNS
where
qn = case atom_prefix of
Nothing -> QName { qName = "xmlns"
, qURI = Nothing
, qPrefix = Nothing
}
Just s -> QName { qName = s
, qURI = Nothing -- XXX: is this ok?
, qPrefix = Just "xmlns"
}
atomName :: String -> QName
atomName nc = QName { qName = nc
, qURI = Just atomNS
, qPrefix = atom_prefix
}
atomAttr :: String -> String -> Attr
atomAttr x y = Attr (atomName x) y
atomNode :: String -> [XML.Content] -> XML.Element
atomNode x xs = blank_element { elName = atomName x, elContent = xs }
atomLeaf :: String -> String -> XML.Element
atomLeaf tag txt = blank_element
{ elName = atomName tag
, elContent = [ Text blank_cdata { cdData = txt } ]
}
atomThreadName :: String -> QName
atomThreadName nc =
QName { qName = nc
, qURI = Just atomThreadNS
, qPrefix = atom_thr_prefix
}
atomThreadAttr :: String -> String -> Attr
atomThreadAttr x y = Attr (atomThreadName x) y
atomThreadNode :: String -> [XML.Content] -> XML.Element
atomThreadNode x xs =
blank_element { elName = atomThreadName x, elContent = xs }
atomThreadLeaf :: String -> String -> XML.Element
atomThreadLeaf tag txt =
blank_element { elName = atomThreadName tag
, elContent = [ Text blank_cdata { cdData = txt } ]
}
--------------------------------------------------------------------------------
xmlFeed :: Feed -> XML.Element
xmlFeed f = ( atomNode "feed"
$ map Elem
$ [ xmlTitle (feedTitle f) ]
++ [ xmlId (feedId f) ]
++ [ xmlUpdated (feedUpdated f) ]
++ map xmlLink (feedLinks f)
++ map xmlAuthor (feedAuthors f)
++ map xmlCategory (feedCategories f)
++ map xmlContributor (feedContributors f)
++ mb xmlGenerator (feedGenerator f)
++ mb xmlIcon (feedIcon f)
++ mb xmlLogo (feedLogo f)
++ mb xmlRights (feedRights f)
++ mb xmlSubtitle (feedSubtitle f)
++ map xmlEntry (feedEntries f)
++ feedOther f )
{ elAttribs = [xmlns_atom] }
xmlEntry :: Entry -> XML.Element
xmlEntry e = ( atomNode "entry"
$ map Elem
$ [ xmlId (entryId e) ]
++ [ xmlTitle (entryTitle e) ]
++ [ xmlUpdated (entryUpdated e) ]
++ map xmlAuthor (entryAuthors e)
++ map xmlCategory (entryCategories e)
++ mb xmlContent (entryContent e)
++ map xmlContributor (entryContributor e)
++ map xmlLink (entryLinks e)
++ mb xmlPublished (entryPublished e)
++ mb xmlRights (entryRights e)
++ mb xmlSource (entrySource e)
++ mb xmlSummary (entrySummary e)
++ mb xmlInReplyTo (entryInReplyTo e)
++ mb xmlInReplyTotal (entryInReplyTotal e)
++ entryOther e )
{ elAttribs = entryAttrs e }
xmlContent :: EntryContent -> XML.Element
xmlContent cont = case cont of
TextContent t -> (atomLeaf "content" t)
{ elAttribs = [ atomAttr "type" "text" ] }
HTMLContent t -> (atomLeaf "content" t)
{ elAttribs = [ atomAttr "type" "html" ] }
XHTMLContent x -> (atomNode "content" [ Elem x ])
{ elAttribs = [ atomAttr "type" "xhtml" ] }
MixedContent mbTy cs -> (atomNode "content" cs)
{ elAttribs = mb (atomAttr "type") mbTy }
ExternalContent mbTy src -> (atomNode "content" [])
{ elAttribs = [ atomAttr "src" src ]
++ mb (atomAttr "type") mbTy }
xmlCategory :: Category -> XML.Element
xmlCategory c = (atomNode "category" (map Elem (catOther c)))
{ elAttribs = [ atomAttr "term" (catTerm c) ]
++ mb (atomAttr "scheme") (catScheme c)
++ mb (atomAttr "label") (catLabel c)
}
xmlLink :: Link -> XML.Element
xmlLink l = (atomNode "link" (map Elem (linkOther l)))
{ elAttribs = [ atomAttr "href" (linkHref l) ]
++ mb (atomAttr "rel" . either id id) (linkRel l)
++ mb (atomAttr "type") (linkType l)
++ mb (atomAttr "hreflang") (linkHrefLang l)
++ mb (atomAttr "title") (linkTitle l)
++ mb (atomAttr "length") (linkLength l)
++ linkAttrs l
}
xmlSource :: Source -> Element
xmlSource s = atomNode "source"
$ map Elem
$ sourceOther s
++ map xmlAuthor (sourceAuthors s)
++ map xmlCategory (sourceCategories s)
++ mb xmlGenerator (sourceGenerator s)
++ mb xmlIcon (sourceIcon s)
++ mb xmlId (sourceId s)
++ map xmlLink (sourceLinks s)
++ mb xmlLogo (sourceLogo s)
++ mb xmlRights (sourceRights s)
++ mb xmlSubtitle (sourceSubtitle s)
++ mb xmlTitle (sourceTitle s)
++ mb xmlUpdated (sourceUpdated s)
xmlGenerator :: Generator -> Element
xmlGenerator g = (atomLeaf "generator" (genText g))
{ elAttribs = mb (atomAttr "uri") (genURI g)
++ mb (atomAttr "version") (genVersion g)
}
xmlAuthor :: Person -> XML.Element
xmlAuthor p = atomNode "author" (xmlPerson p)
xmlContributor :: Person -> XML.Element
xmlContributor c = atomNode "contributor" (xmlPerson c)
xmlPerson :: Person -> [XML.Content]
xmlPerson p = map Elem $
[ atomLeaf "name" (personName p) ]
++ mb (atomLeaf "uri") (personURI p)
++ mb (atomLeaf "email") (personEmail p)
++ personOther p
xmlInReplyTo :: InReplyTo -> XML.Element
xmlInReplyTo irt =
(atomThreadNode "in-reply-to" (replyToContent irt))
{ elAttribs =
mb (atomThreadAttr "ref") (Just $ replyToRef irt)
++ mb (atomThreadAttr "href") (replyToHRef irt)
++ mb (atomThreadAttr "type") (replyToType irt)
++ mb (atomThreadAttr "source") (replyToSource irt)
++ replyToOther irt
}
xmlInReplyTotal :: InReplyTotal -> XML.Element
xmlInReplyTotal irt =
(atomThreadLeaf "total" (show $ replyToTotal irt))
{ elAttribs = replyToTotalOther irt }
xmlId :: String -> XML.Element
xmlId i = atomLeaf "id" i
xmlIcon :: URI -> XML.Element
xmlIcon i = atomLeaf "icon" i
xmlLogo :: URI -> XML.Element
xmlLogo l = atomLeaf "logo" l
xmlUpdated :: Date -> XML.Element
xmlUpdated u = atomLeaf "updated" u
xmlPublished :: Date -> XML.Element
xmlPublished p = atomLeaf "published" p
xmlRights :: TextContent -> XML.Element
xmlRights r = xmlTextContent "rights" r
xmlTitle :: TextContent -> XML.Element
xmlTitle r = xmlTextContent "title" r
xmlSubtitle :: TextContent -> XML.Element
xmlSubtitle s = xmlTextContent "subtitle" s
xmlSummary :: TextContent -> XML.Element
xmlSummary s = xmlTextContent "summary" s
xmlTextContent :: String -> TextContent -> XML.Element
xmlTextContent tg t =
case t of
TextString s -> (atomLeaf tg s) { elAttribs = [atomAttr "type" "text"] }
HTMLString s -> (atomLeaf tg s) { elAttribs = [atomAttr "type" "html"] }
XHTMLString e -> (atomNode tg [XML.Elem e])
{ elAttribs = [atomAttr "type" "xhtml"] }
--------------------------------------------------------------------------------
mb :: (a -> b) -> Maybe a -> [b]
mb _ Nothing = []
mb f (Just x) = [f x]
|