Small improvement of maintainability of link escaping

* lisp/org.el (org-link-escape-chars): Add comment with escape numbers
alphabetically ordered.
(org-link-escape-chars-browser): Add comment with escape numbers.
(org-link-escape): Use better readable char constant instead of number
constant for percent char.
(org-link-escape-browser): New function to substitute duplicate source
code.
(org-open-at-point): Substitute duplicate source code.
* testing/README: Make comment in source code example clearer.
* testing/lisp/test-org.el (test-org/org-link-escape-chars-browser):
Change URL to real use case and use the new function
`org-link-escape-browser'.
This commit is contained in:
Michael Brand 2013-10-05 15:00:25 +02:00 committed by Carsten Dominik
parent 4bf9a53cde
commit e04a22ed2d
3 changed files with 21 additions and 23 deletions

View File

@ -9732,11 +9732,13 @@ according to FMT (default from `org-email-link-description-format')."
"]")) "]"))
(defconst org-link-escape-chars (defconst org-link-escape-chars
'(?\ ?\[ ?\] ?\; ?\= ?\+) ;;%20 %2B %3B %3D %5B %5D
'(?\ ?\+ ?\; ?\= ?\[ ?\])
"List of characters that should be escaped in link. "List of characters that should be escaped in link.
This is the list that is used for internal purposes.") This is the list that is used for internal purposes.")
(defconst org-link-escape-chars-browser (defconst org-link-escape-chars-browser
;;%20 %22
'(?\ ?\") '(?\ ?\")
"List of escapes for characters that are problematic in links. "List of escapes for characters that are problematic in links.
This is the list that is used before handing over to the browser.") This is the list that is used before handing over to the browser.")
@ -9758,7 +9760,7 @@ If optional argument MERGE is set, merge TABLE into
(mapconcat (mapconcat
(lambda (char) (lambda (char)
(if (or (member char table) (if (or (member char table)
(and (or (< char 32) (= char 37) (> char 126)) (and (or (< char 32) (= char ?\%) (> char 126))
org-url-hexify-p)) org-url-hexify-p))
(mapconcat (lambda (sequence-element) (mapconcat (lambda (sequence-element)
(format "%%%.2X" sequence-element)) (format "%%%.2X" sequence-element))
@ -9767,6 +9769,13 @@ If optional argument MERGE is set, merge TABLE into
(char-to-string char))) "") (char-to-string char))) "")
(char-to-string char))) text "")) (char-to-string char))) text ""))
(defun org-link-escape-browser (text)
(if (org-string-match-p
(concat "[[:nonascii:]" org-link-escape-chars-browser "]")
text)
(org-link-escape text org-link-escape-chars-browser)
text))
(defun org-link-unescape (str) (defun org-link-unescape (str)
"Unhex hexified Unicode strings as returned from the JavaScript function "Unhex hexified Unicode strings as returned from the JavaScript function
encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut." encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
@ -10467,24 +10476,11 @@ application the system uses for this file type."
(apply cmd (nreverse args1)))) (apply cmd (nreverse args1))))
((member type '("http" "https" "ftp" "news")) ((member type '("http" "https" "ftp" "news"))
(browse-url (browse-url (concat type ":" (org-link-escape-browser path))))
(concat type ":"
(if (org-string-match-p
(concat "[[:nonascii:]"
org-link-escape-chars-browser "]")
path)
(org-link-escape path org-link-escape-chars-browser)
path))))
((string= type "doi") ((string= type "doi")
(browse-url (browse-url (concat org-doi-server-url
(concat org-doi-server-url (org-link-escape-browser path))))
(if (org-string-match-p
(concat "[[:nonascii:]"
org-link-escape-chars-browser "]")
path)
(org-link-escape path org-link-escape-chars-browser)
path))))
((member type '("message")) ((member type '("message"))
(browse-url (concat type ":" path))) (browse-url (concat type ":" path)))

View File

@ -64,9 +64,9 @@ load and run the test suite with the following commands.
Use this as a demo example of a failing test Use this as a demo example of a failing test
#+BEGIN_SRC emacs-lisp #+BEGIN_SRC emacs-lisp
(ert-deftest test-org/org-link-escape-ascii-character-demo-of-fail () (ert-deftest test-org/org-link-escape-ascii-character-demo-of-fail ()
(should (string= "%5B" ;; expected is right (should (string= "%5B" ;; expecting %5B is right
(org-link-escape "["))) (org-link-escape "[")))
(should (string= "%5C" ;; expected is wrong, "%5D" would be right (should (string= "%5C" ;; expecting %5C is wrong, %5D right
(org-link-escape "]")))) (org-link-escape "]"))))
#+END_SRC #+END_SRC
or evaluate the ert-deftest form of the test you want to run. Then or evaluate the ert-deftest form of the test you want to run. Then

View File

@ -524,9 +524,11 @@ http://article.gmane.org/gmane.emacs.orgmode/21459/"
"Escape a URL to pass to `browse-url'." "Escape a URL to pass to `browse-url'."
(should (should
(string= (string=
"http://some.host.com/search?q=%22Org%20mode%22" (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
(org-link-escape "http://some.host.com/search?q=\"Org mode\"" "%22Release%208.2%22&idxname=emacs-orgmode")
org-link-escape-chars-browser)))) (org-link-escape-browser
(concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query="
"\"Release 8.2\"&idxname=emacs-orgmode")))))