emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [PATCH] Remote link localisation, i.e. exporting remote images
@ 2022-01-29 16:00 Timothy
  2022-01-31 11:40 ` Timothy
  0 siblings, 1 reply; 2+ messages in thread
From: Timothy @ 2022-01-29 16:00 UTC (permalink / raw)
  To: orgmode

[-- Attachment #1: Type: text/plain, Size: 583 bytes --]

Hi All,

Following Ihor’s recent org-persist improvements, I’ve prepared a patch making
use of it to allow for remote resource links to be transformed to local file
links during export.

This feature is implemented in ox.el, and made use of in ox-latex.el (for now)
— you can now link to https and tramp files and it should /just work/™.

Here’s a demo of what you can expect:
<file:/tmp/image-link-localisation-demo.png>

The patch is attached. If there aren’t any objections raised, I plan to merge it
shortly (in a day or two).

All the best,
Timothy

[-- Attachment #2: image-link-localisation-demo.png --]
[-- Type: image/png, Size: 133357 bytes --]

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #3: 0001-ox-Add-link-localisation-feature-using-persist.patch --]
[-- Type: text/x-patch, Size: 4277 bytes --]

From 721db5de07ca667acde997c621cf62b557f9dc3b Mon Sep 17 00:00:00 2001
From: TEC <tec@tecosaur.com>
Date: Sat, 29 Jan 2022 23:49:52 +0800
Subject: [PATCH] ox: Add link localisation feature using persist

* lisp/ox.el (org-export-link-remote-p): A new function to determine
whether a link refers to a remote resource.
(org-export-link--remote-local-copy): Download and return the local
location of a remote resource link.
(org-export-link-localise): Transform remote links to refer to a local
copy of the resource.

* lisp/ox-latex.el (org-latex-link, org-latex-inline-image-rules): Make
use of the new functions for remote resources in ox.el to support
embedding https and tramp -linked files.
---
 lisp/ox-latex.el |  9 ++++++---
 lisp/ox.el       | 41 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+), 3 deletions(-)

diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 58252c6b8..5dda9b3ab 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -755,8 +755,11 @@ (defcustom org-latex-default-figure-position "htbp"
 
 (defcustom org-latex-inline-image-rules
   `(("file" . ,(rx "."
-		   (or "pdf" "jpeg" "jpg" "png" "ps" "eps" "tikz" "pgf" "svg")
-		   eos)))
+                   (or "pdf" "jpeg" "jpg" "png" "ps" "eps" "tikz" "pgf" "svg")
+                   eos))
+    ("https" . ,(rx "."
+                    (or "jpeg" "jpg" "png" "ps" "eps" "tikz" "pgf" "svg")
+                    eos)))
   "Rules characterizing image files that can be inlined into LaTeX.
 
 A rule consists in an association whose key is the type of link
@@ -2588,7 +2591,7 @@ (defun org-latex-link (link desc info)
      ;; Link type is handled by a special function.
      ((org-export-custom-protocol-maybe link desc 'latex info))
      ;; Image file.
-     (imagep (org-latex--inline-image link info))
+     (imagep (org-latex--inline-image (org-export-link-localise link) info))
      ;; Radio link: Transcode target's contents and use them as link's
      ;; description.
      ((string= type "radio")
diff --git a/lisp/ox.el b/lisp/ox.el
index 831b3bf12..a258ed57f 100644
--- a/lisp/ox.el
+++ b/lisp/ox.el
@@ -4446,6 +4446,47 @@ (defun org-export-file-uri (filename)
 	   (concat (if (string-prefix-p "/" fullname) "file://" "file:///")
 		   fullname)))))
 
+(defun org-export-link-remote-p (link)
+  "Returns non-nil if the link refers to a remote resource."
+  (or (member (org-element-property :type link) '("http" "https" "ftp"))
+      (and (string= (org-element-property :type link) "file")
+           (file-remote-p (org-element-property :path link)))))
+
+(defun org-export-link--remote-local-copy (link)
+  "Download the remote resource specified by LINK, and return its local path."
+  ;; TODO work this into ol.el as a link parameter, say :download.
+  (let* ((location-type
+          (pcase (org-element-property :type link)
+            ((or "http" "https" "ftp") 'url)
+            ((and "file" (guard (file-remote-p
+                                 (org-element-property :path link))))
+             'file)
+            (_ (error "Cannot copy %s:%s to a local file"
+                      (org-element-property :type link)
+                      (org-element-property :path link)))))
+         (path
+          (pcase location-type
+            ('url
+             (concat (org-element-property :type link)
+                     ":" (org-element-property :path link)))
+            ('file
+             (org-element-property :path link)))))
+    (or (org-persist-read location-type path)
+        (org-persist-register location-type path
+                              :write-immediately t))))
+
+(defun org-export-link-localise (link)
+  "If LINK refers to a remote resource, modify it to point to a local downloaded copy."
+  (when (org-export-link-remote-p link)
+    (let* ((local-path (org-export-link--remote-local-copy link)))
+      (setcdr link
+              (thread-first (cadr link)
+                            (plist-put :type "file")
+                            (plist-put :path local-path)
+                            (plist-put :raw-link (concat "file:" local-path))
+                            list))))
+  link)
+
 ;;;; For References
 ;;
 ;; `org-export-get-reference' associate a unique reference for any
-- 
2.34.1


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] Remote link localisation, i.e. exporting remote images
  2022-01-29 16:00 [PATCH] Remote link localisation, i.e. exporting remote images Timothy
@ 2022-01-31 11:40 ` Timothy
  0 siblings, 0 replies; 2+ messages in thread
From: Timothy @ 2022-01-31 11:40 UTC (permalink / raw)
  To: orgmode

[-- Attachment #1: Type: text/plain, Size: 39 bytes --]

I’ve just pushed this as 6ee4551 :)

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-01-31 12:18 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-29 16:00 [PATCH] Remote link localisation, i.e. exporting remote images Timothy
2022-01-31 11:40 ` Timothy

Code repositories for project(s) associated with this inbox:

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).