Release 7.01e
[org-mode/org-tableheadings.git] / lisp / org-feed.el
blobc31df55541ea108e96efc3e1743d40787340b93f
1 ;;; org-feed.el --- Add RSS feed items to Org files
2 ;;
3 ;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
4 ;;
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
8 ;; Version: 7.01e
9 ;;
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
26 ;;; Commentary:
28 ;; This module allows to create and change entries in an Org-mode
29 ;; file triggered by items in an RSS feed. The basic functionality is
30 ;; geared toward simply adding new items found in a feed as outline nodes
31 ;; to an Org file. Using hooks, arbitrary actions can be triggered for
32 ;; new or changed items.
34 ;; Selecting feeds and target locations
35 ;; ------------------------------------
37 ;; This module is configured through a single variable, `org-feed-alist'.
38 ;; Here is an example, using a notes/tasks feed from reQall.com.
40 ;; (setq org-feed-alist
41 ;; '(("ReQall"
42 ;; "http://www.reqall.com/user/feeds/rss/a1b2c3....."
43 ;; "~/org/feeds.org" "ReQall Entries")
45 ;; With this setup, the command `M-x org-feed-update-all' will
46 ;; collect new entries in the feed at the given URL and create
47 ;; entries as subheadings under the "ReQall Entries" heading in the
48 ;; file "~/org/feeds.org". Each feed should normally have its own
49 ;; heading - however see the `:drawer' parameter.
51 ;; Besides these standard elements that need to be specified for each
52 ;; feed, keyword-value pairs can set additional options. For example,
53 ;; to de-select transitional entries with a title containing
55 ;; "reQall is typing what you said",
57 ;; you could use the `:filter' argument:
59 ;; (setq org-feed-alist
60 ;; '(("ReQall"
61 ;; "http://www.reqall.com/user/feeds/rss/a1b2c3....."
62 ;; "~/org/feeds.org" "ReQall Entries"
63 ;; :filter my-reqall-filter)))
65 ;; (defun my-reqall-filter (e)
66 ;; (if (string-match "reQall is typing what you said"
67 ;; (plist-get e :title))
68 ;; nil
69 ;; e))
71 ;; See the docstring for `org-feed-alist' for more details.
74 ;; Keeping track of previously added entries
75 ;; -----------------------------------------
77 ;; Since Org allows you to delete, archive, or move outline nodes,
78 ;; org-feed.el needs to keep track of which feed items have been handled
79 ;; before, so that they will not be handled again. For this, org-feed.el
80 ;; stores information in a special drawer, FEEDSTATUS, under the heading
81 ;; that received the input of the feed. You should add FEEDSTATUS
82 ;; to your list of drawers in the files that receive feed input:
84 ;; #+DRAWERS: PROPERTIES LOGBOOK FEEDSTATUS
86 ;; Acknowledgments
87 ;; ---------------
89 ;; org-feed.el is based on ideas by Brad Bozarth who implemented a
90 ;; similar mechanism using shell and awk scripts.
92 ;;; Code:
94 (require 'org)
95 (require 'sha1)
97 (declare-function url-retrieve-synchronously "url" (url))
98 (declare-function xml-node-children "xml" (node))
99 (declare-function xml-get-children "xml" (node child-name))
100 (declare-function xml-get-attribute "xml" (node attribute))
101 (declare-function xml-get-attribute-or-nil "xml" (node attribute))
102 (defvar xml-entity-alist)
104 (defgroup org-feed nil
105 "Options concerning RSS feeds as inputs for Org files."
106 :tag "Org ID"
107 :group 'org)
109 (defcustom org-feed-alist nil
110 "Alist specifying RSS feeds that should create inputs for Org.
111 Each entry in this list specified an RSS feed tat should be queried
112 to create inbox items in Org. Each entry is a list with the following items:
114 name a custom name for this feed
115 URL the Feed URL
116 file the target Org file where entries should be listed
117 headline the headline under which entries should be listed
119 Additional arguments can be given using keyword-value pairs. Many of these
120 specify functions that receive one or a list of \"entries\" as their single
121 argument. An entry is a property list that describes a feed item. The
122 property list has properties for each field in the item, for example `:title'
123 for the `<title>' field and `:pubDate' for the publication date. In addition,
124 it contains the following properties:
126 `:item-full-text' the full text in the <item> tag
127 `:guid-permalink' t when the guid property is a permalink
129 Here are the keyword-value pair allows in `org-feed-alist'.
131 :drawer drawer-name
132 The name of the drawer for storing feed information. The default is
133 \"FEEDSTATUS\". Using different drawers for different feeds allows
134 several feeds to target the same inbox heading.
136 :filter filter-function
137 A function to select interesting entries in the feed. It gets a single
138 entry as parameter. It should return the entry if it is relevant, or
139 nil if it is not.
141 :template template-string
142 The default action on new items in the feed is to add them as children
143 under the headline for the feed. The template describes how the entry
144 should be formatted. If not given, it defaults to
145 `org-feed-default-template'.
147 :formatter formatter-function
148 Instead of relying on a template, you may specify a function to format
149 the outline node to be inserted as a child. This function gets passed
150 a property list describing a single feed item, and it should return a
151 string that is a properly formatted Org outline node of level 1.
153 :new-handler function
154 If adding new items as children to the outline is not what you want
155 to do with new items, define a handler function that is called with
156 a list of all new items in the feed, each one represented as a property
157 list. The handler should do what needs to be done, and org-feed will
158 mark all items given to this handler as \"handled\", i.e. they will not
159 be passed to this handler again in future readings of the feed.
160 When the handler is called, point will be at the feed headline.
162 :changed-handler function
163 This function gets passed a list of all entries that have been
164 handled before, but are now still in the feed and have *changed*
165 since last handled (as evidenced by a different sha1 hash).
166 When the handler is called, point will be at the feed headline.
168 :parse-feed function
169 This function gets passed a buffer, and should return a list
170 of entries, each being a property list containing the
171 `:guid' and `:item-full-text' keys. The default is
172 `org-feed-parse-rss-feed'; `org-feed-parse-atom-feed' is an
173 alternative.
175 :parse-entry function
176 This function gets passed an entry as returned by the parse-feed
177 function, and should return the entry with interesting properties added.
178 The default is `org-feed-parse-rss-entry'; `org-feed-parse-atom-entry'
179 is an alternative."
180 :group 'org-feed
181 :type '(repeat
182 (list :value ("" "http://" "" "")
183 (string :tag "Name")
184 (string :tag "Feed URL")
185 (file :tag "File for inbox")
186 (string :tag "Headline for inbox")
187 (repeat :inline t
188 (choice
189 (list :inline t :tag "Filter"
190 (const :filter)
191 (symbol :tag "Filter Function"))
192 (list :inline t :tag "Template"
193 (const :template)
194 (string :tag "Template"))
195 (list :inline t :tag "Formatter"
196 (const :formatter)
197 (symbol :tag "Formatter Function"))
198 (list :inline t :tag "New items handler"
199 (const :new-handler)
200 (symbol :tag "Handler Function"))
201 (list :inline t :tag "Changed items"
202 (const :changed-handler)
203 (symbol :tag "Handler Function"))
204 (list :inline t :tag "Parse Feed"
205 (const :parse-feed)
206 (symbol :tag "Parse Feed Function"))
207 (list :inline t :tag "Parse Entry"
208 (const :parse-entry)
209 (symbol :tag "Parse Entry Function"))
210 )))))
212 (defcustom org-feed-drawer "FEEDSTATUS"
213 "The name of the drawer for feed status information.
214 Each feed may also specify its own drawer name using the `:drawer'
215 parameter in `org-feed-alist'.
216 Note that in order to make these drawers behave like drawers, they must
217 be added to the variable `org-drawers' or configured with a #+DRAWERS
218 line."
219 :group 'org-feed
220 :type '(string :tag "Drawer Name"))
222 (defcustom org-feed-default-template "\n* %h\n %U\n %description\n %a\n"
223 "Template for the Org node created from RSS feed items.
224 This is just the default, each feed can specify its own.
225 Any fields from the feed item can be interpolated into the template with
226 %name, for example %title, %description, %pubDate etc. In addition, the
227 following special escapes are valid as well:
229 %h the title, or the first line of the description
230 %t the date as a stamp, either from <pubDate> (if present), or
231 the current date.
232 %T date and time
233 %u,%U like %t,%T, but inactive time stamps
234 %a A link, from <guid> if that is a permalink, else from <link>"
235 :group 'org-feed
236 :type '(string :tag "Template"))
238 (defcustom org-feed-save-after-adding t
239 "Non-nil means save buffer after adding new feed items."
240 :group 'org-feed
241 :type 'boolean)
243 (defcustom org-feed-retrieve-method 'url-retrieve-synchronously
244 "The method to be used to retrieve a feed URL.
245 This can be `curl' or `wget' to call these external programs, or it can be
246 an Emacs Lisp function that will return a buffer containing the content
247 of the file pointed to by the URL."
248 :group 'org-feed
249 :type '(choice
250 (const :tag "Internally with url.el" url-retrieve-synchronously)
251 (const :tag "Externally with curl" curl)
252 (const :tag "Externally with wget" wget)
253 (function :tag "Function")))
255 (defcustom org-feed-before-adding-hook nil
256 "Hook that is run before adding new feed items to a file.
257 You might want to commit the file in its current state to version control,
258 for example."
259 :group 'org-feed
260 :type 'hook)
262 (defcustom org-feed-after-adding-hook nil
263 "Hook that is run after new items have been added to a file.
264 Depending on `org-feed-save-after-adding', the buffer will already
265 have been saved."
266 :group 'org-feed
267 :type 'hook)
269 (defvar org-feed-buffer "*Org feed*"
270 "The buffer used to retrieve a feed.")
272 (defun org-feed-unescape (s)
273 "Unescape protected entities in S."
274 (require 'xml)
275 (let ((re (concat "&\\("
276 (mapconcat 'car xml-entity-alist "\\|")
277 "\\);")))
278 (while (string-match re s)
279 (setq s (replace-match
280 (cdr (assoc (match-string 1 s) xml-entity-alist)) nil nil s)))
283 ;;;###autoload
284 (defun org-feed-update-all ()
285 "Get inbox items from all feeds in `org-feed-alist'."
286 (interactive)
287 (let ((nfeeds (length org-feed-alist))
288 (nnew (apply '+ (mapcar 'org-feed-update org-feed-alist))))
289 (message "%s from %d %s"
290 (cond ((= nnew 0) "No new entries")
291 ((= nnew 1) "1 new entry")
292 (t (format "%d new entries" nnew)))
293 nfeeds
294 (if (= nfeeds 1) "feed" "feeds"))))
296 ;;;###autoload
297 (defun org-feed-update (feed &optional retrieve-only)
298 "Get inbox items from FEED.
299 FEED can be a string with an association in `org-feed-alist', or
300 it can be a list structured like an entry in `org-feed-alist'."
301 (interactive (list (org-completing-read "Feed name: " org-feed-alist)))
302 (if (stringp feed) (setq feed (assoc feed org-feed-alist)))
303 (unless feed
304 (error "No such feed in `org-feed-alist"))
305 (catch 'exit
306 (let ((name (car feed))
307 (url (nth 1 feed))
308 (file (nth 2 feed))
309 (headline (nth 3 feed))
310 (filter (nth 1 (memq :filter feed)))
311 (formatter (nth 1 (memq :formatter feed)))
312 (new-handler (nth 1 (memq :new-handler feed)))
313 (changed-handler (nth 1 (memq :changed-handler feed)))
314 (template (or (nth 1 (memq :template feed))
315 org-feed-default-template))
316 (drawer (or (nth 1 (memq :drawer feed))
317 org-feed-drawer))
318 (parse-feed (or (nth 1 (memq :parse-feed feed))
319 'org-feed-parse-rss-feed))
320 (parse-entry (or (nth 1 (memq :parse-entry feed))
321 'org-feed-parse-rss-entry))
322 feed-buffer inbox-pos new-formatted
323 entries old-status status new changed guid-alist e guid olds)
324 (setq feed-buffer (org-feed-get-feed url))
325 (unless (and feed-buffer (bufferp (get-buffer feed-buffer)))
326 (error "Cannot get feed %s" name))
327 (when retrieve-only
328 (throw 'exit feed-buffer))
329 (setq entries (funcall parse-feed feed-buffer))
330 (ignore-errors (kill-buffer feed-buffer))
331 (save-excursion
332 (save-window-excursion
333 (setq inbox-pos (org-feed-goto-inbox-internal file headline))
334 (setq old-status (org-feed-read-previous-status inbox-pos drawer))
335 ;; Add the "handled" status to the appropriate entries
336 (setq entries (mapcar (lambda (e)
337 (setq e
338 (plist-put e :handled
339 (nth 1 (assoc
340 (plist-get e :guid)
341 old-status)))))
342 entries))
343 ;; Find out which entries are new and which are changed
344 (dolist (e entries)
345 (if (not (plist-get e :handled))
346 (push e new)
347 (setq olds (nth 2 (assoc (plist-get e :guid) old-status)))
348 (if (and olds
349 (not (string= (sha1
350 (plist-get e :item-full-text))
351 olds)))
352 (push e changed))))
354 ;; Parse the relevant entries fully
355 (setq new (mapcar parse-entry new)
356 changed (mapcar parse-entry changed))
358 ;; Run the filter
359 (when filter
360 (setq new (delq nil (mapcar filter new))
361 changed (delq nil (mapcar filter new))))
363 (when (not (or new changed))
364 (message "No new items in feed %s" name)
365 (throw 'exit 0))
367 ;; Get alist based on guid, to look up entries
368 (setq guid-alist
369 (append
370 (mapcar (lambda (e) (list (plist-get e :guid) e)) new)
371 (mapcar (lambda (e) (list (plist-get e :guid) e)) changed)))
373 ;; Construct the new status
374 (setq status
375 (mapcar
376 (lambda (e)
377 (setq guid (plist-get e :guid))
378 (list guid
379 ;; things count as handled if we handle them now,
380 ;; or if they were handled previously
381 (if (assoc guid guid-alist) t (plist-get e :handled))
382 ;; A hash, to detect changes
383 (sha1 (plist-get e :item-full-text))))
384 entries))
386 ;; Handle new items in the feed
387 (when new
388 (if new-handler
389 (progn
390 (goto-char inbox-pos)
391 (funcall new-handler new))
392 ;; No custom handler, do the default adding
393 ;; Format the new entries into an alist with GUIDs in the car
394 (setq new-formatted
395 (mapcar
396 (lambda (e) (org-feed-format-entry e template formatter))
397 new)))
399 ;; Insert the new items
400 (org-feed-add-items inbox-pos new-formatted))
402 ;; Handle changed items in the feed
403 (when (and changed-handler changed)
404 (goto-char inbox-pos)
405 (funcall changed-handler changed))
407 ;; Write the new status
408 ;; We do this only now, in case something goes wrong above, so
409 ;; that would would end up with a status that does not reflect
410 ;; which items truely have been handled
411 (org-feed-write-status inbox-pos drawer status)
413 ;; Normalize the visibility of the inbox tree
414 (goto-char inbox-pos)
415 (hide-subtree)
416 (show-children)
417 (org-cycle-hide-drawers 'children)
419 ;; Hooks and messages
420 (when org-feed-save-after-adding (save-buffer))
421 (message "Added %d new item%s from feed %s to file %s, heading %s"
422 (length new) (if (> (length new) 1) "s" "")
423 name
424 (file-name-nondirectory file) headline)
425 (run-hooks 'org-feed-after-adding-hook)
426 (length new))))))
428 ;;;###autoload
429 (defun org-feed-goto-inbox (feed)
430 "Go to the inbox that captures the feed named FEED."
431 (interactive
432 (list (if (= (length org-feed-alist) 1)
433 (car org-feed-alist)
434 (org-completing-read "Feed name: " org-feed-alist))))
435 (if (stringp feed) (setq feed (assoc feed org-feed-alist)))
436 (unless feed
437 (error "No such feed in `org-feed-alist"))
438 (org-feed-goto-inbox-internal (nth 2 feed) (nth 3 feed)))
440 ;;;###autoload
441 (defun org-feed-show-raw-feed (feed)
442 "Show the raw feed buffer of a feed."
443 (interactive
444 (list (if (= (length org-feed-alist) 1)
445 (car org-feed-alist)
446 (org-completing-read "Feed name: " org-feed-alist))))
447 (if (stringp feed) (setq feed (assoc feed org-feed-alist)))
448 (unless feed
449 (error "No such feed in `org-feed-alist"))
450 (switch-to-buffer
451 (org-feed-update feed 'retrieve-only))
452 (goto-char (point-min)))
454 (defun org-feed-goto-inbox-internal (file heading)
455 "Find or create HEADING in FILE.
456 Switch to that buffer, and return the position of that headline."
457 (find-file file)
458 (widen)
459 (goto-char (point-min))
460 (if (re-search-forward
461 (concat "^\\*+[ \t]+" heading "[ \t]*\\(:.*?:[ \t]*\\)?$")
462 nil t)
463 (goto-char (match-beginning 0))
464 (goto-char (point-max))
465 (insert "\n\n* " heading "\n\n")
466 (org-back-to-heading t))
467 (point))
469 (defun org-feed-read-previous-status (pos drawer)
470 "Get the alist of old GUIDs from the entry at POS.
471 This will find DRAWER and extract the alist."
472 (save-excursion
473 (goto-char pos)
474 (let ((end (save-excursion (org-end-of-subtree t t))))
475 (if (re-search-forward
476 (concat "^[ \t]*:" drawer ":[ \t]*\n\\([^\000]*?\\)\n[ \t]*:END:")
477 end t)
478 (read (match-string 1))
479 nil))))
481 (defun org-feed-write-status (pos drawer status)
482 "Write the feed STATUS to DRAWER in entry at POS."
483 (save-excursion
484 (goto-char pos)
485 (let ((end (save-excursion (org-end-of-subtree t t)))
486 guid)
487 (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*\n")
488 end t)
489 (progn
490 (goto-char (match-end 0))
491 (delete-region (point)
492 (save-excursion
493 (and (re-search-forward "^[ \t]*:END:" nil t)
494 (match-beginning 0)))))
495 (outline-next-heading)
496 (insert " :" drawer ":\n :END:\n")
497 (beginning-of-line 0))
498 (insert (pp-to-string status)))))
500 (defun org-feed-add-items (pos entries)
501 "Add the formatted items to the headline as POS."
502 (let (entry level)
503 (save-excursion
504 (goto-char pos)
505 (unless (looking-at org-complex-heading-regexp)
506 (error "Wrong position"))
507 (setq level (org-get-valid-level (length (match-string 1)) 1))
508 (org-end-of-subtree t t)
509 (skip-chars-backward " \t\n")
510 (beginning-of-line 2)
511 (setq pos (point))
512 (while (setq entry (pop entries))
513 (org-paste-subtree level entry 'yank))
514 (org-mark-ring-push pos))))
516 (defun org-feed-format-entry (entry template formatter)
517 "Format ENTRY so that it can be inserted into an Org file.
518 ENTRY is a property list. This function adds a `:formatted-for-org' property
519 and returns the full property list.
520 If that property is already present, nothing changes."
521 (if formatter
522 (funcall formatter entry)
523 (let (dlines fmt tmp indent time name
524 v-h v-t v-T v-u v-U v-a)
525 (setq dlines (org-split-string (or (plist-get entry :description) "???")
526 "\n")
527 v-h (or (plist-get entry :title) (car dlines) "???")
528 time (or (if (plist-get entry :pubDate)
529 (org-read-date t t (plist-get entry :pubDate)))
530 (current-time))
531 v-t (format-time-string (org-time-stamp-format nil nil) time)
532 v-T (format-time-string (org-time-stamp-format t nil) time)
533 v-u (format-time-string (org-time-stamp-format nil t) time)
534 v-U (format-time-string (org-time-stamp-format t t) time)
535 v-a (if (setq tmp (or (and (plist-get entry :guid-permalink)
536 (plist-get entry :guid))
537 (plist-get entry :link)))
538 (concat "[[" tmp "]]\n")
539 ""))
540 (with-temp-buffer
541 (insert template)
542 (goto-char (point-min))
543 (while (re-search-forward "%\\([a-zA-Z]+\\)" nil t)
544 (setq name (match-string 1))
545 (cond
546 ((member name '("h" "t" "T" "u" "U" "a"))
547 (replace-match (symbol-value (intern (concat "v-" name))) t t))
548 ((setq tmp (plist-get entry (intern (concat ":" name))))
549 (save-excursion
550 (save-match-data
551 (beginning-of-line 1)
552 (when (looking-at (concat "^\\([ \t]*\\)%" name "[ \t]*$"))
553 (setq tmp (org-feed-make-indented-block
554 tmp (org-get-indentation))))))
555 (replace-match tmp t t))))
556 (buffer-string)))))
558 (defun org-feed-make-indented-block (s n)
559 "Add indentation of N spaces to a multiline string S."
560 (if (not (string-match "\n" s))
562 (mapconcat 'identity
563 (org-split-string s "\n")
564 (concat "\n" (make-string n ?\ )))))
566 (defun org-feed-skip-http-headers (buffer)
567 "Remove HTTP headers from BUFFER, and return it.
568 Assumes headers are indeed present!"
569 (with-current-buffer buffer
570 (widen)
571 (goto-char (point-min))
572 (search-forward "\n\n")
573 (delete-region (point-min) (point))
574 buffer))
576 (defun org-feed-get-feed (url)
577 "Get the RSS feed file at URL and return the buffer."
578 (cond
579 ((eq org-feed-retrieve-method 'url-retrieve-synchronously)
580 (org-feed-skip-http-headers (url-retrieve-synchronously url)))
581 ((eq org-feed-retrieve-method 'curl)
582 (ignore-errors (kill-buffer org-feed-buffer))
583 (call-process "curl" nil org-feed-buffer nil "--silent" url)
584 org-feed-buffer)
585 ((eq org-feed-retrieve-method 'wget)
586 (ignore-errors (kill-buffer org-feed-buffer))
587 (call-process "wget" nil org-feed-buffer nil "-q" "-O" "-" url)
588 org-feed-buffer)
589 ((functionp org-feed-retrieve-method)
590 (funcall org-feed-retrieve-method url))))
592 (defun org-feed-parse-rss-feed (buffer)
593 "Parse BUFFER for RSS feed entries.
594 Returns a list of entries, with each entry a property list,
595 containing the properties `:guid' and `:item-full-text'."
596 (let ((case-fold-search t)
597 entries beg end item guid entry)
598 (with-current-buffer buffer
599 (widen)
600 (goto-char (point-min))
601 (while (re-search-forward "<item\\>.*?>" nil t)
602 (setq beg (point)
603 end (and (re-search-forward "</item>" nil t)
604 (match-beginning 0)))
605 (setq item (buffer-substring beg end)
606 guid (if (string-match "<guid\\>.*?>\\(.*?\\)</guid>" item)
607 (org-match-string-no-properties 1 item)))
608 (setq entry (list :guid guid :item-full-text item))
609 (push entry entries)
610 (widen)
611 (goto-char end))
612 (nreverse entries))))
614 (defun org-feed-parse-rss-entry (entry)
615 "Parse the `:item-full-text' field for xml tags and create new properties."
616 (with-temp-buffer
617 (insert (plist-get entry :item-full-text))
618 (goto-char (point-min))
619 (while (re-search-forward "<\\([a-zA-Z]+\\>\\).*?>\\([^\000]*?\\)</\\1>"
620 nil t)
621 (setq entry (plist-put entry
622 (intern (concat ":" (match-string 1)))
623 (org-feed-unescape (match-string 2)))))
624 (goto-char (point-min))
625 (unless (re-search-forward "isPermaLink[ \t]*=[ \t]*\"false\"" nil t)
626 (setq entry (plist-put entry :guid-permalink t))))
627 entry)
629 (defun org-feed-parse-atom-feed (buffer)
630 "Parse BUFFER for Atom feed entries.
631 Returns a list of entries, with each entry a property list,
632 containing the properties `:guid' and `:item-full-text'.
634 The `:item-full-text' property actually contains the sexp
635 formatted as a string, not the original XML data."
636 (require 'xml)
637 (with-current-buffer buffer
638 (widen)
639 (let ((feed (car (xml-parse-region (point-min) (point-max)))))
640 (mapcar
641 (lambda (entry)
642 (list
643 :guid (car (xml-node-children (car (xml-get-children entry 'id))))
644 :item-full-text (prin1-to-string entry)))
645 (xml-get-children feed 'entry)))))
647 (defun org-feed-parse-atom-entry (entry)
648 "Parse the `:item-full-text' as a sexp and create new properties."
649 (let ((xml (car (read-from-string (plist-get entry :item-full-text)))))
650 ;; Get first <link href='foo'/>.
651 (setq entry (plist-put entry :link
652 (xml-get-attribute
653 (car (xml-get-children xml 'link))
654 'href)))
655 ;; Add <title/> as :title.
656 (setq entry (plist-put entry :title
657 (org-feed-unescape
658 (car (xml-node-children
659 (car (xml-get-children xml 'title)))))))
660 (let* ((content (car (xml-get-children xml 'content)))
661 (type (xml-get-attribute-or-nil content 'type)))
662 (when content
663 (cond
664 ((string= type "text")
665 ;; We like plain text.
666 (setq entry (plist-put entry :description
667 (org-feed-unescape
668 (car (xml-node-children content))))))
669 ((string= type "html")
670 ;; TODO: convert HTML to Org markup.
671 (setq entry (plist-put entry :description
672 (org-feed-unescape
673 (car (xml-node-children content))))))
674 ((string= type "xhtml")
675 ;; TODO: convert XHTML to Org markup.
676 (setq entry (plist-put entry :description
677 (prin1-to-string
678 (xml-node-children content)))))
680 (setq entry (plist-put entry :description
681 (format "Unknown '%s' content." type)))))))
682 entry))
684 (provide 'org-feed)
686 ;; arch-tag: 0929b557-9bc4-47f4-9633-30a12dbb5ae2
687 ;;; org-feed.el ends here