emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: "Daniel P. Gomez" <gomez.danp@gmail.com>
To: Nicolas Goaziou <mail@nicolasgoaziou.fr>
Cc: emacs-orgmode@gnu.org
Subject: Re: How to keep correct filepaths when using the #+INCLUDE derivative?
Date: Sat, 10 Mar 2018 12:28:39 +0100	[thread overview]
Message-ID: <m2d10c2mqg.fsf@gmail.com> (raw)
In-Reply-To: <87y3j537uu.fsf@nicolasgoaziou.fr>

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


I've fixed the implementation of 
`org-export--prepare-file-contents` so
that links are relative to the included file. A patch is attached.

I've also created two org files in the test examples directory:
"includer-with-links.org" and "subdir/includee-with-links.org". My 
goal
was to add a test in "test-ox.el" `test-org-export/expand-include` 
that
checks whether, after exporting the "includer-with-links.org" file 
to
Org, the headings *Source and *Target are the same.

I did not figure out how to do this cleanly, so any help 
implementing
the test itself would be greatly appreciated.

Regards,

Daniel


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Fix-file-links-when-using-INCLUDE.patch --]
[-- Type: text/x-patch, Size: 4560 bytes --]

From b47dcf43067cd57e2ee3c1f8e4dfea94bca7d14b Mon Sep 17 00:00:00 2001
From: Daniel Gomez <d.gomez@posteo.org>
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


[-- Attachment #3: Type: text/plain, Size: 1896 bytes --]




Nicolas Goaziou writes:

> Hello,
>
> Daniel P Gomez <gomez.danp@gmail.com> writes:
>
>> I noticed that (buffer-file-name (buffer-base-buffer)) will 
>> always
>> return nil because `org-export--prepare-file-contents` is 
>> called in
>> `org-export-include-keyword` after a call to 
>> `with-temp-buffer`. Two
>> possible solutions to this issue would be either 1. passing the
>> includer-file as an extra parameter to
>> `org-export--prepare-file-contents` and then using
>> `file-relative-name` to generate a relative path, or 
>> alternatively 2 .
>> passing the matched string that points to the file to be 
>> included.
>> Example:
>>
>> #+INCLUDE: "directory/file.org"
>>
>> Here, if file.org contains a link [[other/image.png]], then all 
>> one
>> has to do is append the (file-name-directory matched) to the 
>> old-path.
>> In this example this would result in directory/other/image.png.
>>
>> This second solution does not require a call to 
>> (buffer-file-name
>> (buffer-base-buffer)), but seems hackish in the sense that we 
>> would
>> pass 2 redundant arguments to 
>> `org-export-prepare-file-contents`: both
>> the expanded and the non-expanded include-file filename.
>> Perhaps I'm missing a simpler 3rd solution?
>
> I think solution 1 is fine.
>
>> If we opt for solution 1 then new-path could be made relative 
>> here
>>>                  ;; (org-element-put-property new :path 
>>>                  new-path)
>>
>> (org-element-put-property
>>                new :path
>>                (if includer-file
>>                (file-relative-name
>>                 new-path (file-name-directory includer-file))
>>              new-path))
>
> Indeed. However, the (if includer-file ...) should be integrated 
> in
> new-path binding, IMO.
>
>> I will attempt to write them once the implementation is 
>> completed.
>
> Great. Thank you!
>
> Regards,


--
Daniel P. Gomez

  reply	other threads:[~2018-03-10 11:28 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-28 13:29 How to keep correct filepaths when using the #+INCLUDE derivative? Daniel P Gomez
2018-02-28 17:51 ` Nicolas Goaziou
2018-02-28 20:11   ` Daniel P Gomez
2018-03-01  1:01     ` Daniel P Gomez
2018-03-01 18:32       ` Nicolas Goaziou
2018-03-01 18:56         ` Daniel P Gomez
2018-03-01 22:42           ` Nicolas Goaziou
2018-03-02 14:16             ` Daniel P Gomez
2018-03-03  1:24               ` Nicolas Goaziou
2018-03-03 13:06                 ` Daniel P Gomez
2018-03-06  8:51                   ` Nicolas Goaziou
2018-03-10 11:28                     ` Daniel P. Gomez [this message]
2018-03-18 15:15                       ` Nicolas Goaziou

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m2d10c2mqg.fsf@gmail.com \
    --to=gomez.danp@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    --cc=mail@nicolasgoaziou.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public 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).