Merge branch 't/patch59'
[org-mode.git] / lisp / org-feed.el
blob3edcf1a219735fbfdb102cdd26cd2cd7e5b748eb
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: 6.36trans
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 ;; Acknowledgements
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 of entries,
170 each being a property list containing the `:guid' and `:item-full-text'
171 keys. The default is `org-feed-parse-rss-feed'; `org-feed-parse-atom-feed'
172 is an alternative.
174 :parse-entry function
175 This function gets passed an entry as returned by the parse-feed
176 function, and should return the entry with interesting properties added.
177 The default is `org-feed-parse-rss-entry'; `org-feed-parse-atom-entry'
178 is an alternative."
179 :group 'org-feed
180 :type '(repeat
181 (list :value ("" "http://" "" "")
182 (string :tag "Name")
183 (string :tag "Feed URL")
184 (file :tag "File for inbox")
185 (string :tag "Headline for inbox")
186 (repeat :inline t
187 (choice
188 (list :inline t :tag "Filter"
189 (const :filter)
190 (symbol :tag "Filter Function"))
191 (list :inline t :tag "Template"
192 (const :template)
193 (string :tag "Template"))
194 (list :inline t :tag "Formatter"
195 (const :formatter)
196 (symbol :tag "Formatter Function"))
197 (list :inline t :tag "New items handler"
198 (const :new-handler)
199 (symbol :tag "Handler Function"))
200 (list :inline t :tag "Changed items"
201 (const :changed-handler)
202 (symbol :tag "Handler Function"))
203 (list :inline t :tag "Parse Feed"
204 (const :parse-feed)
205 (symbol :tag "Parse Feed Function"))
206 (list :inline t :tag "Parse Entry"
207 (const :parse-entry)
208 (symbol :tag "Parse Entry Function"))
209 )))))
211 (defcustom org-feed-drawer "FEEDSTATUS"
212 "The name of the drawer for feed status information.
213 Each feed may also specify its own drawer name using the `:drawer'
214 parameter in `org-feed-alist'.
215 Note that in order to make these drawers behave like drawers, they must
216 be added to the variable `org-drawers' or configured with a #+DRAWERS
217 line."
218 :group 'org-feed
219 :type '(string :tag "Drawer Name"))
221 (defcustom org-feed-default-template "\n* %h\n %U\n %description\n %a\n"
222 "Template for the Org node created from RSS feed items.
223 This is just the default, each feed can specify its own.
224 Any fields from the feed item can be interpolated into the template with
225 %name, for example %title, %description, %pubDate etc. In addition, the
226 following special escapes are valid as well:
228 %h the title, or the first line of the description
229 %t the date as a stamp, either from <pubDate> (if present), or
230 the current date.
231 %T date and time
232 %u,%U like %t,%T, but inactive time stamps
233 %a A link, from <guid> if that is a permalink, else from <link>"
234 :group 'org-feed
235 :type '(string :tag "Template"))
237 (defcustom org-feed-save-after-adding t
238 "Non-nil means save buffer after adding new feed items."
239 :group 'org-feed
240 :type 'boolean)
242 (defcustom org-feed-retrieve-method 'url-retrieve-synchronously
243 "The method to be used to retrieve a feed URL.
244 This can be `curl' or `wget' to call these external programs, or it can be
245 an Emacs Lisp function that will return a buffer containing the content
246 of the file pointed to by the URL."
247 :group 'org-feed
248 :type '(choice
249 (const :tag "Internally with url.el" url-retrieve-synchronously)
250 (const :tag "Externally with curl" curl)
251 (const :tag "Externally with wget" wget)
252 (function :tag "Function")))
254 (defcustom org-feed-before-adding-hook nil
255 "Hook that is run before adding new feed items to a file.
256 You might want to commit the file in its current state to version control,
257 for example."
258 :group 'org-feed
259 :type 'hook)
261 (defcustom org-feed-after-adding-hook nil
262 "Hook that is run after new items have been added to a file.
263 Depending on `org-feed-save-after-adding', the buffer will already
264 have been saved."
265 :group 'org-feed
266 :type 'hook)
268 (defvar org-feed-buffer "*Org feed*"
269 "The buffer used to retrieve a feed.")
271 (defun org-feed-unescape (s)
272 "Unescape protected entities in S."
273 (let ((re (concat "&\\("
274 (mapconcat (lambda (e)
275 (car e)) xml-entity-alist "\\|")
276 "\\);")))
277 (while (string-match re s)
278 (setq s (replace-match
279 (cdr (assoc (match-string 1 s) xml-entity-alist)) nil nil s)))
282 ;;;###autoload
283 (defun org-feed-update-all ()
284 "Get inbox items from all feeds in `org-feed-alist'."
285 (interactive)
286 (let ((nfeeds (length org-feed-alist))
287 (nnew (apply '+ (mapcar 'org-feed-update org-feed-alist))))
288 (message "%s from %d %s"
289 (cond ((= nnew 0) "No new entries")
290 ((= nnew 1) "1 new entry")
291 (t (format "%d new entries" nnew)))
292 nfeeds
293 (if (= nfeeds 1) "feed" "feeds"))))
295 ;;;###autoload
296 (defun org-feed-update (feed &optional retrieve-only)
297 "Get inbox items from FEED.
298 FEED can be a string with an association in `org-feed-alist', or
299 it can be a list structured like an entry in `org-feed-alist'."
300 (interactive (list (org-completing-read "Feed name: " org-feed-alist)))
301 (if (stringp feed) (setq feed (assoc feed org-feed-alist)))
302 (unless feed
303 (error "No such feed in `org-feed-alist"))
304 (catch 'exit
305 (let ((name (car feed))
306 (url (nth 1 feed))
307 (file (nth 2 feed))
308 (headline (nth 3 feed))
309 (filter (nth 1 (memq :filter feed)))
310 (formatter (nth 1 (memq :formatter feed)))
311 (new-handler (nth 1 (memq :new-handler feed)))
312 (changed-handler (nth 1 (memq :changed-handler feed)))
313 (template (or (nth 1 (memq :template feed))
314 org-feed-default-template))
315 (drawer (or (nth 1 (memq :drawer feed))
316 org-feed-drawer))
317 (parse-feed (or (nth 1 (memq :parse-feed feed))
318 'org-feed-parse-rss-feed))
319 (parse-entry (or (nth 1 (memq :parse-entry feed))
320 'org-feed-parse-rss-entry))
321 feed-buffer inbox-pos new-formatted
322 entries old-status status new changed guid-alist e guid olds)
323 (setq feed-buffer (org-feed-get-feed url))
324 (unless (and feed-buffer (bufferp (get-buffer feed-buffer)))
325 (error "Cannot get feed %s" name))
326 (when retrieve-only
327 (throw 'exit feed-buffer))
328 (setq entries (funcall parse-feed feed-buffer))
329 (ignore-errors (kill-buffer feed-buffer))
330 (save-excursion
331 (save-window-excursion
332 (setq inbox-pos (org-feed-goto-inbox-internal file headline))
333 (setq old-status (org-feed-read-previous-status inbox-pos drawer))
334 ;; Add the "handled" status to the appropriate entries
335 (setq entries (mapcar (lambda (e)
336 (setq e (plist-put e :handled
337 (nth 1 (assoc
338 (plist-get e :guid)
339 old-status)))))
340 entries))
341 ;; Find out which entries are new and which are changed
342 (dolist (e entries)
343 (if (not (plist-get e :handled))
344 (push e new)
345 (setq olds (nth 2 (assoc (plist-get e :guid) old-status)))
346 (if (and olds
347 (not (string= (sha1
348 (plist-get e :item-full-text))
349 olds)))
350 (push e changed))))
352 ;; Parse the relevant entries fully
353 (setq new (mapcar parse-entry new)
354 changed (mapcar parse-entry changed))
356 ;; Run the filter
357 (when filter
358 (setq new (delq nil (mapcar filter new))
359 changed (delq nil (mapcar filter new))))
361 (when (not (or new changed))
362 (message "No new items in feed %s" name)
363 (throw 'exit 0))
365 ;; Get alist based on guid, to look up entries
366 (setq guid-alist
367 (append
368 (mapcar (lambda (e) (list (plist-get e :guid) e)) new)
369 (mapcar (lambda (e) (list (plist-get e :guid) e)) changed)))
371 ;; Construct the new status
372 (setq status
373 (mapcar
374 (lambda (e)
375 (setq guid (plist-get e :guid))
376 (list guid
377 ;; things count as handled if we handle them now,
378 ;; or if they were handled previously
379 (if (assoc guid guid-alist) t (plist-get e :handled))
380 ;; A hash, to detect changes
381 (sha1 (plist-get e :item-full-text))))
382 entries))
384 ;; Handle new items in the feed
385 (when new
386 (if new-handler
387 (progn
388 (goto-char inbox-pos)
389 (funcall new-handler new))
390 ;; No custom handler, do the default adding
391 ;; Format the new entries into an alist with GUIDs in the car
392 (setq new-formatted
393 (mapcar
394 (lambda (e) (org-feed-format-entry e template formatter))
395 new)))
397 ;; Insert the new items
398 (org-feed-add-items inbox-pos new-formatted))
400 ;; Handle changed items in the feed
401 (when (and changed-handler changed)
402 (goto-char inbox-pos)
403 (funcall changed-handler changed))
405 ;; Write the new status
406 ;; We do this only now, in case something goes wrong above, so
407 ;; that would would end up with a status that does not reflect
408 ;; which items truely have been handled
409 (org-feed-write-status inbox-pos drawer status)
411 ;; Normalize the visibility of the inbox tree
412 (goto-char inbox-pos)
413 (hide-subtree)
414 (show-children)
415 (org-cycle-hide-drawers 'children)
417 ;; Hooks and messages
418 (when org-feed-save-after-adding (save-buffer))
419 (message "Added %d new item%s from feed %s to file %s, heading %s"
420 (length new) (if (> (length new) 1) "s" "")
421 name
422 (file-name-nondirectory file) headline)
423 (run-hooks 'org-feed-after-adding-hook)
424 (length new))))))
426 ;;;###autoload
427 (defun org-feed-goto-inbox (feed)
428 "Go to the inbox that captures the feed named FEED."
429 (interactive
430 (list (if (= (length org-feed-alist) 1)
431 (car org-feed-alist)
432 (org-completing-read "Feed name: " org-feed-alist))))
433 (if (stringp feed) (setq feed (assoc feed org-feed-alist)))
434 (unless feed
435 (error "No such feed in `org-feed-alist"))
436 (org-feed-goto-inbox-internal (nth 2 feed) (nth 3 feed)))
438 ;;;###autoload
439 (defun org-feed-show-raw-feed (feed)
440 "Show the raw feed buffer of a feed."
441 (interactive
442 (list (if (= (length org-feed-alist) 1)
443 (car org-feed-alist)
444 (org-completing-read "Feed name: " org-feed-alist))))
445 (if (stringp feed) (setq feed (assoc feed org-feed-alist)))
446 (unless feed
447 (error "No such feed in `org-feed-alist"))
448 (switch-to-buffer
449 (org-feed-update feed 'retrieve-only))
450 (goto-char (point-min)))
452 (defun org-feed-goto-inbox-internal (file heading)
453 "Find or create HEADING in FILE.
454 Switch to that buffer, and return the position of that headline."
455 (find-file file)
456 (widen)
457 (goto-char (point-min))
458 (if (re-search-forward
459 (concat "^\\*+[ \t]+" heading "[ \t]*\\(:.*?:[ \t]*\\)?$")
460 nil t)
461 (goto-char (match-beginning 0))
462 (goto-char (point-max))
463 (insert "\n\n* " heading "\n\n")
464 (org-back-to-heading t))
465 (point))
467 (defun org-feed-read-previous-status (pos drawer)
468 "Get the alist of old GUIDs from the entry at POS.
469 This will find DRAWER and extract the alist."
470 (save-excursion
471 (goto-char pos)
472 (let ((end (save-excursion (org-end-of-subtree t t))))
473 (if (re-search-forward
474 (concat "^[ \t]*:" drawer ":[ \t]*\n\\([^\000]*?\\)\n[ \t]*:END:")
475 end t)
476 (read (match-string 1))
477 nil))))
479 (defun org-feed-write-status (pos drawer status)
480 "Write the feed STATUS to DRAWER in entry at POS."
481 (save-excursion
482 (goto-char pos)
483 (let ((end (save-excursion (org-end-of-subtree t t)))
484 guid)
485 (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*\n")
486 end t)
487 (progn
488 (goto-char (match-end 0))
489 (delete-region (point)
490 (save-excursion
491 (and (re-search-forward "^[ \t]*:END:" nil t)
492 (match-beginning 0)))))
493 (outline-next-heading)
494 (insert " :" drawer ":\n :END:\n")
495 (beginning-of-line 0))
496 (insert (pp-to-string status)))))
498 (defun org-feed-add-items (pos entries)
499 "Add the formatted items to the headline as POS."
500 (let (entry level)
501 (save-excursion
502 (goto-char pos)
503 (unless (looking-at org-complex-heading-regexp)
504 (error "Wrong position"))
505 (setq level (org-get-valid-level (length (match-string 1)) 1))
506 (org-end-of-subtree t t)
507 (skip-chars-backward " \t\n")
508 (beginning-of-line 2)
509 (setq pos (point))
510 (while (setq entry (pop entries))
511 (org-paste-subtree level entry 'yank))
512 (org-mark-ring-push pos))))
514 (defun org-feed-format-entry (entry template formatter)
515 "Format ENTRY so that it can be inserted into an Org file.
516 ENTRY is a property list. This function adds a `:formatted-for-org' property
517 and returns the full property list.
518 If that property is already present, nothing changes."
519 (if formatter
520 (funcall formatter entry)
521 (let (dlines fmt tmp indent time name
522 v-h v-t v-T v-u v-U v-a)
523 (setq dlines (org-split-string (or (plist-get entry :description) "???")
524 "\n")
525 v-h (or (plist-get entry :title) (car dlines) "???")
526 time (or (if (plist-get entry :pubDate)
527 (org-read-date t t (plist-get entry :pubDate)))
528 (current-time))
529 v-t (format-time-string (org-time-stamp-format nil nil) time)
530 v-T (format-time-string (org-time-stamp-format t nil) time)
531 v-u (format-time-string (org-time-stamp-format nil t) time)
532 v-U (format-time-string (org-time-stamp-format t t) time)
533 v-a (if (setq tmp (or (and (plist-get entry :guid-permalink)
534 (plist-get entry :guid))
535 (plist-get entry :link)))
536 (concat "[[" tmp "]]\n")
537 ""))
538 (with-temp-buffer
539 (insert template)
540 (goto-char (point-min))
541 (while (re-search-forward "%\\([a-zA-Z]+\\)" nil t)
542 (setq name (match-string 1))
543 (cond
544 ((member name '("h" "t" "T" "u" "U" "a"))
545 (replace-match (symbol-value (intern (concat "v-" name))) t t))
546 ((setq tmp (plist-get entry (intern (concat ":" name))))
547 (save-excursion
548 (save-match-data
549 (beginning-of-line 1)
550 (when (looking-at (concat "^\\([ \t]*\\)%" name "[ \t]*$"))
551 (setq tmp (org-feed-make-indented-block
552 tmp (org-get-indentation))))))
553 (replace-match tmp t t))))
554 (buffer-string)))))
556 (defun org-feed-make-indented-block (s n)
557 "Add indentation of N spaces to a multiline string S."
558 (if (not (string-match "\n" s))
560 (mapconcat 'identity
561 (org-split-string s "\n")
562 (concat "\n" (make-string n ?\ )))))
564 (defun org-feed-skip-http-headers (buffer)
565 "Remove HTTP headers from BUFFER, and return it.
566 Assumes headers are indeed present!"
567 (with-current-buffer buffer
568 (widen)
569 (goto-char (point-min))
570 (search-forward "\n\n")
571 (delete-region (point-min) (point))
572 buffer))
574 (defun org-feed-get-feed (url)
575 "Get the RSS feed file at URL and return the buffer."
576 (cond
577 ((eq org-feed-retrieve-method 'url-retrieve-synchronously)
578 (org-feed-skip-http-headers (url-retrieve-synchronously url)))
579 ((eq org-feed-retrieve-method 'curl)
580 (ignore-errors (kill-buffer org-feed-buffer))
581 (call-process "curl" nil org-feed-buffer nil "--silent" url)
582 org-feed-buffer)
583 ((eq org-feed-retrieve-method 'wget)
584 (ignore-errors (kill-buffer org-feed-buffer))
585 (call-process "wget" nil org-feed-buffer nil "-q" "-O" "-" url)
586 org-feed-buffer)
587 ((functionp org-feed-retrieve-method)
588 (funcall org-feed-retrieve-method url))))
590 (defun org-feed-parse-rss-feed (buffer)
591 "Parse BUFFER for RSS feed entries.
592 Returns a list of entries, with each entry a property list,
593 containing the properties `:guid' and `:item-full-text'."
594 (let ((case-fold-search t)
595 entries beg end item guid entry)
596 (with-current-buffer buffer
597 (widen)
598 (goto-char (point-min))
599 (while (re-search-forward "<item\\>.*?>" nil t)
600 (setq beg (point)
601 end (and (re-search-forward "</item>" nil t)
602 (match-beginning 0)))
603 (setq item (buffer-substring beg end)
604 guid (if (string-match "<guid\\>.*?>\\(.*?\\)</guid>" item)
605 (org-match-string-no-properties 1 item)))
606 (setq entry (list :guid guid :item-full-text item))
607 (push entry entries)
608 (widen)
609 (goto-char end))
610 (nreverse entries))))
612 (defun org-feed-parse-rss-entry (entry)
613 "Parse the `:item-full-text' field for xml tags and create new properties."
614 (with-temp-buffer
615 (insert (plist-get entry :item-full-text))
616 (goto-char (point-min))
617 (while (re-search-forward "<\\([a-zA-Z]+\\>\\).*?>\\([^\000]*?\\)</\\1>"
618 nil t)
619 (setq entry (plist-put entry
620 (intern (concat ":" (match-string 1)))
621 (org-feed-unescape (match-string 2)))))
622 (goto-char (point-min))
623 (unless (re-search-forward "isPermaLink[ \t]*=[ \t]*\"false\"" nil t)
624 (setq entry (plist-put entry :guid-permalink t))))
625 entry)
627 (defun org-feed-parse-atom-feed (buffer)
628 "Parse BUFFER for Atom feed entries.
629 Returns a list of entries, with each entry a property list,
630 containing the properties `:guid' and `:item-full-text'.
632 The `:item-full-text' property actually contains the sexp
633 formatted as a string, not the original XML data."
634 (with-current-buffer buffer
635 (widen)
636 (let ((feed (car (xml-parse-region (point-min) (point-max)))))
637 (mapcar
638 (lambda (entry)
639 (list
640 :guid (car (xml-node-children (car (xml-get-children entry 'id))))
641 :item-full-text (prin1-to-string entry)))
642 (xml-get-children feed 'entry)))))
644 (defun org-feed-parse-atom-entry (entry)
645 "Parse the `:item-full-text' as a sexp and create new properties."
646 (let ((xml (car (read-from-string (plist-get entry :item-full-text)))))
647 ;; Get first <link href='foo'/>.
648 (setq entry (plist-put entry :link
649 (xml-get-attribute
650 (car (xml-get-children xml 'link))
651 'href)))
652 ;; Add <title/> as :title.
653 (setq entry (plist-put entry :title
654 (org-feed-unescape (car (xml-node-children
655 (car (xml-get-children xml 'title)))))))
656 (let* ((content (car (xml-get-children xml 'content)))
657 (type (xml-get-attribute-or-nil content 'type)))
658 (when content
659 (cond
660 ((string= type "text")
661 ;; We like plain text.
662 (setq entry (plist-put entry :description (org-feed-unescape (car (xml-node-children content))))))
663 ((string= type "html")
664 ;; TODO: convert HTML to Org markup.
665 (setq entry (plist-put entry :description (org-feed-unescape (car (xml-node-children content))))))
666 ((string= type "xhtml")
667 ;; TODO: convert XHTML to Org markup.
668 (setq entry (plist-put entry :description (prin1-to-string (xml-node-children content)))))
670 (setq entry (plist-put entry :description (format "Unknown '%s' content." type)))))))
671 entry))
673 (provide 'org-feed)
675 ;; arch-tag: 0929b557-9bc4-47f4-9633-30a12dbb5ae2
676 ;;; org-feed.el ends here