Reputation: 10573
Is it possible to copy a text with hyperlink from external files (e.g., word(.doc) file) to Emacs with hyperlink preserved? it could be very annoying if I copy an articles with 100 hyperlinks from word to Emacs and have to re-input each hyperlink
Such function seems to be unavailable from the vanilla Emacs, and I am pretty new to Emacs, so I wish somebody here can come up with a simple function to enable that.
thanks
Upvotes: 3
Views: 1891
Reputation: 141
I researched the binary content of clipboard on macos by using
osascript -e 'the clipboard as "HTML"'
copy text with hyperlink from excel to org mode file can be done.
(defun decode-hex-to-string (hex-string)
"Decode a hexadecimal string to its original content."
(apply 'concat
(mapcar (lambda (pair)
(string (string-to-number pair 16)))
(seq-partition hex-string 2))))
(defun extract-hyperlink-from-html (html)
"Extract the hyperlink and text from HTML content."
(if (string-match "<a href=\"\\([^\"]+\\)\"[^>]*>\\([^<]+\\)</a>" html)
(let ((url (match-string 1 html))
(text (match-string 2 html)))
(format "[[%s][%s]]" url text))
html))
(defun copy-text-with-hyperlink-to-org-file ()
"Decode the clipboard content from hexadecimal to HTML and insert it into the current buffer."
(interactive)
(let* ((osascript-cmd "osascript -e 'the clipboard as \"HTML\"'")
(hex-string (shell-command-to-string osascript-cmd))
(decoded-html (decode-hex-to-string (string-trim hex-string)))
(org-link (extract-hyperlink-from-html decoded-html)))
(insert org-link)))
Upvotes: 1
Reputation: 4359
In order for the hyperlink to work (meaning the actual hyperlink is hidden and the word "here" is emphasized/highlighted and is "clickable" by either mouse or keyboard shortcut and/or M-x function call) in emacs you need to use one of the modes which support such a behavior:
Org mode which is a major mode and a very substantial package with a ton of functionality. Create a file with .org
extension, copy the link's url there and it becomes "live". You can edit it to hide the url by clicking C-cC-l while your cursor is on the link, then confirm the url and add description - the "here" word. After that the url becomes hidden and "here" becomes highlighted and clickable.
One of the Wiki modes, there are both major and minor modes (so you can try using the functionality in files having various major modes). Please consult the link for details.
If you would rather not use additional packages, here is the documentation on how to make arbitrary text clickable but you would need to be comfortable with some elisp programming.
UPDATE:
I see that you need the bulk import of urs to emacs. The problem is with incomplete support of the multi-format clipboard content (which is created, for instance, when you copying from a web browser) in emacs. According to http://www.mail-archive.com/[email protected]/msg03026.html there is no support for HTML content so it gets pasted to emacs as plain text. The only way I see is to open the page source in the browser, save it to file, extract list of urls (and surrounding text if you needed it) using xsltproc or whatever and convert the urls to org-mode style links (also with xsltproc or emacs regex search/replace). Unfortunately there is no html-to-org converter that I know of.
Upvotes: 3
Reputation: 20342
http://orgmode.org/worg/org-contrib/org-protocol.html should do the job. And it's able to do a lot more.
Upvotes: 0