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: Thu, 1 Mar 2018 02:01:13 +0100	[thread overview]
Message-ID: <CAJTMgxg+Tw8rfcDsL29wTv3GXsJhYkZmGXdEH6JDgLYv-ky_OQ@mail.gmail.com> (raw)
In-Reply-To: <CAJTMgxjMJ1g-vQsK2vMJyvAu7bra+4nOhhqLZ3GQ6MQhCQaxHw@mail.gmail.com>

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

If there is any interest, I've created a patch adding support for a
:absolute-paths functionality.
As I said, I don't have much experience writing Elisp, so any
suggestions on how to improve the code would be appreciated. If there
is any interest in adding this feature to Org I could try to clean it
up.

Patch attached.


On Wed, Feb 28, 2018 at 9:11 PM, Daniel P Gomez <gomez.danp@gmail.com> wrote:
> Sorry for the ignorance, but where could I look up how to use a parse
> tree filter? And where would these modifications make sense?
> Any pointers towards documentation, functions, or any help of any kind
> would be appreciated. I must say I am a bit lost.
>
> Thank you in advance,
>
> Daniel
>
> On Wed, Feb 28, 2018 at 6:51 PM, Nicolas Goaziou <mail@nicolasgoaziou.fr> wrote:
>> Hello,
>>
>> Daniel P Gomez <gomez.danp@gmail.com> writes:
>>
>>> If I include a file child.org in parent.org using the #+INCLUDE
>>> derivative, and the following two conditions are true:
>>>
>>> 1. child.org and parent.org are not in the same directory nor share
>>> directory structure,
>>> 2. child.org contains file links with relative file paths,
>>>
>>> then exporting parent.org will produce a file with broken links.
>>>
>>> Is there a way to have org sort this out?
>>>
>>> Or even make this optional,
>>> say by using:
>>>
>>> #+INCLUDE: child.org :fix-paths t
>>>
>>> I guess the quickest (but perhaps not cleanest) way to have this
>>> feature would be to convert relative paths in file links into absolute
>>> paths within `org-export-expand-include-keyword`. I'm not that well
>>> versed in Elisp yet to do this yet, though.
>>>
>>> Are there any known solutions to this, or suggestions on how to get it
>>> working?
>>
>> As you suggest, I would use a parse tree filter that turns every
>> relative file link into an absolute one.
>>
>> Regards,
>>
>> --
>> Nicolas Goaziou

[-- Attachment #2: 0001-Add-support-for-absolute-paths-in-INCLUDE-derivative.patch --]
[-- Type: application/octet-stream, Size: 3601 bytes --]

From c52a993f91567d87eb87ded907dcccd83a0f2ed3 Mon Sep 17 00:00:00 2001
From: Daniel Gomez <d.gomez@posteo.org>
Date: Thu, 1 Mar 2018 01:49:54 +0100
Subject: [PATCH 1/1] Add support for :absolute-paths in #+INCLUDE derivatives.

---
 lisp/ox.el | 32 +++++++++++++++++++++++++++++---
 1 file changed, 29 insertions(+), 3 deletions(-)

diff --git a/lisp/ox.el b/lisp/ox.el
index bd49a8a26..b6f6e576e 100644
--- a/lisp/ox.el
+++ b/lisp/ox.el
@@ -3302,6 +3302,11 @@ storing and resolving footnotes.  It is created automatically."
 				       value)
 			 (prog1 (org-not-nil (match-string 1 value))
 			   (setq value (replace-match "" nil nil value)))))
+		   (absolute-paths
+                    (and (string-match ":absolute-paths *\\([^: \r\t\n]\\S-*\\)?"
+                                       value)
+                         (prog1 (org-not-nil (match-string 1 value))
+                           (setq value (replace-match "" nil nil value)))))
 		   (lines
 		    (and (string-match
 			  ":lines +\"\\(\\(?:[0-9]+\\)?-\\(?:[0-9]+\\)?\\)\""
@@ -3373,7 +3378,8 @@ storing and resolving footnotes.  It is created automatically."
 			 (or
 			  (gethash file file-prefix)
 			  (puthash file (cl-incf current-prefix) file-prefix))
-			 footnotes)))
+			 footnotes
+			 absolute-paths)))
 		     (org-export-expand-include-keyword
 		      (cons (list file lines) included)
 		      (file-name-directory file)
@@ -3451,7 +3457,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 absolute-paths)
   "Prepare contents of FILE for inclusion and return it as a string.
 
 When optional argument LINES is a string specifying a range of
@@ -3473,7 +3479,10 @@ This is useful to avoid conflicts when more than one Org file
 with footnotes is included in a document.
 
 Optional argument FOOTNOTES is a hash-table to store footnotes in
-the included document."
+the included document.
+
+Optional argument ABSOLUTE-PATHS, when non-nil, toggles the convertion
+of all relative paths in the included document into absolute paths."
   (with-temp-buffer
     (insert-file-contents file)
     (when lines
@@ -3533,6 +3542,23 @@ the included document."
 		(lambda ()
 		  (if (< offset 0) (delete-char (abs offset))
 		    (insert (make-string offset ?*)))))))))))
+    ;;; When ABSOLUTE-PATHS is specified, all paths from links of type
+    ;;; "file" that are present in the file being included are
+    ;;; converted to absolute and canonicalized.
+    (when absolute-paths
+      (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= (org-element-property :type link) "file")
+            (let* ((old-path (org-element-property :path link))
+                   (new-path (expand-file-name old-path
+                                               (file-name-directory file)))
+                   (remove (list (org-element-property :begin link)
+                                 (org-element-property :end link))))
+              (apply #'delete-region remove)
+              (insert "[[file:" new-path "]]"))))))
     ;; Append ID to all footnote references and definitions, so they
     ;; become file specific and cannot collide with footnotes in other
     ;; included files.  Further, collect relevant footnote definitions
-- 
2.16.2


  reply	other threads:[~2018-03-01  1:01 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 [this message]
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
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=CAJTMgxg+Tw8rfcDsL29wTv3GXsJhYkZmGXdEH6JDgLYv-ky_OQ@mail.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).