From 38ff96ecc37ebfe3b65b48c2b0a8a07264868b88 Mon Sep 17 00:00:00 2001 From: Nicolas Goaziou Date: Sat, 26 Jul 2014 10:47:29 +0200 Subject: [PATCH] org-element: Fix parsing multi-line links * lisp/org-element.el (org-element-link-parser): Properly handle multi-line links according to RFC 3986 when enclosed within square brackets. * testing/lisp/test-org-element.el (test-org-element/link-parser): Add test. --- lisp/org-element.el | 14 +++++++++----- testing/lisp/test-org-element.el | 21 +++++++++++++-------- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/lisp/org-element.el b/lisp/org-element.el index ed058be83..bbed67a04 100644 --- a/lisp/org-element.el +++ b/lisp/org-element.el @@ -3107,16 +3107,20 @@ Assume point is at the beginning of the link." (cond ;; File type. ((or (file-name-absolute-p raw-link) - (string-match "^\\.\\.?/" raw-link)) + (string-match "\\`\\.\\.?/" raw-link)) (setq type "file" path raw-link)) ;; Explicit type (http, irc, bbdb...). See `org-link-types'. - ((string-match org-link-re-with-space3 raw-link) - (setq type (match-string 1 raw-link) path (match-string 2 raw-link))) + ((string-match org-link-types-re raw-link) + (setq type (match-string 1 raw-link) + ;; According to RFC 3986, extra whitespace should be + ;; ignored when a URI is extracted. + path (replace-regexp-in-string + "[ \t]*\n[ \t]*" "" (substring raw-link (match-end 0))))) ;; Id type: PATH is the id. - ((string-match "^id:\\([-a-f0-9]+\\)" raw-link) + ((string-match "\\`id:\\([-a-f0-9]+\\)" raw-link) (setq type "id" path (match-string 1 raw-link))) ;; Code-ref type: PATH is the name of the reference. - ((string-match "^(\\(.*\\))$" raw-link) + ((string-match "\\`(\\(.*\\))\\'" raw-link) (setq type "coderef" path (match-string 1 raw-link))) ;; Custom-id type: PATH is the name of the custom id. ((= (aref raw-link 0) ?#) diff --git a/testing/lisp/test-org-element.el b/testing/lisp/test-org-element.el index 7036a89fe..389fe8740 100644 --- a/testing/lisp/test-org-element.el +++ b/testing/lisp/test-org-element.el @@ -1388,17 +1388,22 @@ e^{i\\pi}+1=0 (org-test-with-temp-text "[[file:projects.org::*task title]]" (org-element-map (org-element-parse-buffer) 'link (lambda (l) (list (org-element-property :type l) - (org-element-property :path l) - (org-element-property :search-option l))))))) - ;; ... file-type link with application. + (org-element-property :path l) + (org-element-property :search-option l))))))) + ;; ... file-type link with application... (should (equal - '(("file" "projects.org" "docview")) + '("file" "projects.org" "docview") (org-test-with-temp-text "[[docview:projects.org]]" - (org-element-map (org-element-parse-buffer) 'link - (lambda (l) (list (org-element-property :type l) - (org-element-property :path l) - (org-element-property :application l))))))) + (let ((l (org-element-context))) + (list (org-element-property :type l) + (org-element-property :path l) + (org-element-property :application l)))))) + ;; ... multi-line link. + (should + (equal "//orgmode.org" + (org-test-with-temp-text "[[http://orgmode.\norg]]" + (org-element-property :path (org-element-context))))) ;; Plain link. (should (org-test-with-temp-text "A link: http://orgmode.org"