1 ;;; atom.el --- Create an Atom feed
3 ;; Copyright (C) 2011 Frédéric Perrin
5 ;; Author: Frédéric Perrin <frederic.perrin@resel.fr>
6 ;; Keywords: www, hypermedia, atom, rss
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.
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.
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/>.
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.
30 ;; A feed is really a Lisp structure as used by the `xml.el' package,
31 ;; without the parent `feed' element.
33 ;; A typical usage would look like this:
35 ;; (let ((my-atom-feed (atom-create "My feed" "http://example.org")))
36 ;; ; A simple, text-only entry
37 ;; (atom-add-text-entry
40 ;; "http://example.org/hello"
41 ;; "Hello the world!")
43 ;; ; A text-only entry, with all the optional pieces of data
44 ;; (atom-add-text-entry
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
58 ;; (atom-add-xhtml-entry
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))
70 (defun atom-create (title link &optional author updated id)
71 "Create a new atom structure.
73 TITLE is the title for the feed, a short, text-only, human
76 AUTHOR is the author of the feed. See `atom-massage-author' for
77 the possible ways to specify it.
79 LINK is the URL of a page responible for the content of this
82 UPDATED is the date the feed was last updated. If not given,
83 `(current-time)' is used.
85 ID is a unique identifier for this feed. If not given, it
88 (link ((href . ,link)))
89 ,(atom-massage-author author)
90 (updated nil ,(atom-format-time updated))
91 (id nil ,(or id link))))
93 (defun atom-push-entry (atom entry)
94 "Add the entry ENTRY to the feed ATOM."
95 (nconc atom (list `(entry nil . ,entry))))
97 (defun atom-modify-entry (entry name val)
98 "Set the NAME element of ENTRY to VAL."
99 (let ((elem (assoc name entry)))
102 (setcar (cddr elem) val)
104 (setq elem (if (stringp val)
107 (nconc entry (list elem)))))
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
114 TITLE is a short, text-only, human readable string.
116 LINK is a permanent link for this entry. For a given entry, LINK
117 may change between successive generations of the atom feed.
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.
122 If SUMMARY is not given, the entry will not contain any summary.
124 UPDATED defaults to `(current-time)' if omitted, which is
125 probably not a very good default.
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)
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.")
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."
151 (atom-massage-html content)
152 (and summary (atom-massage-html summary))
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."
162 (atom-massage-xhtml content)
163 (and summary (atom-massage-xhtml summary))
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")
171 (insert "\n</feed>"))
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
181 (format-time-string "%Y-%m-%dT%T%z" time)))
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))
188 (defun atom-string-to-xml (string)
189 "Convert STRING into a Lisp structure as used by `xml.el'."
192 (xml-parse-region (point-min) (point-max))))
194 (defun atom-massage-xhtml (content)
195 "Massage CONTENT so it can be used as an XHTML fragment in an
197 (list '((type . "xhtml"))
198 `(div ((xmlns . "http://www.w3.org/1999/xhtml"))
199 . ,(or (and (stringp content)
200 (atom-string-to-xml content))
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
207 - a single string, the full name of the author;
208 - a list with two elements, the full name and the email address
210 - something else, assumed to be a complete `atomPersonConstruct'."
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))))
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)))
233 ;;; atom.el ends here