From 721db5de07ca667acde997c621cf62b557f9dc3b Mon Sep 17 00:00:00 2001 From: TEC 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