summaryrefslogtreecommitdiff
path: root/src/atom.scm
blob: 6e7a8b269b3a715440640f6528bd1e662d31af46 (plain)
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
(define-module (src atom)
  #:use-module (src mime-types)
  #:use-module (src dates)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-9)
  #:use-module (sxml simple)
  #:use-module (ice-9 binary-ports)
  #:use-module (rnrs bytevectors)
  #:export (feed
            append-feed-entry!
            entry
            person
            media
            render))

; ATOM Explanation: http://www.atomenabled.org/developers/syndication/
; ATOM Validator:   https://validator.w3.org/feed/check.cgi

;; Entities
(define-record-type <feed>
  (%make-feed id title subtitle uri updated entries)
  feed?
  (id feed-id)
  (title feed-title)
  (subtitle feed-subtitle)
  (uri feed-uri)
  (updated feed-updated set-feed-updated!)
  (entries feed-entries set-feed-entries!))

(define-record-type <entry>
  (%make-entry id title published updated authors summary content categories contributors media)
  entry?
  (id entry-id set-entry-id!)
  (title entry-title)
  (published entry-published)
  (updated entry-updated)
  (authors entry-authors)
  (summary entry-summary)
  (content entry-content)
  (categories entry-categories)
  (contributors entry-contributors)
  (media entry-media))

(define-record-type <media>
  (%make-media title type length uri path)
  media?
  (title media-title)
  (type media-type)
  (length media-length)
  (uri media-uri)
  (path media-path))

(define-record-type <person>
  (%make-person name email uri)
  person?
  (name person-name)
  (email person-email)
  (uri person-uri))

(define* (append-feed-entry! feed entry)
         (let ((entries (feed-entries feed)))
           (set-entry-id!     entry (string-append
                                      (feed-uri feed) "/"
                                      (entry-id entry))) ;; TODO: is this enough
           (set-feed-entries! feed (append entries (list entry))))
           (set-feed-updated! feed (find-newest (map entry-updated
                                                     (feed-entries feed)))))

(define* (feed #:key (title              "")
                     (subtitle           "")
                     (uri                "")
                     (atom-feed-location "")
                     (posts             '())
               #:allow-other-keys)
  (let* ((f (%make-feed uri
                        title
                        subtitle
                        (string-append uri atom-feed-location)
                        (find-newest (map entry-updated posts))
                        '()))
         (append-entry! (partial append-feed-entry! f)))
    (for-each append-entry!
              (sort posts (lambda (a b)
                            (date>? (entry-published a) (entry-published b)))))
    f))


(define* (entry  #:key (title "")
                       (id #f)
                       (published unix-date)
                       (updated unix-date)
                       (authors '())
                       (summary-html '())
                       (content-html '())
                       (categories '())
                       (contributors '())
                       (media '())
                 #:allow-other-keys)
  (define valid (char-set-intersection char-set:ascii char-set:letter))
  (define (id-from-title title)
    (string-map (lambda (c)
                  (if (char-set-contains? valid c) c #\-))
                title))

  (%make-entry (or id (id-from-title title))
               title
               published
               (find-newest (list published updated))
               authors
               summary-html
               content-html
               categories
               contributors
               media))

(define* (person name #:key (email "")
                            (uri   "")
                      #:allow-other-keys)
  (%make-person name email uri))


(define* (media path #:key (title "")
                           (uri   "")
                     #:allow-other-keys)
  (define (type path)
    (let* ((extension (car (last-pair (string-split path #\.))))
           (type (assoc-ref mime-types extension)))
      (if (string? type) type (throw "Unknown mime-type"))))
  (let ((contents (call-with-input-file path get-bytevector-all #:binary #t)))
    (%make-media title
                 (type path)
                 (bytevector-length contents)
                 uri
                 path)))

;; Rendering
(define (render-person tag person)
  `(,tag
     (name  ,(person-name person))
     (uri   ,(or (person-uri person) ""))
     (email ,(or (person-email person) ""))))

(define (partial  f . set)
  (lambda (. args) (apply f (append set args))))
(define render-author      (partial render-person 'author))
(define render-contributor (partial render-person 'contributor))

(define (render-media med)
  `(link (@(rel enclosure)
           (href ,(media-uri med))
           (type ,(media-type med))
           (title ,(media-title med))
           (length ,(media-length med)))))

(define (render-category cat)
  `(category (@(term ,cat))))

(define (sxml->string/rendered content)
  (call-with-output-string
    (lambda (p) (sxml->xml content p))))

(define (render-entry ent)
  `(entry
     (id                      ,(entry-id ent))
     (title                   ,(entry-title ent))
     (published               ,(date->string/RFC3339 (entry-published ent)))
     (updated                 ,(date->string/RFC3339 (entry-updated ent)))
     ,@(map render-author      (entry-authors ent))
     (summary (@(type html))  ,(sxml->string/rendered (entry-summary ent)))
     (content (@(type html))  ,(sxml->string/rendered (entry-content ent)))
     ,@(map render-category    (entry-categories ent))
     ,@(map render-contributor (entry-contributors ent))
     ,@(map render-media       (entry-media ent))))

(define (render-feed fe)
  `(feed (@(xmlns "http://www.w3.org/2005/Atom"))
     (id ,(feed-id fe))
     (title ,(feed-title fe))
     (link (@(href ,(feed-uri fe))
             (rel "self")))
     (updated ,(date->string/RFC3339 (feed-updated fe)))
     (subtitle ,(feed-subtitle fe))
     ,@(map render-entry (feed-entries fe))))

(define (render feed)
  (display "<?xml version=\"1.0\" encoding=\"utf-8\"?>")
  (sxml->xml (render-feed feed)))