>From b47dcf43067cd57e2ee3c1f8e4dfea94bca7d14b Mon Sep 17 00:00:00 2001 From: Daniel Gomez Date: Fri, 2 Mar 2018 14:46:41 +0100 Subject: [PATCH 1/1] Fix file links when using #+INCLUDE --- lisp/ox.el | 36 ++++++++++++++++++++++--- testing/examples/includer-with-links.org | 19 +++++++++++++ testing/examples/subdir/includee-with-links.org | 12 +++++++++ 3 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 testing/examples/includer-with-links.org create mode 100644 testing/examples/subdir/includee-with-links.org diff --git a/lisp/ox.el b/lisp/ox.el index bd49a8a26..f9b2d8095 100644 --- a/lisp/ox.el +++ b/lisp/ox.el @@ -3257,7 +3257,8 @@ avoid infinite recursion. Optional argument DIR is the current working directory. It is used to properly resolve relative paths. Optional argument FOOTNOTES is a hash-table used for storing and resolving footnotes. It is created automatically." - (let ((case-fold-search t) + (let ((includer-file (buffer-file-name (buffer-base-buffer))) + (case-fold-search t) (file-prefix (make-hash-table :test #'equal)) (current-prefix 0) (footnotes (or footnotes (make-hash-table :test #'equal))) @@ -3373,7 +3374,8 @@ storing and resolving footnotes. It is created automatically." (or (gethash file file-prefix) (puthash file (cl-incf current-prefix) file-prefix)) - footnotes))) + footnotes + includer-file))) (org-export-expand-include-keyword (cons (list file lines) included) (file-name-directory file) @@ -3451,7 +3453,7 @@ Return a string of lines to be included in the format expected by counter)))))))) (defun org-export--prepare-file-contents - (file &optional lines ind minlevel id footnotes) + (file &optional lines ind minlevel id footnotes includer-file) "Prepare contents of FILE for inclusion and return it as a string. When optional argument LINES is a string specifying a range of @@ -3476,6 +3478,34 @@ Optional argument FOOTNOTES is a hash-table to store footnotes in the included document." (with-temp-buffer (insert-file-contents file) + ;; Adapt all file links within the included document that + ;; contain relative paths in order to make these paths + ;; relative to the base document, or absolute + (goto-char (point-min)) + (while (re-search-forward org-any-link-re nil t) + (let ((link (save-excursion + (backward-char) + (org-element-context)))) + (when (string= "file" (org-element-property :type link)) + (let* ((old-path (org-element-property :path link)) + (new-path (let ((abspath (expand-file-name + old-path + (file-name-directory file)))) + (if includer-file + (file-relative-name + abspath (file-name-directory includer-file)) + abspath)))) + (insert (let ((new (org-element-copy link))) + (org-element-put-property new :path new-path) + (when (org-element-property :contents-begin link) + (org-element-adopt-elements + new + (buffer-substring + (org-element-property :contents-begin link) + (org-element-property :contents-end link)))) + (delete-region (org-element-property :begin link) + (org-element-property :end link)) + (org-element-interpret-data new))))))) (when lines (let* ((lines (split-string lines "-")) (lbeg (string-to-number (car lines))) diff --git a/testing/examples/includer-with-links.org b/testing/examples/includer-with-links.org new file mode 100644 index 000000000..f97b76375 --- /dev/null +++ b/testing/examples/includer-with-links.org @@ -0,0 +1,19 @@ +* Source + +#+INCLUDE: "./subdir/includee-with-links.org::*Links" :only-contents t + + +* Target + +File link with description +[[file:subdir/setupfile2.org][A setupfile]] + +File link pointing to a specific header +[[file:normal.org::top]] + +Bracketed file link without description +[[file:subdir/setupfile.org]] + +Plain file link +file:subdir/setupfile.org + diff --git a/testing/examples/subdir/includee-with-links.org b/testing/examples/subdir/includee-with-links.org new file mode 100644 index 000000000..1f734ab86 --- /dev/null +++ b/testing/examples/subdir/includee-with-links.org @@ -0,0 +1,12 @@ +* Links +File link with description +[[file:setupfile2.org][A setupfile]] + +File link pointing to a specific header +[[file:../normal.org::top]] + +Bracketed file link without description +[[file:setupfile.org]] + +Plain file link +file:setupfile.org -- 2.16.2