Browse Source

ox: Whitespaces are not significant when matching a fuzzy link

* lisp/ox.el (org-export-resolve-fuzzy-link): Whitespaces are not
  significant when matching a fuzzy link.
* lisp/org-element.el (org-element-link-parser): Do not remove
  newlines characters in paths anymore, since this is not required.
* testing/lisp/test-org-element.el: Update tests.
* testing/lisp/test-ox.el: Add test.
Nicolas Goaziou 4 years ago
parent
commit
78a652716e
4 changed files with 49 additions and 45 deletions
  1. 1 5
      lisp/org-element.el
  2. 17 9
      lisp/ox.el
  3. 1 7
      testing/lisp/test-org-element.el
  4. 30 24
      testing/lisp/test-ox.el

+ 1 - 5
lisp/org-element.el

@@ -3007,11 +3007,7 @@ Assume point is at the beginning of the link."
 	      raw-link (org-translate-link
 			(org-link-expand-abbrev
 			 (org-match-string-no-properties 1)))
-	      ;; Remove newline characters due to filling.  Headlines,
-	      ;; targets, radio targets and name affiliated keywords
-	      ;; cannot contain any.
-	      link (org-link-unescape
-		    (replace-regexp-in-string "\n[ \t]*" " " raw-link)))
+	      link (org-link-unescape raw-link))
 	;; Determine TYPE of link and set PATH accordingly.
 	(cond
 	 ;; File type.

+ 17 - 9
lisp/ox.el

@@ -3658,9 +3658,14 @@ Return value can be an object, an element, or nil:
 
 - Otherwise, return nil.
 
-Assume LINK type is \"fuzzy\"."
-  (let* ((path (org-element-property :path link))
-	 (match-title-p (eq (aref path 0) ?*)))
+Assume LINK type is \"fuzzy\".  White spaces are not
+significant."
+  (let* ((raw-path (org-element-property :path link))
+	 (match-title-p (eq (aref raw-path 0) ?*))
+	 ;; Split PATH at white spaces so matches are space
+	 ;; insensitive.
+	 (path (org-split-string
+		(if match-title-p (substring raw-path 1) raw-path))))
     (cond
      ;; First try to find a matching "<<path>>" unless user specified
      ;; he was looking for an headline (path starts with a *
@@ -3670,7 +3675,8 @@ Assume LINK type is \"fuzzy\"."
 	     (lambda (blob)
 	       (and (or (eq (org-element-type blob) 'target)
 			(equal (org-element-property :key blob) "TARGET"))
-		    (equal (org-element-property :value blob) path)
+		    (equal (org-split-string (org-element-property :value blob))
+			   path)
 		    blob))
 	     info t)))
      ;; Then try to find an element with a matching "#+NAME: path"
@@ -3679,7 +3685,8 @@ Assume LINK type is \"fuzzy\"."
 	   (org-element-map (plist-get info :parse-tree)
 	       org-element-all-elements
 	     (lambda (el)
-	       (when (string= (org-element-property :name el) path) el))
+	       (let ((name (org-element-property :name el)))
+		 (when (and name (equal (org-split-string name) path)) el)))
 	     info 'first-match)))
      ;; Last case: link either points to an headline or to
      ;; nothingness.  Try to find the source, with priority given to
@@ -3693,9 +3700,9 @@ Assume LINK type is \"fuzzy\"."
 	      (lambda (name data)
 		(org-element-map data 'headline
 		  (lambda (headline)
-		    (when (string=
-			   (org-element-property :raw-value headline)
-			   name)
+		    (when (equal (org-split-string
+				  (org-element-property :raw-value headline))
+				 name)
 		      headline))
 		  info 'first-match)))))
 	;; Search among headlines sharing an ancestor with link, from
@@ -3709,7 +3716,8 @@ Assume LINK type is \"fuzzy\"."
 	       (org-export-get-genealogy link)) nil)
 	    ;; No match with a common ancestor: try full parse-tree.
 	    (funcall find-headline
-		     (if match-title-p (substring path 1) path)
+		     (if (not match-title-p) path
+		       (cons (substring (car path) 1) (cdr path)))
 		     (plist-get info :parse-tree))))))))
 
 (defun org-export-resolve-id-link (link info)

+ 1 - 7
testing/lisp/test-org-element.el

@@ -1288,13 +1288,7 @@ e^{i\\pi}+1=0
 		   (org-element-map
 		    (org-element-parse-buffer) 'link
 		    (lambda (link) (org-element-property :type link))
-		    nil t nil t)))))
-  ;; Corner case: links with filled path.
-  (should
-   (equal "a b"
-	  (org-test-with-temp-text "* a b\n[[a\nb]]"
-	    (progn (forward-line 1)
-		   (org-element-property :path (org-element-context)))))))
+		    nil t nil t))))))
 
 
 ;;;; Macro

