File: CreateAtom.hs

package info (click to toggle)
haskell-feed 1.3.2.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 820 kB
  • sloc: haskell: 4,735; xml: 4,315; makefile: 2
file content (58 lines) | stat: -rw-r--r-- 1,462 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
module Example.CreateAtom
  ( atomFeed
  ) where

import Prelude.Compat hiding (take)

import qualified Text.Atom.Feed as Atom
import qualified Text.Atom.Feed.Export as Export
import Text.RSS.Utils

import Data.Text
import Data.Text.Lazy (toStrict)
import Text.XML

atomFeed :: Maybe Text
atomFeed = renderFeed examplePosts

data Post =
  Post
    { _postedOn :: Text
    , _url :: Text
    , _content :: Text
    }

examplePosts :: [Post]
examplePosts =
  [ Post "2000-02-02T18:30:00Z" "http://example.com/2" "Bar."
  , Post "2000-01-01T18:30:00Z" "http://example.com/1" "Foo."
  ]

toEntry :: Post -> Atom.Entry
toEntry (Post date url content) =
  (Atom.nullEntry
     url -- The ID field. Must be a link to validate.
     (Atom.TextString (take 20 content)) -- Title
     date)
    { Atom.entryAuthors = [Atom.nullPerson {Atom.personName = "J. Smith"}]
    , Atom.entryLinks = [Atom.nullLink url]
    , Atom.entryContent = Just (Atom.HTMLContent content)
    }

feed :: [Post] -> Atom.Feed
feed posts =
  Atom.nullFeed
    "http://example.com/atom.xml" -- ID
    (Atom.TextString "Example Website") -- Title
    (case posts -- Updated
           of
       Post latestPostDate _ _:_ -> latestPostDate
       _ -> "")

renderFeed :: [Post] -> Maybe Text
renderFeed posts =
  fmap (toStrict . renderText def) $
  elementToDoc $
  Export.xmlFeed $
  (feed posts)
    {Atom.feedEntries = fmap toEntry posts, Atom.feedLinks = [Atom.nullLink "http://example.com/"]}