From c05263f8cad09b9bf13b03ad9198c40400f31483 Mon Sep 17 00:00:00 2001 From: Federico Tedin Date: Mon, 21 Oct 2024 21:14:44 +0200 Subject: [PATCH] Parse Org hyperlinks correctly --- CHANGELOG.md | 1 + test/data/test.org | 3 +++ test/verb-test.el | 21 ++++++++++++++++++++- verb-util.el | 10 ++++++++++ verb.el | 10 +++++++--- 5 files changed, 41 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ef0dcf2..3353f49 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/test/data/test.org b/test/data/test.org index e552e66..4203abb 100644 --- a/test/data/test.org +++ b/test/data/test.org @@ -8,6 +8,9 @@ template http://localhost:8000 +** org-hyperlink +get [[http://localhost:8000/basic][some hyperlink description]] + ** basic get /basic diff --git a/test/verb-test.el b/test/verb-test.el index b7cf2d6..025d714 100644 --- a/test/verb-test.el +++ b/test/verb-test.el @@ -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 @@ -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 diff --git a/verb-util.el b/verb-util.el index 201a01f..3006948 100644 --- a/verb-util.el +++ b/verb-util.el @@ -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. @@ -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 diff --git a/verb.el b/verb.el index 772ade0..cb45e4c 100644 --- a/verb.el +++ b/verb.el @@ -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))