Remove BYTE_MAINTAIN_TOP
[emacs.git] / lisp / org / org-feed.el
blob38f4a9fac4f9fbfb832313dbbf24acc7f14fa9db
1 ;;; org-feed.el --- Add RSS feed items to Org files
2 ;;
3 ;; Copyright (C) 2009-2016 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 entries to be created and changed 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"
97 (url &optional silent inhibit-cookies timeout))
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 (declare-function xml-substitute-special "xml" (string))
104 (declare-function org-capture-escaped-% "org-capture" ())
105 (declare-function org-capture-inside-embedded-elisp-p "org-capture" ())
106 (declare-function org-capture-expand-embedded-elisp "org-capture" ())
108 (defgroup org-feed nil
109 "Options concerning RSS feeds as inputs for Org files."
110 :tag "Org Feed"
111 :group 'org)
113 (defcustom org-feed-alist nil
114 "Alist specifying RSS feeds that should create inputs for Org.
115 Each entry in this list specified an RSS feed tat should be queried
116 to create inbox items in Org. Each entry is a list with the following items:
118 name a custom name for this feed
119 URL the Feed URL
120 file the target Org file where entries should be listed
121 headline the headline under which entries should be listed
123 Additional arguments can be given using keyword-value pairs. Many of these
124 specify functions that receive one or a list of \"entries\" as their single
125 argument. An entry is a property list that describes a feed item. The
126 property list has properties for each field in the item, for example `:title'
127 for the `<title>' field and `:pubDate' for the publication date. In addition,
128 it contains the following properties:
130 `:item-full-text' the full text in the <item> tag
131 `:guid-permalink' t when the guid property is a permalink
133 Here are the keyword-value pair allows in `org-feed-alist'.
135 :drawer drawer-name
136 The name of the drawer for storing feed information. The default is
137 \"FEEDSTATUS\". Using different drawers for different feeds allows
138 several feeds to target the same inbox heading.
140 :filter filter-function
141 A function to select interesting entries in the feed. It gets a single
142 entry as parameter. It should return the entry if it is relevant, or
143 nil if it is not.
145 :template template-string
146 The default action on new items in the feed is to add them as children
147 under the headline for the feed. The template describes how the entry
148 should be formatted. If not given, it defaults to
149 `org-feed-default-template'.
151 :formatter formatter-function
152 Instead of relying on a template, you may specify a function to format
153 the outline node to be inserted as a child. This function gets passed
154 a property list describing a single feed item, and it should return a
155 string that is a properly formatted Org outline node of level 1.
157 :new-handler function
158 If adding new items as children to the outline is not what you want
159 to do with new items, define a handler function that is called with
160 a list of all new items in the feed, each one represented as a property
161 list. The handler should do what needs to be done, and org-feed will
162 mark all items given to this handler as \"handled\", i.e. they will not
163 be passed to this handler again in future readings of the feed.
164 When the handler is called, point will be at the feed headline.
166 :changed-handler function
167 This function gets passed a list of all entries that have been
168 handled before, but are now still in the feed and have *changed*
169 since last handled (as evidenced by a different sha1 hash).
170 When the handler is called, point will be at the feed headline.
172 :parse-feed function
173 This function gets passed a buffer, and should return a list
174 of entries, each being a property list containing the
175 `:guid' and `:item-full-text' keys. The default is
176 `org-feed-parse-rss-feed'; `org-feed-parse-atom-feed' is an
177 alternative.
179 :parse-entry function
180 This function gets passed an entry as returned by the parse-feed
181 function, and should return the entry with interesting properties added.
182 The default is `org-feed-parse-rss-entry'; `org-feed-parse-atom-entry'
183 is an alternative."
184 :group 'org-feed
185 :type '(repeat
186 (list :value ("" "http://" "" "")
187 (string :tag "Name")
188 (string :tag "Feed URL")
189 (file :tag "File for inbox")
190 (string :tag "Headline for inbox")
191 (repeat :inline t
192 (choice
193 (list :inline t :tag "Filter"
194 (const :filter)
195 (symbol :tag "Filter Function"))
196 (list :inline t :tag "Template"
197 (const :template)
198 (string :tag "Template"))
199 (list :inline t :tag "Formatter"
200 (const :formatter)
201 (symbol :tag "Formatter Function"))
202 (list :inline t :tag "New items handler"
203 (const :new-handler)
204 (symbol :tag "Handler Function"))
205 (list :inline t :tag "Changed items"
206 (const :changed-handler)
207 (symbol :tag "Handler Function"))
208 (list :inline t :tag "Parse Feed"
209 (const :parse-feed)
210 (symbol :tag "Parse Feed Function"))
211 (list :inline t :tag "Parse Entry"
212 (const :parse-entry)
213 (symbol :tag "Parse Entry Function"))
214 )))))
216 (defcustom org-feed-drawer "FEEDSTATUS"
217 "The name of the drawer for feed status information.
218 Each feed may also specify its own drawer name using the `:drawer'
219 parameter in `org-feed-alist'.
220 Note that in order to make these drawers behave like drawers, they must
221 be added to the variable `org-drawers' or configured with a #+DRAWERS
222 line."
223 :group 'org-feed
224 :type '(string :tag "Drawer Name"))
226 (defcustom org-feed-default-template "\n* %h\n %U\n %description\n %a\n"
227 "Template for the Org node created from RSS feed items.
228 This is just the default, each feed can specify its own.
229 Any fields from the feed item can be interpolated into the template with
230 %name, for example %title, %description, %pubDate etc. In addition, the
231 following special escapes are valid as well:
233 %h The title, or the first line of the description
234 %t The date as a stamp, either from <pubDate> (if present), or
235 the current date
236 %T Date and time
237 %u,%U Like %t,%T, but inactive time stamps
238 %a A link, from <guid> if that is a permalink, else from <link>
239 %(sexp) Evaluate elisp `(sexp)' and replace with the result, the simple
240 %-escapes above can be used as arguments, e.g. %(capitalize \\\"%h\\\")"
241 :group 'org-feed
242 :type '(string :tag "Template"))
244 (defcustom org-feed-save-after-adding t
245 "Non-nil means save buffer after adding new feed items."
246 :group 'org-feed
247 :type 'boolean)
249 (defcustom org-feed-retrieve-method 'url-retrieve-synchronously
250 "The method to be used to retrieve a feed URL.
251 This can be `curl' or `wget' to call these external programs, or it can be
252 an Emacs Lisp function that will return a buffer containing the content
253 of the file pointed to by the URL."
254 :group 'org-feed
255 :type '(choice
256 (const :tag "Internally with url.el" url-retrieve-synchronously)
257 (const :tag "Externally with curl" curl)
258 (const :tag "Externally with wget" wget)
259 (function :tag "Function")))
261 (defcustom org-feed-before-adding-hook nil
262 "Hook that is run before adding new feed items to a file.
263 You might want to commit the file in its current state to version control,
264 for example."
265 :group 'org-feed
266 :type 'hook)
268 (defcustom org-feed-after-adding-hook nil
269 "Hook that is run after new items have been added to a file.
270 Depending on `org-feed-save-after-adding', the buffer will already
271 have been saved."
272 :group 'org-feed
273 :type 'hook)
275 (defvar org-feed-buffer "*Org feed*"
276 "The buffer used to retrieve a feed.")
278 ;;;###autoload
279 (defun org-feed-update-all ()
280 "Get inbox items from all feeds in `org-feed-alist'."
281 (interactive)
282 (let ((nfeeds (length org-feed-alist))
283 (nnew (apply '+ (mapcar 'org-feed-update org-feed-alist))))
284 (message "%s from %d %s"
285 (cond ((= nnew 0) "No new entries")
286 ((= nnew 1) "1 new entry")
287 (t (format "%d new entries" nnew)))
288 nfeeds
289 (if (= nfeeds 1) "feed" "feeds"))))
291 ;;;###autoload
292 (defun org-feed-update (feed &optional retrieve-only)
293 "Get inbox items from FEED.
294 FEED can be a string with an association in `org-feed-alist', or
295 it can be a list structured like an entry in `org-feed-alist'."
296 (interactive (list (org-completing-read "Feed name: " org-feed-alist)))
297 (if (stringp feed) (setq feed (assoc feed org-feed-alist)))
298 (unless feed
299 (error "No such feed in `org-feed-alist"))
300 (catch 'exit
301 (let ((name (car feed))
302 (url (nth 1 feed))
303 (file (nth 2 feed))
304 (headline (nth 3 feed))
305 (filter (nth 1 (memq :filter feed)))
306 (formatter (nth 1 (memq :formatter feed)))
307 (new-handler (nth 1 (memq :new-handler feed)))
308 (changed-handler (nth 1 (memq :changed-handler feed)))
309 (template (or (nth 1 (memq :template feed))
310 org-feed-default-template))
311 (drawer (or (nth 1 (memq :drawer feed))
312 org-feed-drawer))
313 (parse-feed (or (nth 1 (memq :parse-feed feed))
314 'org-feed-parse-rss-feed))
315 (parse-entry (or (nth 1 (memq :parse-entry feed))
316 'org-feed-parse-rss-entry))
317 feed-buffer inbox-pos new-formatted
318 entries old-status status new changed guid-alist e guid olds)
319 (setq feed-buffer (org-feed-get-feed url))
320 (unless (and feed-buffer (bufferp (get-buffer feed-buffer)))
321 (error "Cannot get feed %s" name))
322 (when retrieve-only
323 (throw 'exit feed-buffer))
324 (setq entries (funcall parse-feed feed-buffer))
325 (ignore-errors (kill-buffer feed-buffer))
326 (save-excursion
327 (save-window-excursion
328 (setq inbox-pos (org-feed-goto-inbox-internal file headline))
329 (setq old-status (org-feed-read-previous-status inbox-pos drawer))
330 ;; Add the "handled" status to the appropriate entries
331 (setq entries (mapcar (lambda (e)
332 (setq e
333 (plist-put e :handled
334 (nth 1 (assoc
335 (plist-get e :guid)
336 old-status)))))
337 entries))
338 ;; Find out which entries are new and which are changed
339 (dolist (e entries)
340 (if (not (plist-get e :handled))
341 (push e new)
342 (setq olds (nth 2 (assoc (plist-get e :guid) old-status)))
343 (if (and olds
344 (not (string= (sha1
345 (plist-get e :item-full-text))
346 olds)))
347 (push e changed))))
349 ;; Parse the relevant entries fully
350 (setq new (mapcar parse-entry new)
351 changed (mapcar parse-entry changed))
353 ;; Run the filter
354 (when filter
355 (setq new (delq nil (mapcar filter new))
356 changed (delq nil (mapcar filter new))))
358 (when (not (or new changed))
359 (message "No new items in feed %s" name)
360 (throw 'exit 0))
362 ;; Get alist based on guid, to look up entries
363 (setq guid-alist
364 (append
365 (mapcar (lambda (e) (list (plist-get e :guid) e)) new)
366 (mapcar (lambda (e) (list (plist-get e :guid) e)) changed)))
368 ;; Construct the new status
369 (setq status
370 (mapcar
371 (lambda (e)
372 (setq guid (plist-get e :guid))
373 (list guid
374 ;; things count as handled if we handle them now,
375 ;; or if they were handled previously
376 (if (assoc guid guid-alist) t (plist-get e :handled))
377 ;; A hash, to detect changes
378 (sha1 (plist-get e :item-full-text))))
379 entries))
381 ;; Handle new items in the feed
382 (when new
383 (if new-handler
384 (progn
385 (goto-char inbox-pos)
386 (funcall new-handler new))
387 ;; No custom handler, do the default adding
388 ;; Format the new entries into an alist with GUIDs in the car
389 (setq new-formatted
390 (mapcar
391 (lambda (e) (org-feed-format-entry e template formatter))
392 new)))
394 ;; Insert the new items
395 (org-feed-add-items inbox-pos new-formatted))
397 ;; Handle changed items in the feed
398 (when (and changed-handler changed)
399 (goto-char inbox-pos)
400 (funcall changed-handler changed))
402 ;; Write the new status
403 ;; We do this only now, in case something goes wrong above, so
404 ;; that would would end up with a status that does not reflect
405 ;; which items truely have been handled
406 (org-feed-write-status inbox-pos drawer status)
408 ;; Normalize the visibility of the inbox tree
409 (goto-char inbox-pos)
410 (hide-subtree)
411 (show-children)
412 (org-cycle-hide-drawers 'children)
414 ;; Hooks and messages
415 (when org-feed-save-after-adding (save-buffer))
416 (message "Added %d new item%s from feed %s to file %s, heading %s"
417 (length new) (if (> (length new) 1) "s" "")
418 name
419 (file-name-nondirectory file) headline)
420 (run-hooks 'org-feed-after-adding-hook)
421 (length new))))))
423 ;;;###autoload
424 (defun org-feed-goto-inbox (feed)
425 "Go to the inbox that captures the feed named FEED."
426 (interactive
427 (list (if (= (length org-feed-alist) 1)
428 (car org-feed-alist)
429 (org-completing-read "Feed name: " org-feed-alist))))
430 (if (stringp feed) (setq feed (assoc feed org-feed-alist)))
431 (unless feed
432 (error "No such feed in `org-feed-alist"))
433 (org-feed-goto-inbox-internal (nth 2 feed) (nth 3 feed)))
435 ;;;###autoload
436 (defun org-feed-show-raw-feed (feed)
437 "Show the raw feed buffer of a feed."
438 (interactive
439 (list (if (= (length org-feed-alist) 1)
440 (car org-feed-alist)
441 (org-completing-read "Feed name: " org-feed-alist))))
442 (if (stringp feed) (setq feed (assoc feed org-feed-alist)))
443 (unless feed
444 (error "No such feed in `org-feed-alist"))
445 (org-pop-to-buffer-same-window
446 (org-feed-update feed 'retrieve-only))
447 (goto-char (point-min)))
449 (defun org-feed-goto-inbox-internal (file heading)
450 "Find or create HEADING in FILE.
451 Switch to that buffer, and return the position of that headline."
452 (find-file file)
453 (widen)
454 (goto-char (point-min))
455 (if (re-search-forward
456 (concat "^\\*+[ \t]+" heading "[ \t]*\\(:.*?:[ \t]*\\)?$")
457 nil t)
458 (goto-char (match-beginning 0))
459 (goto-char (point-max))
460 (insert "\n\n* " heading "\n\n")
461 (org-back-to-heading t))
462 (point))
464 (defun org-feed-read-previous-status (pos drawer)
465 "Get the alist of old GUIDs from the entry at POS.
466 This will find DRAWER and extract the alist."
467 (save-excursion
468 (goto-char pos)
469 (let ((end (save-excursion (org-end-of-subtree t t))))
470 (if (re-search-forward
471 (concat "^[ \t]*:" drawer ":[ \t]*\n\\([^\000]*?\\)\n[ \t]*:END:")
472 end t)
473 (read (match-string 1))
474 nil))))
476 (defun org-feed-write-status (pos drawer status)
477 "Write the feed STATUS to DRAWER in entry at POS."
478 (save-excursion
479 (goto-char pos)
480 (let ((end (save-excursion (org-end-of-subtree t t)))
481 guid)
482 (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*\n")
483 end t)
484 (progn
485 (goto-char (match-end 0))
486 (delete-region (point)
487 (save-excursion
488 (and (re-search-forward "^[ \t]*:END:" nil t)
489 (match-beginning 0)))))
490 (outline-next-heading)
491 (insert " :" drawer ":\n :END:\n")
492 (beginning-of-line 0))
493 (insert (pp-to-string status)))))
495 (defun org-feed-add-items (pos entries)
496 "Add the formatted items to the headline as POS."
497 (let (entry level)
498 (save-excursion
499 (goto-char pos)
500 (unless (looking-at org-complex-heading-regexp)
501 (error "Wrong position"))
502 (setq level (org-get-valid-level (length (match-string 1)) 1))
503 (org-end-of-subtree t t)
504 (skip-chars-backward " \t\n")
505 (beginning-of-line 2)
506 (setq pos (point))
507 (while (setq entry (pop entries))
508 (org-paste-subtree level entry 'yank))
509 (org-mark-ring-push pos))))
511 (defun org-feed-format-entry (entry template formatter)
512 "Format ENTRY so that it can be inserted into an Org file.
513 ENTRY is a property list. This function adds a `:formatted-for-org' property
514 and returns the full property list.
515 If that property is already present, nothing changes."
516 (require 'org-capture)
517 (if formatter
518 (funcall formatter entry)
519 (let (dlines time escape name tmp
520 v-h v-t v-T v-u v-U v-a)
521 (setq dlines (org-split-string (or (plist-get entry :description) "???")
522 "\n")
523 v-h (or (plist-get entry :title) (car dlines) "???")
524 time (or (if (plist-get entry :pubDate)
525 (org-read-date t t (plist-get entry :pubDate)))
526 (current-time))
527 v-t (format-time-string (org-time-stamp-format nil nil) time)
528 v-T (format-time-string (org-time-stamp-format t nil) time)
529 v-u (format-time-string (org-time-stamp-format nil t) time)
530 v-U (format-time-string (org-time-stamp-format t t) time)
531 v-a (if (setq tmp (or (and (plist-get entry :guid-permalink)
532 (plist-get entry :guid))
533 (plist-get entry :link)))
534 (concat "[[" tmp "]]\n")
535 ""))
536 (with-temp-buffer
537 (insert template)
539 ;; Simple %-escapes
540 ;; before embedded elisp to support simple %-escapes as
541 ;; arguments for embedded elisp
542 (goto-char (point-min))
543 (while (re-search-forward "%\\([a-zA-Z]+\\)" nil t)
544 (unless (org-capture-escaped-%)
545 (setq name (match-string 1)
546 escape (org-capture-inside-embedded-elisp-p))
547 (cond
548 ((member name '("h" "t" "T" "u" "U" "a"))
549 (setq tmp (symbol-value (intern (concat "v-" name)))))
550 ((setq tmp (plist-get entry (intern (concat ":" name))))
551 (save-excursion
552 (save-match-data
553 (beginning-of-line 1)
554 (when (looking-at
555 (concat "^\\([ \t]*\\)%" name "[ \t]*$"))
556 (setq tmp (org-feed-make-indented-block
557 tmp (org-get-indentation))))))))
558 (when tmp
559 ;; escape string delimiters `"' when inside %() embedded lisp
560 (when escape
561 (setq tmp (replace-regexp-in-string "\"" "\\\\\"" tmp)))
562 (replace-match tmp t t))))
564 ;; %() embedded elisp
565 (org-capture-expand-embedded-elisp)
567 (decode-coding-string
568 (buffer-string) (detect-coding-region (point-min) (point-max) t))))))
570 (defun org-feed-make-indented-block (s n)
571 "Add indentation of N spaces to a multiline string S."
572 (if (not (string-match "\n" s))
574 (mapconcat 'identity
575 (org-split-string s "\n")
576 (concat "\n" (make-string n ?\ )))))
578 (defun org-feed-skip-http-headers (buffer)
579 "Remove HTTP headers from BUFFER, and return it.
580 Assumes headers are indeed present!"
581 (with-current-buffer buffer
582 (widen)
583 (goto-char (point-min))
584 (search-forward "\n\n")
585 (delete-region (point-min) (point))
586 buffer))
588 (defun org-feed-get-feed (url)
589 "Get the RSS feed file at URL and return the buffer."
590 (cond
591 ((eq org-feed-retrieve-method 'url-retrieve-synchronously)
592 (org-feed-skip-http-headers (url-retrieve-synchronously url)))
593 ((eq org-feed-retrieve-method 'curl)
594 (ignore-errors (kill-buffer org-feed-buffer))
595 (call-process "curl" nil org-feed-buffer nil "--silent" url)
596 org-feed-buffer)
597 ((eq org-feed-retrieve-method 'wget)
598 (ignore-errors (kill-buffer org-feed-buffer))
599 (call-process "wget" nil org-feed-buffer nil "-q" "-O" "-" url)
600 org-feed-buffer)
601 ((functionp org-feed-retrieve-method)
602 (funcall org-feed-retrieve-method url))))
604 (defun org-feed-parse-rss-feed (buffer)
605 "Parse BUFFER for RSS feed entries.
606 Returns a list of entries, with each entry a property list,
607 containing the properties `:guid' and `:item-full-text'."
608 (let ((case-fold-search t)
609 entries beg end item guid entry)
610 (with-current-buffer buffer
611 (widen)
612 (goto-char (point-min))
613 (while (re-search-forward "<item\\>.*?>" nil t)
614 (setq beg (point)
615 end (and (re-search-forward "</item>" nil t)
616 (match-beginning 0)))
617 (setq item (buffer-substring beg end)
618 guid (if (string-match "<guid\\>.*?>\\(.*?\\)</guid>" item)
619 (org-match-string-no-properties 1 item)))
620 (setq entry (list :guid guid :item-full-text item))
621 (push entry entries)
622 (widen)
623 (goto-char end))
624 (nreverse entries))))
626 (defun org-feed-parse-rss-entry (entry)
627 "Parse the `:item-full-text' field for xml tags and create new properties."
628 (require 'xml)
629 (with-temp-buffer
630 (insert (plist-get entry :item-full-text))
631 (goto-char (point-min))
632 (while (re-search-forward "<\\([a-zA-Z]+\\>\\).*?>\\([^\000]*?\\)</\\1>"
633 nil t)
634 (setq entry (plist-put entry
635 (intern (concat ":" (match-string 1)))
636 (xml-substitute-special (match-string 2)))))
637 (goto-char (point-min))
638 (unless (re-search-forward "isPermaLink[ \t]*=[ \t]*\"false\"" nil t)
639 (setq entry (plist-put entry :guid-permalink t))))
640 entry)
642 (defun org-feed-parse-atom-feed (buffer)
643 "Parse BUFFER for Atom feed entries.
644 Returns a list of entries, with each entry a property list,
645 containing the properties `:guid' and `:item-full-text'.
647 The `:item-full-text' property actually contains the sexp
648 formatted as a string, not the original XML data."
649 (require 'xml)
650 (with-current-buffer buffer
651 (widen)
652 (let ((feed (car (xml-parse-region (point-min) (point-max)))))
653 (mapcar
654 (lambda (entry)
655 (list
656 :guid (car (xml-node-children (car (xml-get-children entry 'id))))
657 :item-full-text (prin1-to-string entry)))
658 (xml-get-children feed 'entry)))))
660 (defun org-feed-parse-atom-entry (entry)
661 "Parse the `:item-full-text' as a sexp and create new properties."
662 (let ((xml (car (read-from-string (plist-get entry :item-full-text)))))
663 ;; Get first <link href='foo'/>.
664 (setq entry (plist-put entry :link
665 (xml-get-attribute
666 (car (xml-get-children xml 'link))
667 'href)))
668 ;; Add <title/> as :title.
669 (setq entry (plist-put entry :title
670 (xml-substitute-special
671 (car (xml-node-children
672 (car (xml-get-children xml 'title)))))))
673 (let* ((content (car (xml-get-children xml 'content)))
674 (type (xml-get-attribute-or-nil content 'type)))
675 (when content
676 (cond
677 ((string= type "text")
678 ;; We like plain text.
679 (setq entry (plist-put entry :description
680 (xml-substitute-special
681 (car (xml-node-children content))))))
682 ((string= type "html")
683 ;; TODO: convert HTML to Org markup.
684 (setq entry (plist-put entry :description
685 (xml-substitute-special
686 (car (xml-node-children content))))))
687 ((string= type "xhtml")
688 ;; TODO: convert XHTML to Org markup.
689 (setq entry (plist-put entry :description
690 (prin1-to-string
691 (xml-node-children content)))))
693 (setq entry (plist-put entry :description
694 (format-message
695 "Unknown `%s' content." type)))))))
696 entry))
698 (provide 'org-feed)
700 ;; Local variables:
701 ;; generated-autoload-file: "org-loaddefs.el"
702 ;; End:
704 ;;; org-feed.el ends here