Skip to content

Commit

Permalink
Parse Org hyperlinks correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
federicotdn committed Oct 21, 2024
1 parent 98934fe commit c05263f
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ New features / improvements:
- Added new function `verb-body-lf-to-crlf` designed for use with requests sending multipart data.
- Added new `Verb-Prelude` heading property, which can be used to specify an Emacs Lisp or JSON file to load variables from, before performing requests.
- Added `verb-shell` and `verb-unix-epoch` utility functions.
- Allow using Org [hyperlinks](https://orgmode.org/guide/Hyperlinks.html) in URLs, for example: `get [[http://example.com][my example]]`.

## **2.16.0** - 2024-03-02 (MELPA Stable)
- Fixed LF being used instead of CRLF in multipart boundaries.
Expand Down
3 changes: 3 additions & 0 deletions test/data/test.org
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

template http://localhost:8000

** org-hyperlink
get [[http://localhost:8000/basic][some hyperlink description]]

** basic
get /basic

Expand Down
21 changes: 20 additions & 1 deletion test/verb-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@
;;; Code:

(require 'ert-x)
(require 'cl-lib)

(require 'verb)
(require 'ob-verb)
(require 'cl-lib)

(org-babel-do-load-languages
'org-babel-load-languages
Expand Down Expand Up @@ -2989,5 +2990,23 @@

(should-not url-proxy-services))

(ert-deftest test-verb-util-remove-org-hyperlinks ()
(dolist (elem '(("" . "")
("foo" . "foo")
("[[]]" . "[[]]")
("[[foo]]" . "foo")
("[foo]" . "[foo]")
("[[foo]" . "[[foo]")
("[[foo] ]" . "[[foo] ]")
("[[https://example.com?a=b]]" . "https://example.com?a=b")
("[[https://example.com?a=b][test]]" . "https://example.com?a=b")
("[[https://example.com?a=b][test with space]]" . "https://example.com?a=b")
("[[https://example.com?a=b][]]" . "https://example.com?a=b")))
(should (string= (cdr elem) (verb-util--remove-org-hyperlinks (car elem))))))

(ert-deftest test-server-remove-org-hyperlinks ()
(server-test "org-hyperlink"
(should (string= (buffer-string) "Hello, World!"))))

(provide 'verb-test)
;;; verb-test.el ends here
10 changes: 10 additions & 0 deletions verb-util.el
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ E = Error.")
"^\\s-*\\([[:alnum:]_-]+\\)\\s-*:\\(.*\\)$"
"Regexp for parsing HTTP headers.")

(defconst verb-util--org-hyperlink-regexp
"\\[\\[\\(.+?\\)\\]\\(\\[.*?\\]\\)?\\]"
"Regexp for parsing Org mode hyperlinks.")

(define-derived-mode verb-util-log-mode special-mode "Verb[Log]"
"Major mode for displaying Verb logs.
Expand Down Expand Up @@ -160,5 +164,11 @@ interval covering the whole object, with no properties."
default)
default)))

(defun verb-util--remove-org-hyperlinks (s)
"Remove Org hyperlinks from string S and return the result.
All hyperlinks are replaced with the link they contain."
(replace-regexp-in-string verb-util--org-hyperlink-regexp
"\\1" s t))

(provide 'verb-util)
;;; verb-util.el ends here
10 changes: 7 additions & 3 deletions verb.el
Original file line number Diff line number Diff line change
Expand Up @@ -2523,11 +2523,15 @@ Additionally, given a URL like \"http://foo.com?a=b\", return
\"http://foo.com/?a=b\". This is what curl does when the path is empty
and there are query string arguments present.
Also, replace any present Org hyperlinks with their literal link
contents, using `verb-util--remove-org-hyperlinks'.
If a scheme is not present, interpret the URL as a path, query string
and fragment component of a URL with no host or scheme defined."
;; If we're not expanding code tags, do not attempt to encode '{',
;; '}', etc., so that we keep the original URL text.
(let* ((encoded-url (if verb--inhibit-code-tags-evaluation
(let* ((url (verb-util--remove-org-hyperlinks url))
;; If we're not expanding code tags, do not attempt to encode
;; '{', '}', etc., so that we keep the original URL text.
(encoded-url (if verb--inhibit-code-tags-evaluation
url
(url-encode-url url)))
(url-obj (url-generic-parse-url encoded-url))
Expand Down

0 comments on commit c05263f

Please sign in to comment.