我試圖創建一個捕獲模板,將URL轉換爲組織鏈接模式<title>
作爲鏈接名稱。與性別組織模式捕獲
我的轉換功能是這樣的:
(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 "]]")
))
當從正常的Emacs稱爲Lisp代碼返回正確的結果。但是,在此org-capture-template
中使用時,它僅返回bad url
。
setq org-capture-templates
(quote
(("l" "Link" entry (file+headline "" "Links")
"* \"%c\" %(get-page-title \"%c\")"))))
是否擴展順序不同?我需要以不同的方式逃避字符串嗎?魔法? 第一個%c
只是它們的調試字符串,並且確實正在打印爲「url」。
請不要打擾指出用正則表達式解析XML是錯誤的方法。克蘇魯是已經困擾着我,這不會讓事情變得更糟。
爲什麼這不是一個被接受的答案? –
@LeVieuxGildas接受我自己的答案總是有點痛苦。我更新並接受了它。 – pmr
我閱讀[手冊頁](http://orgmode.org/manual/Template-expansion.html#Template-expansion)的方式表示它應該與此相反 - 「評估Elisp sexp並將結果替換。方便,%:關鍵字(見下文)表達式中的佔位符將在此之前展開。「但我同意你所描述的是似乎正在發生的事情。 – studgeek