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 file from a Lisp program.
27 (defun atom-create (title author link &optional updated id)
28 "Create a new atom structure.
30 TITLE is the title for the feed, a short, text-only, human
33 AUTHOR is the author of the feed. See `atom-massage-author' for
34 the possible ways to specify it.
36 LINK is the URL of a page responible for the content of this
39 UPDATED is the date the feed was last updated. If not given,
40 `(current-time)' is used.
42 ID is a unique identifier for this feed. If not given, it
45 (link ((href . ,link)))
46 ,(atom-massage-author author)
47 (updated nil ,(atom-format-time updated))
48 (id nil ,(or id link))))
50 (defun atom-push-entry (atom entry)
51 "Add the entry ENTRY to the feed ATOM."
52 (nconc atom (list `(entry nil . ,entry))))
54 (defun atom-modify-entry (entry name val)
55 "Set the NAME element of ENTRY to VAL."
56 (let ((elem (assoc name entry)))
59 (setcar (cddr elem) val)
61 (setq elem (if (stringp val)
64 (nconc entry (list elem)))))
66 (defun atom-add-entry (atom title link content
67 &optional summary updated id)
68 "Add an entry to the atom flux ATOM. Return the newly added
71 TITLE is a short, text-only, human readable string.
73 LINK is a permanent link for this entry. For a given entry, LINK
74 may change between successive generations of the atom feed.
76 CONTENT is the content of the entry; use `atom-add-html-entry'
77 or `atom-add-xhtml-entry' when CONTENT is not text-only.
79 If SUMMARY is not given, the entry will not contain any summary.
81 UPDATED defaults to `(current-time)' if omitted, which is
82 probably not a very good default.
84 ID defaults to LINK, which is not optimal; TODO give a way to
85 easily generate IDs. For a given entry, it must not change
86 between successive generations of the atom feed."
87 (let ((entry (list (list 'title nil title))))
88 (atom-modify-entry entry 'link (list (list (cons 'href link))))
89 (atom-modify-entry entry 'id (or id link))
90 (atom-modify-entry entry 'updated (atom-format-time updated))
91 (if summary (atom-modify-entry entry 'summary summary))
92 (atom-modify-entry entry 'content content)
93 (atom-push-entry atom entry)
96 (defalias 'atom-add-text-entry 'atom-add-entry
97 "Add an entry to ATOM, with a textual content. See
98 `atom-add-entry' for details.")
100 (defun atom-add-html-entry (atom title link content
101 &optional summary updated id)
102 "Add an entry to ATOM, with some HTML content. CONTENT should
103 be a string enconding a valid HTML fragment. See `atom-add-entry'
104 for additional details."
107 (atom-massage-html content)
108 (and summary (atom-massage-html summary))
111 (defun atom-add-xhtml-entry (atom title link content
112 &optional summary updated id)
113 "Add an entry to ATOM, with some XHTML content. CONTENT may be
114 given either as a string, or as an XML tree, of a valid XHTML
115 fragment. See `atom-add-entry' for additional details."
118 (atom-massage-xhtml content)
119 (and summary (atom-massage-xhtml summary))
122 (defun atom-print (atom)
123 "Print the Atom feed ATOM in the current buffer."
124 (insert "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n")
125 (insert "<feed xmlns=\"http://www.w3.org/2005/Atom\">\n")
127 (insert "\n</feed>"))
130 (defun atom-format-time (&optional time)
131 "Format a time according to RFC3339."
132 ;; The time zone must be specified in numeric form, but with a colon between
133 ;; the hour and minute parts.
134 (replace-regexp-in-string
137 (format-time-string "%Y-%m-%dT%T%z" time)))
139 (defun atom-massage-html (content)
140 "Massage CONTENT so it can be used as an HTML fragment in an
141 Atom feed. CONTENT must be a string."
142 (list '((type . "html")) content))
144 (defun atom-string-to-xml (string)
145 "Convert STRING into a Lisp structure as used by `xml.el'."
148 (xml-parse-region (point-min) (point-max))))
150 (defun atom-massage-xhtml (content)
151 "Massage CONTENT so it can be used as an XHTML fragment in an
153 (list '((type . "xhtml"))
154 `(div ((xmlns . "http://www.w3.org/1999/xhtml"))
155 . ,(or (and (stringp content)
156 (atom-string-to-xml content))
159 (defun atom-massage-author (author)
160 "Return an XML node representing the author. AUTHOR can be:
161 - nil, in which case `user-full-name' and `user-mail-address' are
163 - a single string, the full name of the author;
164 - a list with two elements, the full name and the email address
166 - something else, assumed to be a complete `atomPersonConstruct'."
168 ((null author) `(author nil
169 (name nil ,user-full-name)
170 (email nil ,user-mail-address)))
171 ((stringp author) `(author nil
172 (name nil ,user-full-name)))
173 ((= 2 (length author)) `(author nil (name nil ,(car author))
174 (email nil ,(cadr author))))
175 (t `(author nil ,author))))
178 ;;; atom.el ends here