我正在尝试创建一个捕获模板,使用<title>
链接名称将URL转换为组织模式链接.
我的转换函数如下所示:
(defun get-page-title (url)
"Get title of web page, whose url can be found in the current line"
;; Get title of web page, with the help of functions in url.el
(with-current-buffer (url-retrieve-synchronously url)
;; find title by grep the html code
(goto-char 0)
(re-search-forward "<title>\\([^<]*\\)</title>" nil t 1)
(setq web_title_str (match-string 1))
;; find charset by grep the html code
(goto-char 0)
;; find the charset, assume utf-8 otherwise
(if (re-search-forward "charset=\\([-0-9a-zA-Z]*\\)" nil t 1)
(setq coding_charset (downcase (match-string 1)))
(setq coding_charset "utf-8")
;; decode the string of title.
(setq web_title_str (decode-coding-string web_title_str (intern
coding_charset)))
)
(concat "[[" url "][" web_title_str "]]")
))
Run Code Online (Sandbox Code Playgroud)
从正常的emacs lisp代码调用时,它返回正确的结果.但是当它在这里使用org-capture-template
它只会返回bad url
.
setq org-capture-templates
(quote
(("l" "Link" entry (file+headline "" "Links")
"* \"%c\" %(get-page-title \"%c\")"))))
Run Code Online (Sandbox Code Playgroud)
扩张的顺序是否不同?我需要以不同的方式逃避字符串吗?魔法?第一个%c
是他们调试字符串,实际上打印为"url".
请不要指出用regexp解析XML是错误的方法.邪神是已经困扰我,这不会使情况变得更糟.
问题是模板参数的扩展顺序.%
在评估sexp之后扩展简单的模板.原始错误消息仍包含模板,因此会扩展到剪贴板的内容,因此错误消息不包含最初传递给的字符串get-page-title
.
解决方案是从sexp中访问kill ring:
%(get-page-title (current-kill 0))
Run Code Online (Sandbox Code Playgroud)
编辑此行为现在记录在组织模式中.
解决方案不是使用org-protocol.el吗? http://orgmode.org/worg/org-contrib/org-protocol.html
我刚刚使用以下模板对其进行了测试(为标题添加了所需标题的子标题).
模板:
("t"
"Testing Template"
entry
(file+headline "~/org/capture.org" "Testing")
"* %^{Title}\n** %:description\n\n Source: %u, %c\n\n%i"
:empty-lines 1)
Run Code Online (Sandbox Code Playgroud)
然后使用基于浏览器的keybind(在我的情况下使用Opera,虽然也提供了Firefox,Uzbl,Acrobat和Conkeror的示例),我能够捕获以下内容:
** Testing for StackExchange
*** org-protocol.el - Intercept calls from emacsclient to trigger custom actions
Source: [2011-08-05 Fri], [[http://orgmode.org/worg/org-contrib/org-protocol.html]
[org-protocol.el - Intercept calls from emacsclient to trigger custom actions]]
org-protocol intercepts calls from emacsclient to trigger custom actions
without external dependencies.
Run Code Online (Sandbox Code Playgroud)
(我打破了org-link只是为了让滚动保持最小,原来不是两行)