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