]> gitweb.fperrin.net Git - atom.git/blob - atom.el
Add the generation of the atom:id element.
[atom.git] / atom.el
1 ;;; atom.el --- Create an Atom feed
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 ;; A feed is really a Lisp structure as used by the `xml.el' package,
31 ;; without the parent `feed' element.
32
33 ;; A typical usage would look like this:
34
35 ;; (let ((my-atom-feed (atom-create "My feed" "http://example.org")))
36 ;;   ; A simple, text-only entry
37 ;;   (atom-add-text-entry
38 ;;    my-atom-feed
39 ;;    "Hello world"
40 ;;    "http://example.org/hello"
41 ;;    "Hello the world!")
42 ;;
43 ;;   ; A text-only entry, with all the optional pieces of data
44 ;;   (atom-add-text-entry
45 ;;    my-atom-feed
46 ;;    "Bonjour"
47 ;;    "http://example.org/bonjour"
48 ;;    "Bonjour à tout le monde !"
49 ;;    ;; optional: the last modification time
50 ;;    (date-to-time "2011-01-30 23:40:12")
51 ;;    ;; optional: an identifier for this entry; a common way to generate it is
52 ;;    ;; to use the domain name and the creation date of the entry.
53 ;;    (atom-generate-id "http://example.org"
54 ;;                   (date-to-time "2011-01-30 10:01:05"))
55 ;;    ;; optional: a summary for this entry
56 ;;    "Bonjour, monde.")
57 ;;
58 ;;   (atom-add-xhtml-entry
59 ;;    my-atom-feed
60 ;;    "An XHTML example"
61 ;;    "http://example.org/html-example"
62 ;;    "<p>One can also use <acronym>XHTML</acronym> in the entries.</p>")
63 ;;   (atom-print my-atom-feed))
64
65
66 ;;; Code:
67
68 (require 'xml)
69
70 (defun atom-create (title link &optional author updated id)
71   "Create a new atom structure.
72
73 TITLE is the title for the feed, a short, text-only, human
74 readable string.
75
76 AUTHOR is the author of the feed. See `atom-massage-author' for
77 the possible ways to specify it.
78
79 LINK is the URL of a page responible for the content of this
80 feed.
81
82 UPDATED is the date the feed was last updated. If not given,
83 `(current-time)' is used.
84
85 ID is a unique identifier for this feed. If not given, it
86 defaults to LINK."
87   `((title nil ,title)
88     (link ((href . ,link)))
89     ,(atom-massage-author author)
90     (updated nil ,(atom-format-time updated))
91     (id nil ,(or id link))))
92
93 (defun atom-push-entry (atom entry)
94   "Add the entry ENTRY to the feed ATOM."
95   (nconc atom (list `(entry nil . ,entry))))
96
97 (defun atom-modify-entry (entry name val)
98   "Set the NAME element of ENTRY to VAL."
99   (let ((elem (assoc name entry)))
100     (if elem
101         (if (stringp val)
102             (setcar (cddr elem) val)
103           (setcdr elem val))
104       (setq 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
110                             &optional updated id summary)
111   "Add an entry to the atom flux ATOM. Return the newly added
112 entry.
113
114 TITLE is a short, text-only, human readable string.
115
116 LINK is a permanent link for this entry. For a given entry, LINK
117 may change between successive generations of the atom feed.
118
119 CONTENT is the content of the entry; use `atom-add-html-entry'
120 or `atom-add-xhtml-entry' when CONTENT is not text-only.
121
122 If SUMMARY is not given, the entry will not contain any summary.
123
124 UPDATED defaults to `(current-time)' if omitted, which is
125 probably not a very good default.
126
127 ID defaults to LINK, which is not optimal; see `atom-generate-id'
128 for a way to create good identifiers. For a given entry, it must
129 not change between successive generations of the atom feed, even
130 when the content of the entry ."
131   (let ((entry (list (list 'title nil title))))
132     (atom-modify-entry entry 'link  (list (list (cons 'href link))))
133     (atom-modify-entry entry 'id (or id link))
134     (atom-modify-entry entry 'updated (atom-format-time updated))
135     (if summary (atom-modify-entry entry 'summary 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
145                                   &optional updated id summary)
146   "Add an entry to ATOM, with some HTML content. CONTENT should
147 be a string enconding a valid HTML fragment. See `atom-add-entry'
148 for additional details."
149   (atom-add-entry atom
150    title link
151    (atom-massage-html content)
152    (and summary (atom-massage-html summary))
153    updated id))
154
155 (defun atom-add-xhtml-entry (atom title link content
156                                   &optional updated id summary)
157   "Add an entry to ATOM, with some XHTML content. CONTENT may be
158 given either as a string, or as an XML tree, of a valid XHTML
159 fragment. See `atom-add-entry' for additional details."
160   (atom-add-entry atom
161    title link
162    (atom-massage-xhtml content)
163    (and summary (atom-massage-xhtml summary))
164    updated id))
165
166 (defun atom-print (atom)
167   "Print the Atom feed ATOM in the current buffer."
168   (insert "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n")
169   (insert "<feed xmlns=\"http://www.w3.org/2005/Atom\">\n")
170   (xml-print atom)
171   (insert "\n</feed>"))
172
173 \f
174 (defun atom-format-time (&optional time)
175   "Format a time according to RFC3339."
176   ;; The time zone must be specified in numeric form, but with a colon between
177   ;; the hour and minute parts.
178   (replace-regexp-in-string
179    "\\(..\\)$"
180    ":\\1"
181    (format-time-string "%Y-%m-%dT%T%z" time)))
182
183 (defun atom-massage-html (content)
184   "Massage CONTENT so it can be used as an HTML fragment in an
185 Atom feed. CONTENT must be a string."
186   (list '((type . "html")) content))
187
188 (defun atom-string-to-xml (string)
189   "Convert STRING into a Lisp structure as used by `xml.el'."
190   (with-temp-buffer
191     (insert string)
192     (xml-parse-region (point-min) (point-max))))
193
194 (defun atom-massage-xhtml (content)
195   "Massage CONTENT so it can be used as an XHTML fragment in an
196 Atom feed."
197   (list '((type . "xhtml"))
198         `(div ((xmlns . "http://www.w3.org/1999/xhtml"))
199               . ,(or (and (stringp content)
200                           (atom-string-to-xml content))
201                      content))))
202
203 (defun atom-massage-author (author)
204   "Return an XML node representing the author. AUTHOR can be:
205 - nil, in which case `user-full-name' and `user-mail-address' are
206   used;
207 - a single string, the full name of the author;
208 - a list with two elements, the full name and the email address
209   of the author;
210 - something else, assumed to be a complete `atomPersonConstruct'."
211   (cond
212    ((null author) `(author nil 
213                            (name nil ,user-full-name)
214                            (email nil ,user-mail-address)))
215    ((stringp author) `(author nil 
216                               (name nil ,user-full-name)))
217    ((= 2 (length author)) `(author nil (name nil ,(car author))
218                                    (email nil ,(cadr author))))
219    (t `(author nil ,author))))
220
221 (require 'url-parse)
222
223 (defun atom-generate-id (link creation-date)
224   "Generate a string suitable for use as an atom:id element. This
225 implements Mark Pilgrom's tag: URI method, using the
226 CREATION-DATE of the entry, and the domain part of LINK"
227     (format "tag:%s,%s:/%s"
228             (url-host (url-generic-parse-url link))
229             (format-time-string "%Y-%m-%d" creation-date)
230             (format-time-string "%Y%m%d%H%M%S" creation-date)))
231
232 (provide 'atom)
233 ;;; atom.el ends here