File: Feed.hs

package info (click to toggle)
haskell-feed 0.3.7-4
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 416 kB
  • sloc: haskell: 3,423; xml: 1,096; makefile: 2
file content (263 lines) | stat: -rw-r--r-- 7,093 bytes parent folder | download | duplicates (2)
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
--------------------------------------------------------------------
-- |
-- Module    : Text.Atom.Feed
-- Copyright : (c) Galois, Inc. 2008
-- License   : BSD3
--
-- Maintainer: Sigbjorn Finne <sof@galois.com>
-- Stability : provisional
-- Portability:
--
--------------------------------------------------------------------

module Text.Atom.Feed where

import qualified Text.XML.Light as XML

-- *Core types

-- NOTE: In the future we may want to have more structured
-- types for these.
type URI        = String
type NCName     = String
type Date       = String
type MediaType  = String

data Feed
 = Feed
      { feedId           :: String
      , feedTitle        :: TextContent
      , feedUpdated      :: Date
      , feedAuthors      :: [Person]
      , feedCategories   :: [Category]
      , feedContributors :: [Person]
      , feedGenerator    :: Maybe Generator
      , feedIcon         :: Maybe URI
      , feedLinks        :: [Link]
      , feedLogo         :: Maybe URI
      , feedRights       :: Maybe TextContent
      , feedSubtitle     :: Maybe TextContent
      , feedEntries      :: [Entry]
      , feedAttrs        :: [XML.Attr]
      , feedOther        :: [XML.Element]
      }
     deriving (Show)

data Entry
 = Entry
      { entryId           :: String
      , entryTitle        :: TextContent
      , entryUpdated      :: Date
      , entryAuthors      :: [Person]
      , entryCategories   :: [Category]
      , entryContent      :: Maybe EntryContent
      , entryContributor  :: [Person]
      , entryLinks        :: [Link]
      , entryPublished    :: Maybe Date
      , entryRights       :: Maybe TextContent
      , entrySource       :: Maybe Source
      , entrySummary      :: Maybe TextContent
      , entryInReplyTo    :: Maybe InReplyTo
      , entryInReplyTotal :: Maybe InReplyTotal
      , entryAttrs        :: [XML.Attr]
      , entryOther        :: [XML.Element]
      }
     deriving (Show)

data EntryContent
 = TextContent   String
 | HTMLContent   String
 | XHTMLContent  XML.Element
 | MixedContent  (Maybe String) [XML.Content]
 | ExternalContent (Maybe MediaType) URI
     deriving (Show)

data Category
 = Category
       { catTerm   :: String         -- ^ the tag\/term of the category.
       , catScheme :: Maybe URI      -- ^ optional URL for identifying the categorization scheme.
       , catLabel  :: Maybe String   -- ^ human-readable label of the category
       , catOther  :: [XML.Element]  -- ^ unknown elements, for extensibility.
       }
     deriving (Show)


data Generator
 = Generator
       { genURI     :: Maybe URI
       , genVersion :: Maybe String
       , genText    :: String
       }
     deriving (Eq, Show)

data Link
 = Link
      { linkHref     :: URI
         -- ToDo: make the switch over to using the Atom.Feed.Link relation type.
      , linkRel      :: Maybe (Either NCName URI)
      , linkType     :: Maybe MediaType
      , linkHrefLang :: Maybe String
      , linkTitle    :: Maybe String
      , linkLength   :: Maybe String
      , linkAttrs    :: [XML.Attr]
      , linkOther    :: [XML.Element]
      }
     deriving (Show)

data TextContent
 = TextString  String
 | HTMLString  String
 | XHTMLString XML.Element
     deriving (Show)

txtToString :: TextContent -> String
txtToString (TextString s) = s
txtToString (HTMLString s) = s
txtToString (XHTMLString x) = show x

