From 2e2189c451c8e8bbd8461a626fe63e5e97dd4a07 Mon Sep 17 00:00:00 2001 From: TEC Date: Sun, 5 Jun 2022 22:25:22 +0800 Subject: [PATCH 1/2] ox: Support #+include-ing URLs * lisp/ox.el (org-export--prepare-file-contents, org-export--inclusion-absolute-lines): Replace instances of `(insert-file-contents FILE)' with `(insert (org-file-contents FILE))', as in `org--collect-keywords-1'. (org-export-expand-include-keyword): Tweak to accept a URL as FILE, and not perform the standard "file exists and is readable" check. * etc/ORG-NEWS: Mention this change in behaviour. --- etc/ORG-NEWS | 3 +++ lisp/ox.el | 22 +++++++++++++--------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS index 35af94f92..397cb668c 100644 --- a/etc/ORG-NEWS +++ b/etc/ORG-NEWS @@ -219,6 +219,9 @@ blocks to LaTeX. This requires the =fvextra=, =float=, and (by default, but not necessarily) =tcolorbox= LaTeX packages be installed. It uses Emacs' font-lock information, and so tends to produce results superior to Minted or Listings. +*** Support for =#+include=-ing URLs + +=#+include: FILE= will now accept URLs as the file. ** New functions and changes in function arguments diff --git a/lisp/ox.el b/lisp/ox.el index 9a8e63046..fc0ee671f 100644 --- a/lisp/ox.el +++ b/lisp/ox.el @@ -3229,15 +3229,18 @@ (defun org-export-expand-include-keyword (&optional included dir footnotes) value) (prog1 (save-match-data - (let ((matched (match-string 1 value))) + (let ((matched (match-string 1 value)) + stripped) (when (string-match "\\(::\\(.*?\\)\\)\"?\\'" matched) (setq location (match-string 2 matched)) (setq matched (replace-match "" nil nil matched 1))) - (expand-file-name (org-strip-quotes matched) - dir))) - (setq value (replace-match "" nil nil value))))) + (setq stripped (org-strip-quotes matched)) + (if (org-url-p stripped) + stripped + (expand-file-name stripped dir)))) + (setq value (replace-match "" nil nil value))))) (only-contents (and (string-match ":only-contents *\\([^: \r\t\n]\\S-*\\)?" value) @@ -3273,7 +3276,7 @@ (defun org-export-expand-include-keyword (&optional included dir footnotes) (delete-region (point) (line-beginning-position 2)) (cond ((not file) nil) - ((not (file-readable-p file)) + ((and (not (org-url-p file)) (not (file-readable-p file))) (error "Cannot include file %s" file)) ;; Check if files has already been parsed. Look after ;; inclusion lines too, as different parts of the same @@ -3319,8 +3322,9 @@ (defun org-export-expand-include-keyword (&optional included dir footnotes) includer-file))) (org-export-expand-include-keyword (cons (list file lines) included) - (file-name-directory file) - footnotes) + (unless (org-url-p file) + (file-name-directory file)) + footnotes) (buffer-string))))) ;; Expand footnotes after all files have been ;; included. Footnotes are stored at end of buffer. @@ -3343,7 +3347,7 @@ (defun org-export--inclusion-absolute-lines (file location only-contents lines) Return a string of lines to be included in the format expected by `org-export--prepare-file-contents'." (with-temp-buffer - (insert-file-contents file) + (insert (org-file-contents file)) (unless (eq major-mode 'org-mode) (let ((org-inhibit-startup t)) (org-mode))) (condition-case err @@ -3448,7 +3452,7 @@ (defun org-export--prepare-file-contents Optional argument INCLUDER is the file name where the inclusion is to happen." (with-temp-buffer - (insert-file-contents file) + (insert (org-file-contents file)) (when lines (let* ((lines (split-string lines "-")) (lbeg (string-to-number (car lines))) -- 2.36.1