]> gitweb.fperrin.net Git - atom.el.git/blob - atom.el
atom.el is a library for generating an Atom feed from a Lisp program.
[atom.el.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 file from a Lisp program.
24
25 ;;; Code:
26
27 (defun atom-create (title author link &optional updated id)
28   "Create a new atom structure.
29
30 TITLE is the title for the feed, a short, text-only, human
31 readable string.
32
33 AUTHOR is the author of the feed. See `atom-massage-author' for
34 the possible ways to specify it.
35
36 LINK is the URL of a page responible for the content of this
37 feed.
38
39 UPDATED is the date the feed was last updated. If not given,
40 `(current-time)' is used.
41
42 ID is a unique identifier for this feed. If not given, it
43 defaults to LINK."
44   `((title nil ,title)
45     (link ((href . ,link)))
46     ,(atom-massage-author author)
47     (updated nil ,(atom-format-time updated))
48     (id nil ,(or id link))))
49
50 (defun atom-push-entry (atom entry)
51   "Add the entry ENTRY to the feed ATOM."
52   (nconc atom (list `(entry nil . ,entry))))
53
54 (defun atom-modify-entry (entry name val)
55   "Set the NAME element of ENTRY to VAL."
56   (let ((elem (assoc name entry)))
57     (if elem
58         (if (stringp val)
59             (setcar (cddr elem) val)
60           (setcdr elem val))
61       (setq elem (if (stringp val)
62                      (list name nil val)
63                    (cons name val)))
64       (nconc entry (list elem)))))
65
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
69 entry.
70
71 TITLE is a short, text-only, human readable string.
72
73 LINK is a permanent link for this entry. For a given entry, LINK
74 may change between successive generations of the atom feed.
75
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.
78
79 If SUMMARY is not given, the entry will not contain any summary.
80
81 UPDATED defaults to `(current-time)' if omitted, which is
82 probably not a very good default.
83
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)
94     entry))
95
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.")
99
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."
105   (atom-add-entry atom
106    title link
107    (atom-massage-html content)
108    (and summary (atom-massage-html summary))
109    updated id))
110
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."
116   (atom-add-entry atom
117    title link
118    (atom-massage-xhtml content)
119    (and summary (atom-massage-xhtml summary))
120    updated id))
121
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")
126   (xml-print atom)
127   (insert "\n</feed>"))
128
129 \f
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
135    "\\(..\\)$"
136    ":\\1"
137    (format-time-string "%Y-%m-%dT%T%z" time)))
138
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))
143
144 (defun atom-string-to-xml (string)
145   "Convert STRING into a Lisp structure as used by `xml.el'."
146   (with-temp-buffer
147     (insert string)
148     (xml-parse-region (point-min) (point-max))))
149
150 (defun atom-massage-xhtml (content)
151   "Massage CONTENT so it can be used as an XHTML fragment in an
152 Atom feed."
153   (list '((type . "xhtml"))
154         `(div ((xmlns . "http://www.w3.org/1999/xhtml"))
155               . ,(or (and (stringp content)
156                           (atom-string-to-xml content))
157                      content))))
158
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
162   used;
163 - a single string, the full name of the author;
164 - a list with two elements, the full name and the email address
165   of the author;
166 - something else, assumed to be a complete `atomPersonConstruct'."
167   (cond
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))))
176
177 (provide 'atom)
178 ;;; atom.el ends here