org-feed.el 26 KB

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