]> gitweb.fperrin.net Git - atom.git/blob - atom.el
Use plist for optional arguments
[atom.git] / atom.el
1 ;;; atom.el --- Create an Atom feed  -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 2011  Frédéric Perrin
4
5 ;; Author: Frédéric Perrin <frederic.perrin@resel.fr>
6 ;; Keywords: www, hypermedia, atom, rss
7
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
12
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 ;; GNU General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
21 ;;; Commentary:
22
23 ;; This is a library for creating an Atom feed from a Lisp program.
24 ;; The normal usage is to create a feed with `atom-create', giving it
25 ;; a title and a Web address. Once the feed has been created, entries
26 ;; may be added to the feed, by specifying (at the minimum) a title, a
27 ;; permanent link and the content of the entry. Text-only, HTML and
28 ;; XHTML entries are supported.
29
30 ;; It is possible to produce both Atom and RSS feeds.
31
32 ;; A typical usage would look like this:
33
34 ;; (let ((my-atom-feed (atom-create "My feed" "http://example.org")))
35 ;;   ; A simple, text-only entry
36 ;;   (atom-add-text-entry
37 ;;    my-atom-feed
38 ;;    "Hello world"
39 ;;    "http://example.org/hello"
40 ;;    "Hello the world!")
41 ;;
42 ;;   (atom-add-xhtml-entry
43 ;;    my-atom-feed
44 ;;    "An XHTML example"
45 ;;    "http://example.org/html-example"
46 ;;    "<p>One can also use <acronym>XHTML</acronym> in the entries.</p>")
47 ;;
48 ;;   (atom-print my-atom-feed)
49 ;;   ;; If you prefer RSS feeds:
50 ;;   (atom-print-as-rss my-atom-feed))
51
52 ;; Full documentation is available at <http://tar-jx.bz/code/atom.html>.
53 ;; See 
54
55 ;;; Code:
56
57 (require 'xml)
58 (require 'url-parse)
59
60 (defun atom-create (title link &optional props)
61   "Create a new atom structure.
62
63 TITLE is the title for the feed, a short, text-only, human
64 readable string.
65
66 LINK is the URL of a page responible for the content of this
67 feed.
68
69 PROPS is an optional plist with the following properties:
70
71 - :subtitle is a subtitle for the feed; it can be a bit longer than
72   TITLE, maybe a paragraph long.
73
74 - :self is the canonical URL to this feed.
75
76 - :id is a unique identifier for this feed. If not given, it
77   defaults to :self.
78
79 - :author is the author of the feed. See `atom-massage-author' for
80 the possible ways to specify it. In particular, `nil' uses
81 `user-full-name' and `user-mail-address'.
82
83 - :updated is the date the feed was last updated. If not given,
84 `(current-time)' is used."
85   (let ((atom-feed (list (list 'title nil title))))
86     (atom-modify-entry atom-feed 'link `(((href . ,link))))
87     (atom-modify-entry atom-feed 'author (atom-massage-author (plist-get props :author)))
88     (if (plist-member props :subtitle)
89         (atom-modify-entry atom-feed 'subtitle (plist-get props :subtitle)))
90     (if (plist-member props :self)
91         (atom-modify-entry atom-feed 'link
92                            `(((href . ,(plist-get props :self)) (rel . "self")
93                               (type . "application/atom+xml")))))
94     (atom-modify-entry atom-feed 'updated (atom-format-time (plist-get props :updated)))
95     (atom-modify-entry atom-feed 'id (or (plist-get props :id) (plist-get props :self) link))
96     atom-feed))
97
98 (defun atom-push-entry (atom entry)
99   "Add the entry ENTRY to the feed ATOM."
100   (nconc atom (list `(entry nil ,@entry))))
101
102 (defun atom-modify-entry (entry name val)
103   "Set the NAME element of ENTRY to VAL."
104   (let ((elem (if (stringp val)
105                   (list name nil val)
106                 (cons name val))))
107     (nconc entry (list elem))))
108
109 (defun atom-add-entry (atom title link content &optional props)
110   "Add an entry to the atom flux ATOM. Return the newly added
111 entry.
112
113 TITLE is a short, text-only, human readable string.
114
115 LINK is a permanent link for this entry. For a given entry, LINK
116 may change between successive generations of the atom feed.
117
118 CONTENT is the content of the entry; use `atom-add-html-entry'
119 or `atom-add-xhtml-entry' when CONTENT is not text-only.
120
121 If SUMMARY is not given, the entry will not contain any summary.
122
123 UPDATED defaults to `(current-time)' if omitted, which is
124 probably not a very good default.
125
126 ID defaults to LINK, which is not optimal; see `atom-generate-id'
127 for a way to create good identifiers. For a given entry, it must
128 not change between successive generations of the atom feed, even
129 when the content of the entry changes."
130   (let ((entry (list (list 'title nil title))))
131     (atom-modify-entry entry 'link  (list (list (cons 'href link))))
132     (atom-modify-entry entry 'id (or (plist-get props :id) link))
133     (atom-modify-entry entry 'updated (atom-format-time (plist-get props :updated)))
134     (if (plist-member props :summary)
135         (atom-modify-entry entry 'summary (plist-get props :summary)))
136     (atom-modify-entry entry 'content content)
137     (atom-push-entry atom entry)
138     entry))
139
140 (defalias 'atom-add-text-entry 'atom-add-entry
141   "Add an entry to ATOM, with a textual content. See
142 `atom-add-entry' for details.")
143
144 (defun atom-add-html-entry (atom title link content &optional props)
145   "Add an entry to ATOM, with some HTML content. CONTENT should
146 be a string enconding a valid HTML fragment. See `atom-add-entry'
147 for additional details."
148   (if (plist-member props :summary)
149       (plist-put props :summary (atom-massage-html (plist-get props :summary))))
150   (atom-add-entry atom title link (atom-massage-html content) props))
151
152 (defun atom-add-xhtml-entry (atom title link content &optional props)
153   "Add an entry to ATOM, with some XHTML content. CONTENT may be
154 given either as a string, or as an XML tree, of a valid XHTML
155 fragment. See `atom-add-entry' for additional details."
156   (if (plist-member props :summary)
157       (plist-put props :summary (atom-massage-xhtml (plist-get props :summary))))
158   (atom-add-entry atom title link (atom-massage-xhtml content) props))
159
160 (defun atom-print (atom)
161   "Print the Atom feed ATOM in the current buffer."
162   (insert atom-xml-declaration)
163   (insert "<feed xmlns=\"http://www.w3.org/2005/Atom\">\n")
164   (xml-print atom)
165   (insert "\n</feed>"))
166
167 (defun atom-write-file (atom filename)
168   "Writes the feed ATOM to FILENAME."
169   (with-temp-buffer
170     (atom-print atom)
171     (write-file filename)))
172
173 \f
174 (defun atom-to-rss (atom)
175   "Translate an Atom feed into an RSS one, returning the translation.
176
177 Some information may be lost or approximated."
178   (let ((rss (list (assoc 'title atom))))
179     (atom-to-rss-translator atom rss '((subtitle . description)
180                                        (updated . pubDate)
181                                        (link . link)))
182     (atom-to-rss-modify-time rss)
183     (atom-to-rss-modify-link rss)
184     (dolist (entry (xml-get-children atom 'entry))
185       (push (atom-to-rss-item entry) rss))
186     (reverse rss)))
187
188 (defun atom-to-rss-item (entry)
189   "Translates an Atom entry into an RSS item."
190   (let ((item (list (assoc 'title entry))))
191     (atom-to-rss-translator
192      (xml-node-children entry) item
193      '((id . guid) (content . description) (updated . pubDate) (link . link)))
194     (atom-to-rss-modify-time item)
195     (atom-to-rss-modify-link item)
196     (let ((guid (assoc 'guid item))
197           (descr (assoc 'description item)))
198       (if guid
199           (setcar (cdr guid) (list (cons 'isPermaLink "false"))))
200       (if (and descr
201                (equal (xml-get-attribute descr 'type) "xhtml"))
202           (setcar (cddr descr) (xml-node-as-text descr))))
203     `(item nil ,@item)))
204
205 (defun atom-to-rss-translator (source target translations)
206   (dolist (translation translations)
207     (let* ((from (car translation))
208            (to (cdr translation))
209            (data (copy-tree (cdr (assoc from source)))))
210       (when data
211         (atom-modify-entry target to data)))))
212
213 (defun atom-to-rss-modify-link (entry)
214   (let* ((link (assoc 'link entry))
215          (link-addr (xml-get-attribute-or-nil link 'href)))
216     (when link
217       (setcar (cdr link) nil)
218       (setcdr (cdr link) (cons link-addr nil)))))
219
220 (defun atom-print-as-rss (atom)
221   (let ((rss (atom-to-rss atom)))
222     (insert atom-xml-declaration)
223     (insert "<rss version=\"2.0\">\n")
224     (insert "  <channel>\n")
225     (xml-print rss "    ")
226     (insert "\n  </channel>\n")
227     (insert "</rss>")))
228
229 (defun atom-to-rss-time (time)
230   "Translates a string from the format used by Atom into the
231 format used by RSS."
232   (let ((system-time-locale "C"))
233     (format-time-string "%a, %d %b %Y %T %z" (atom-parse-time time))))
234
235 (defun atom-to-rss-modify-time (entry)
236   "Modify ENTRY, changing the format of the `pubDate' in it."
237   (let ((pubDate (assoc 'pubDate entry)))
238     (setcar (cddr pubDate)
239             (atom-to-rss-time (car (xml-node-children pubDate))))))
240
241 (defun atom-to-rss-write-file (atom filename)
242   "Saves ATOM as a RSS feed into FILENAME."
243   (with-temp-buffer
244     (atom-print-as-rss atom)
245     (write-file filename)))
246
247 \f
248 (defvar atom-time-format-string "%Y-%m-%dT%T%z"
249   "The format for string representation of dates.")
250
251 (defvar atom-xhtml-namespace "http://www.w3.org/1999/xhtml")
252
253 (defvar atom-xml-declaration "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n")
254
255 (defun atom-format-time (&optional time)
256   "Format a time according to RFC3339."
257   ;; The time zone must be specified in numeric form, but with a colon between
258   ;; the hour and minute parts.
259   (replace-regexp-in-string
260    "\\(..\\)$" ":\\1"
261    (format-time-string atom-time-format-string time)))
262
263 (defun atom-parse-time (&optional time)
264   "Parse a time as specified in RFC3339 into Emacs's native format."
265   ;; Same remark as in `atom-format-time'
266   (date-to-time (replace-regexp-in-string ":\\(..\\)$" "\\1" time)))
267
268 (defun atom-massage-html (content)
269   "Massage CONTENT so it can be used as an HTML fragment in an
270 Atom feed. CONTENT must be a string."
271   (list '((type . "html")) content))
272
273 (defun atom-string-to-xml (string)
274   "Convert STRING into a Lisp structure as used by `xml.el'."
275   (require 'xml-xhtml-entities)
276   (let ((xml-entity-alist xml-xhtml-entities)
277         (xml-validating-parser t))
278     (with-temp-buffer
279       (insert "<div xmlns=\"" atom-xhtml-namespace "\">")
280       (insert string)
281       (insert "</div>")
282       ;; `xml-parse-region' returns a list of elements, even though it
283       ;; requires an only root node. We are only interested in the first
284       ;; one, the DIV we just inserted.
285       (car (xml-parse-region (point-min) (point-max))))))
286
287 (defun atom-massage-xhtml (content)
288   "Massage CONTENT so it can be used as an XHTML fragment in an
289 Atom feed."
290   (list '((type . "xhtml"))
291         (or (and (stringp content)
292                  (atom-string-to-xml content))
293             `(div ((xmlns . ,atom-xhtml-namespace)) ,@content))))
294
295 (defun atom-massage-author (author)
296   "Return an XML node representing the author. AUTHOR can be:
297 - nil, in which case `user-full-name' and `user-mail-address' are
298   used;
299 - a single string, the full name of the author; no email address
300   will be included;
301 - a list with two elements, the full name and the email address
302   of the author;
303 - something else, assumed to be a complete `atomPersonConstruct'."
304   `(nil ,@(cond
305            ((null author) `((name nil ,user-full-name)
306                             (email nil ,user-mail-address)))
307            ((stringp author) `((name nil ,author)))
308            ((= 2 (length author)) `((name nil ,(car author))
309                                     (email nil ,(cadr author))))
310            (t `(author nil ,author)))))
311
312 (defun atom-xhtml-convert-links (node base)
313   "Make all links in NODE (a fragment of an XHTML document)
314 absolute, in the context of BASE, an URL."
315   (dolist (attr-name (list 'href 'src))
316     (let ((attr (assoc attr-name (xml-node-attributes node))))
317       (when attr (setcdr attr (url-canonalize (cdr attr) base)))))
318   (dolist (child (xml-node-children node))
319     (when (listp child) (atom-xhtml-convert-links child base))))
320
321 (defun atom-generate-id (link creation-date)
322   "Generate a string suitable for use as an atom:id element. This
323 implements Mark Pilgrom's tag: URI method, using the
324 CREATION-DATE of the entry, and the domain part of LINK.
325
326 See <https://www.xml.com/pub/a/2004/08/18/pilgrim.html>."
327     (format "tag:%s,%s:/%s"
328             (url-host (url-generic-parse-url link))
329             (format-time-string "%Y-%m-%d" creation-date)
330             (format-time-string "%Y%m%d%H%M%S" creation-date)))
331
332 \f
333 ;;; Functions that should probably not be there
334
335 (defun url-canonalize (address base)
336   "Make ADRESS an absolute URL, taking it in the BASE context."
337   ;; I feel such a function should exist in `url-parse'. Did I miss it?
338   (let ((url-base (url-generic-parse-url base))
339         (url-address (url-generic-parse-url address)))
340     (if (url-host url-address)
341         address
342       (setf (url-filename url-base)
343             (expand-file-name address
344                               (file-name-directory (url-filename url-base))))
345       (url-recreate-url url-base))))
346
347 (defun xml-node-as-text (node)
348   "Return a string representing NODE, an XML structure."
349   (with-temp-buffer
350     (xml-print (xml-node-children node))
351     (buffer-string)))
352
353 (defun xml-node-create (name attrlist childlist)
354   "Create a new XML node."
355   (list name attrlist . childlist))
356
357 (provide 'atom)
358 ;;; atom.el ends here