+ 30 - 24
testing/lisp/test-ox.el

@@ -1205,10 +1205,10 @@ Paragraph[fn:1]"
       "Paragraph.\n#+TARGET: Test\n[[Test]]"
     (should-not
      (org-element-map
-      tree 'link
-      (lambda (link)
-	(org-export-get-ordinal
-	 (org-export-resolve-fuzzy-link link info) info)) info)))
+	 tree 'link
+       (lambda (link)
+	 (org-export-get-ordinal
+	  (org-export-resolve-fuzzy-link link info) info)) info)))
   ;; 2. Link to an headline should return headline's number.
   (org-test-with-parsed-data
       "Paragraph.\n* Head1\n* Head2\n* Head3\n[[Head2]]"
@@ -1216,10 +1216,10 @@ Paragraph[fn:1]"
      ;; Note: Headline's number is in fact a list of numbers.
      (equal '(2)
 	    (org-element-map
-	     tree 'link
-	     (lambda (link)
-	       (org-export-get-ordinal
-		(org-export-resolve-fuzzy-link link info) info)) info t))))
+		tree 'link
+	      (lambda (link)
+		(org-export-get-ordinal
+		 (org-export-resolve-fuzzy-link link info) info)) info t))))
   ;; 3. Link to a target in an item should return item's number.
   (org-test-with-parsed-data
       "- Item1\n  - Item11\n  - <<test>>Item12\n- Item2\n\n\n[[test]]"
@@ -1227,10 +1227,10 @@ Paragraph[fn:1]"
      ;; Note: Item's number is in fact a list of numbers.
      (equal '(1 2)
 	    (org-element-map
-	     tree 'link
-	     (lambda (link)
-	       (org-export-get-ordinal
-		(org-export-resolve-fuzzy-link link info) info)) info t))))
+		tree 'link
+	      (lambda (link)
+		(org-export-get-ordinal
+		 (org-export-resolve-fuzzy-link link info) info)) info t))))
   ;; 4. Link to a target in a footnote should return footnote's
   ;;    number.
   (org-test-with-parsed-data "
@@ -1238,10 +1238,10 @@ Paragraph[1][2][fn:lbl3:C<<target>>][[test]][[target]]\n[1] A\n\n[2] <<test>>B"
     (should
      (equal '(2 3)
 	    (org-element-map
-	     tree 'link
-	     (lambda (link)
-	       (org-export-get-ordinal
-		(org-export-resolve-fuzzy-link link info) info)) info))))
+		tree 'link
+	      (lambda (link)
+		(org-export-get-ordinal
+		 (org-export-resolve-fuzzy-link link info) info)) info))))
   ;; 5. Link to a named element should return sequence number of that
   ;;    element.
   (org-test-with-parsed-data
@@ -1249,10 +1249,10 @@ Paragraph[1][2][fn:lbl3:C<<target>>][[test]][[target]]\n[1] A\n\n[2] <<test>>B"
     (should
      (= 2
 	(org-element-map
-	 tree 'link
-	 (lambda (link)
-	   (org-export-get-ordinal
-	    (org-export-resolve-fuzzy-link link info) info)) info t))))
+	    tree 'link
+	  (lambda (link)
+	    (org-export-get-ordinal
+	     (org-export-resolve-fuzzy-link link info) info)) info t))))
   ;; 6. Link to a target not within an item, a table, a footnote
   ;;    reference or definition should return section number.
   (org-test-with-parsed-data
@@ -1260,10 +1260,16 @@ Paragraph[1][2][fn:lbl3:C<<target>>][[test]][[target]]\n[1] A\n\n[2] <<test>>B"
     (should
      (equal '(2)
 	    (org-element-map
-	     tree 'link
-	     (lambda (link)
-	       (org-export-get-ordinal
-		(org-export-resolve-fuzzy-link link info) info)) info t)))))
+		tree 'link
+	      (lambda (link)
+		(org-export-get-ordinal
+		 (org-export-resolve-fuzzy-link link info) info)) info t))))
+  ;; 7. Space are not significant when matching a fuzzy link.
+  (should
+   (org-test-with-parsed-data "* Head 1\n[[Head\n  1]]"
+     (org-element-map tree 'link
+       (lambda (link) (org-export-resolve-fuzzy-link link info))
+       info t))))
 
 (ert-deftest test-org-export/resolve-coderef ()
   "Test `org-export-resolve-coderef' specifications."