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: Fri, 2 Mar 2018 15:16:21 +0100 [thread overview]
Message-ID: <CAJTMgxihg-gyPkgjyzX89ejcbDfNP8OuZ-QHuu9sfO+AvmGt3A@mail.gmail.com> (raw)
In-Reply-To: <87h8pzifk7.fsf@nicolasgoaziou.fr>
[-- Attachment #1: Type: text/plain, Size: 2300 bytes --]
I've adapted the code such that it handles both bracketed and
unbracketed links, and links with descriptions.
As it is now, the changes are always automatically applied.
> Also, rewriting needs not be always absolute path, if both directories
> share a common root.
I couldn't find a simple way of rewriting links without making them
absolute, as `org-export--prepare-file-contents` does not have access
to the path of the including file, only of the included file.
On Thu, Mar 1, 2018 at 11:42 PM, Nicolas Goaziou <mail@nicolasgoaziou.fr> wrote:
> Hello,
>
> Daniel P Gomez <gomez.danp@gmail.com> writes:
>
>> Currently when passing the :absolute-paths toggle to an include
>> derivative, as in :
>>
>> #+INCLUDE: file.org :absolute-paths t
>>
>> The function `org-export--prepare-file-contents` will automatically
>> deduce the directory from file.org and adapt links by calling:
>> `(new-path (expand-file-name old-path (file-name-directory file)))`.
>> I could either make this a default option, such that links get
>> corrected but users can overwrite this by calling `:absolute-paths
>> nil`, or I could completely remove this toggle and always correct
>> links no matter what.
>
> I am wondering about the latter. If there is no reason to keep broken
> file names, it makes sense to automatically apply it.
>
> Also, rewriting needs not be always absolute path, if both directories
> share a common root.
>
>> One question regarding the implementation, currently I'm deleting the
>> link with a call to `delete-region` and using `(insert "[[file:"
>> new-path "]]")` to insert the corrected one. This does not take into
>> consideration whether links are bracketed or not ( is there a
>> functional difference if links are not bracketed?).
>
> Yes. White spaces are handled differently in each link type.
>
>> Also, my approach completely disregards link descriptions, which may
>> be relevant if the linked file would be, for example, an html
>> document. Would there be a cleaner org approach to replace the path
>> keeping the description?
>
> You could check :contents-begin and :contents-end for the link. If they
> are not nil, extract this region, and insert it again in the new link.
>
> Regards,
>
> --
> Nicolas Goaziou 0x80A93738
[-- Attachment #2: 0001-Fix-file-links-when-using-INCLUDE.patch --]
[-- Type: application/octet-stream, Size: 1778 bytes --]
From feccfe829ea9fa4f44c47d8c845ed602d2945dcd 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 | 28 ++++++++++++++++++++++++++++
1 file changed, 28 insertions(+)
diff --git a/lisp/ox.el b/lisp/ox.el
index bd49a8a26..8f0aca989 100644
--- a/lisp/ox.el
+++ b/lisp/ox.el
@@ -3476,6 +3476,34 @@ Optional argument FOOTNOTES is a hash-table to store footnotes in
the included document."
(with-temp-buffer
(insert-file-contents file)
+ (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* ((has-bracket (string=
+ (org-element-property :format link) "bracket"))
+ (has-content (org-element-property :contents-begin link))
+ (old-path (org-element-property :path link))
+ (new-path (expand-file-name old-path
+ (file-name-directory file)))
+ (raw-new-link
+ (concat "file:" new-path))
+ (new-link
+ (cond
+ ((and has-bracket (not has-content))
+ (concat "[[" raw-new-link "]]"))
+ ((and has-bracket has-content)
+ (let ((description
+ (buffer-substring
+ (org-element-property :contents-begin link)
+ (org-element-property :contents-end link))))
+ (concat "[[" raw-new-link "][" description "]]")))
+ (t raw-new-link))))
+ (apply #'delete-region (list (org-element-property :begin link)
+ (org-element-property :end link)))
+ (insert new-link)))))
(when lines
(let* ((lines (split-string lines "-"))
(lbeg (string-to-number (car lines)))
--
2.16.2
next prev parent reply other threads:[~2018-03-02 14:16 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 [this message]
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=CAJTMgxihg-gyPkgjyzX89ejcbDfNP8OuZ-QHuu9sfO+AvmGt3A@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).