Browse Source

org-lint: Add checker for obsolete link escape syntax

* lisp/org-lint.el (org-lint--checkers): Add new linter.
(org-lint-percent-encoding-link-escape): New function.
* testing/lisp/test-org-lint.el (test-org-lint/percenc-encoding-link-escape):
  New test.
Nicolas Goaziou 6 years ago
parent
commit
c04372dbfe
2 changed files with 51 additions and 1 deletions
  1. 24 1
      lisp/org-lint.el
  2. 27 0
      testing/lisp/test-org-lint.el

+ 24 - 1
lisp/org-lint.el

@@ -288,9 +288,15 @@
     :name 'file-application
     :description "Report obsolete \"file+application\" link"
     :categories '(link obsolete))
+   (make-org-lint-checker
+    :name 'percent-encoding-link-escape
+    :description "Report obsolete escape syntax in links"
+    :categories '(link obsolete)
+    :trust 'low)
    (make-org-lint-checker
     :name 'spurious-colons
-    :description "Report spurious colons in tags"))
+    :description "Report spurious colons in tags"
+    :categories '(tags)))
   "List of all available checkers.")
 
 (defun org-lint--collect-duplicates
@@ -884,6 +890,23 @@ Use \"export %s\" instead"
 	     (list (org-element-property :begin l)
 		   (format "Deprecated \"file+%s\" link type" app)))))))
 
+(defun org-lint-percent-encoding-link-escape (ast)
+  (org-element-map ast 'link
+    (lambda (l)
+      (when (eq 'bracket (org-element-property :format l))
+	(let* ((uri (org-element-property :path l))
+	       (start 0)
+	       (obsolete-flag
+		(catch :obsolete
+		  (while (string-match "%\\(..\\)?" uri start)
+		    (setq start (match-end 0))
+		    (unless (member (match-string 1 uri) '("25" "5B" "5D" "20"))
+		      (throw :obsolete nil)))
+		  (string-match-p "%" uri))))
+	  (when obsolete-flag
+	    (list (org-element-property :begin l)
+		  "Link escaped with obsolete percent-encoding syntax")))))))
+
 (defun org-lint-wrong-header-argument (ast)
   (let* ((reports)
 	 (verify

+ 27 - 0
testing/lisp/test-org-lint.el

@@ -415,6 +415,33 @@ SCHEDULED: <2012-03-29 thu.>"
    (org-test-with-temp-text "[[file+emacs:foo.org]]"
      (org-lint '(file-application)))))
 
+(ert-deftest test-org-lint/percenc-encoding-link-escape ()
+  "Test `org-lint-percent-encoding-link-escape' checker."
+  (should
+   (org-test-with-temp-text "[[A%20B]]"
+     (org-lint '(percent-encoding-link-escape))))
+  (should
+   (org-test-with-temp-text "[[%5Bfoo%5D]]"
+     (org-lint '(percent-encoding-link-escape))))
+  (should
+   (org-test-with-temp-text "[[A%2520B]]"
+     (org-lint '(percent-encoding-link-escape))))
+  (should-not
+   (org-test-with-temp-text "[[A B]]"
+     (org-lint '(percent-encoding-link-escape))))
+  (should-not
+   (org-test-with-temp-text "[[A%30B]]"
+     (org-lint '(percent-encoding-link-escape))))
+  (should-not
+   (org-test-with-temp-text "[[A%20%30B]]"
+     (org-lint '(percent-encoding-link-escape))))
+  (should-not
+   (org-test-with-temp-text "<file:A%20B>"
+     (org-lint '(percent-encoding-link-escape))))
+  (should-not
+   (org-test-with-temp-text "[[A B%]]"
+     (org-lint '(percent-encoding-link-escape)))))
+
 (ert-deftest test-org-lint/wrong-header-argument ()
   "Test `org-lint-wrong-header-argument' checker."
   (should