org-feed: Fix `org-feed-format-entry'

* lisp/org-feed.el (org-feed-format-entry): Fix function according to
  recent changes to Org Capture library.  Small refactoring.

Reported-by: Michael Brand <michael.ch.brand@gmail.com>
<http://permalink.gmane.org/gmane.emacs.orgmode/103941>
This commit is contained in:
Nicolas Goaziou 2016-01-07 17:23:33 +01:00
parent aa52550e4b
commit 1124b2fc2a

View file

@ -1,6 +1,6 @@
;;; org-feed.el --- Add RSS feed items to Org files ;;; org-feed.el --- Add RSS feed items to Org files
;; ;;
;; Copyright (C) 2009-2015 Free Software Foundation, Inc. ;; Copyright (C) 2009-2016 Free Software Foundation, Inc.
;; ;;
;; Author: Carsten Dominik <carsten at orgmode dot org> ;; Author: Carsten Dominik <carsten at orgmode dot org>
;; Keywords: outlines, hypermedia, calendar, wp ;; Keywords: outlines, hypermedia, calendar, wp
@ -513,66 +513,70 @@ ENTRY is a property list. This function adds a `:formatted-for-org' property
and returns the full property list. and returns the full property list.
If that property is already present, nothing changes." If that property is already present, nothing changes."
(require 'org-capture) (require 'org-capture)
(if formatter (if formatter (funcall formatter entry)
(funcall formatter entry) (let* ((dlines
(let (dlines time escape name tmp (org-split-string (or (plist-get entry :description) "???")
v-h v-t v-T v-u v-U v-a) "\n"))
(setq dlines (org-split-string (or (plist-get entry :description) "???") (time (or (if (plist-get entry :pubDate)
"\n") (org-read-date t t (plist-get entry :pubDate)))
v-h (or (plist-get entry :title) (car dlines) "???") (current-time)))
time (or (if (plist-get entry :pubDate) (v-h (or (plist-get entry :title) (car dlines) "???"))
(org-read-date t t (plist-get entry :pubDate))) (v-t (format-time-string (org-time-stamp-format nil nil) time))
(current-time)) (v-T (format-time-string (org-time-stamp-format t nil) time))
v-t (format-time-string (org-time-stamp-format nil nil) time) (v-u (format-time-string (org-time-stamp-format nil t) time))
v-T (format-time-string (org-time-stamp-format t nil) time) (v-U (format-time-string (org-time-stamp-format t t) time))
v-u (format-time-string (org-time-stamp-format nil t) time) (v-a (let ((tmp (or (and (plist-get entry :guid-permalink)
v-U (format-time-string (org-time-stamp-format t t) time) (plist-get entry :guid))
v-a (if (setq tmp (or (and (plist-get entry :guid-permalink) (plist-get entry :link))))
(plist-get entry :guid)) (if tmp (format "[[%s]]\n" tmp ) ""))))
(plist-get entry :link)))
(concat "[[" tmp "]]\n")
""))
(with-temp-buffer (with-temp-buffer
(insert template) (insert template)
(goto-char (point-min))
;; Simple %-escapes ;; Mark %() embedded elisp for later evaluation.
;; before embedded elisp to support simple %-escapes as (org-capture-expand-embedded-elisp 'mark)
;; arguments for embedded elisp
(goto-char (point-min))
(while (re-search-forward "%\\([a-zA-Z]+\\)" nil t)
(unless (org-capture-escaped-%)
(setq name (match-string 1)
escape (org-capture-inside-embedded-elisp-p))
(cond
((member name '("h" "t" "T" "u" "U" "a"))
(setq tmp (symbol-value (intern (concat "v-" name)))))
((setq tmp (plist-get entry (intern (concat ":" name))))
(save-excursion
(save-match-data
(beginning-of-line 1)
(when (looking-at
(concat "^\\([ \t]*\\)%" name "[ \t]*$"))
(setq tmp (org-feed-make-indented-block
tmp (org-get-indentation))))))))
(when tmp
;; escape string delimiters `"' when inside %() embedded lisp
(when escape
(setq tmp (replace-regexp-in-string "\"" "\\\\\"" tmp)))
(replace-match tmp t t))))
;; %() embedded elisp ;; Simple %-escapes
(org-capture-expand-embedded-elisp) (while (re-search-forward "%\\([a-zA-Z]+\\)" nil t)
(unless (org-capture-escaped-%)
(let ((replacement
(pcase (match-string-no-properties 1)
("h" v-h)
("t" v-t)
("T" v-T)
("u" v-u)
("U" v-U)
("a" v-a)
(name
(let ((v (plist-get entry (intern (concat ":" name)))))
(save-excursion
(save-match-data
(beginning-of-line)
(if (looking-at
(concat "^\\([ \t]*\\)%" name "[ \t]*$"))
(org-feed-make-indented-block
v (org-get-indentation))
v))))))))
(when replacement
(replace-match
;; Escape string delimiters within embedded lisp.
(if (org-capture-inside-embedded-elisp-p)
(replace-regexp-in-string "\"" "\\\\\"" replacement nil t)
replacement))))))
(decode-coding-string ;; %() embedded elisp
(buffer-string) (detect-coding-region (point-min) (point-max) t)))))) (org-capture-expand-embedded-elisp)
(decode-coding-string
(buffer-string) (detect-coding-region (point-min) (point-max) t))))))
(defun org-feed-make-indented-block (s n) (defun org-feed-make-indented-block (s n)
"Add indentation of N spaces to a multiline string S." "Add indentation of N spaces to a multiline string S."
(if (not (string-match "\n" s)) (if (not (string-match "\n" s))
s s
(mapconcat 'identity (mapconcat 'identity
(org-split-string s "\n") (org-split-string s "\n")
(concat "\n" (make-string n ?\ ))))) (concat "\n" (make-string n ?\ )))))
(defun org-feed-skip-http-headers (buffer) (defun org-feed-skip-http-headers (buffer)
"Remove HTTP headers from BUFFER, and return it. "Remove HTTP headers from BUFFER, and return it.