org-feed.el 25 KB

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