data Source
 = Source
      { sourceAuthors     :: [Person]
      , sourceCategories  :: [Category]
      , sourceGenerator   :: Maybe Generator
      , sourceIcon        :: Maybe URI
      , sourceId          :: Maybe String
      , sourceLinks       :: [Link]
      , sourceLogo        :: Maybe URI
      , sourceRights      :: Maybe TextContent
      , sourceSubtitle    :: Maybe TextContent
      , sourceTitle       :: Maybe TextContent
      , sourceUpdated     :: Maybe Date
      , sourceOther       :: [XML.Element]
      }
     deriving (Show)


data Person
 = Person
     { personName  :: String
     , personURI   :: Maybe URI
     , personEmail :: Maybe String
     , personOther :: [XML.Element]
     }
     deriving (Show)

data InReplyTo
 = InReplyTo
     { replyToRef     :: URI
     , replyToHRef    :: Maybe URI
     , replyToType    :: Maybe MediaType
     , replyToSource  :: Maybe URI
     , replyToOther   :: [XML.Attr]
     , replyToContent :: [XML.Content]
     }
     deriving (Show)

data InReplyTotal
 = InReplyTotal
     { replyToTotal      :: Integer -- non-negative :)
     , replyToTotalOther :: [XML.Attr]
     }
     deriving (Show)

-- *Smart Constructors

newCategory :: String -- ^catTerm
            -> Category
newCategory t = Category
  { catTerm   = t
  , catScheme = Nothing
  , catLabel  = Just t
  , catOther  = []
  }

nullFeed :: String  -- ^feedId
         -> TextContent -- ^feedTitle
         -> Date -- ^feedUpdated
         -> Feed
nullFeed i t u = Feed
      { feedId           = i
      , feedTitle        = t
      , feedUpdated      = u
      , feedAuthors      = []
      , feedCategories   = []
      , feedContributors = []
      , feedGenerator    = Nothing
      , feedIcon         = Nothing
      , feedLinks        = []
      , feedLogo         = Nothing
      , feedRights       = Nothing
      , feedSubtitle     = Nothing
      , feedEntries      = []
      , feedAttrs        = []
      , feedOther        = []
      }

nullEntry :: String -- ^entryId
          -> TextContent -- ^entryTitle
          -> Date -- ^entryUpdated
          -> Entry
nullEntry i t u = Entry
      { entryId           = i
      , entryTitle        = t
      , entryUpdated      = u
      , entryAuthors      = []
      , entryCategories   = []
      , entryContent      = Nothing
      , entryContributor  = []
      , entryLinks        = []
      , entryPublished    = Nothing
      , entryRights       = Nothing
      , entrySource       = Nothing
      , entrySummary      = Nothing
      , entryInReplyTo    = Nothing
      , entryInReplyTotal = Nothing
      , entryAttrs        = []
      , entryOther        = []
      }


nullGenerator :: String -- ^genText
              -> Generator
nullGenerator t = Generator
  { genURI     = Nothing
  , genVersion = Nothing
  , genText    = t
  }

nullLink :: URI -- ^linkHref
         -> Link
nullLink uri = Link
  { linkHref      = uri
  , linkRel       = Nothing
  , linkType      = Nothing
  , linkHrefLang  = Nothing
  , linkTitle     = Nothing
  , linkLength    = Nothing
  , linkAttrs     = []
  , linkOther     = []
  }

nullSource :: Source
nullSource = Source
      { sourceAuthors     = []
      , sourceCategories  = []
      , sourceGenerator   = Nothing
      , sourceIcon        = Nothing
      , sourceId          = Nothing
      , sourceLinks       = []
      , sourceLogo        = Nothing
      , sourceRights      = Nothing
      , sourceSubtitle    = Nothing
      , sourceTitle       = Nothing
      , sourceUpdated     = Nothing
      , sourceOther       = []
      }
  
nullPerson :: Person
nullPerson = Person
  { personName  = ""
  , personURI   = Nothing
  , personEmail = Nothing
  , personOther = []
  }