emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Nicolas Goaziou <mail@nicolasgoaziou.fr>
To: Daniel P Gomez <gomez.danp@gmail.com>
Cc: emacs-orgmode@gnu.org
Subject: Re: How to keep correct filepaths when using the #+INCLUDE derivative?
Date: Sat, 03 Mar 2018 02:24:28 +0100	[thread overview]
Message-ID: <878tbahrxv.fsf@nicolasgoaziou.fr> (raw)
In-Reply-To: <CAJTMgxihg-gyPkgjyzX89ejcbDfNP8OuZ-QHuu9sfO+AvmGt3A@mail.gmail.com> (Daniel P. Gomez's message of "Fri, 2 Mar 2018 15:16:21 +0100")

Hello,

Daniel P Gomez <gomez.danp@gmail.com> writes:

> 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.

Thank you.

> 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.

`org-export--prepare-file-contents' is called from the including
document, so you can get its path with (buffer-file-name
(buffer-base-buffer)).

However, we need to handle the case where the including buffer is not
associated to a file, i.e., the Sexp above returns nil.

> +    (goto-char (point-min))
> +    (while (re-search-forward org-any-link-re nil t)
> +      (let ((link (save-excursion
> +		    (backward-char)
> +		    (org-element-context))))

It would be nice to add a comment explaining what we are going to do.

> +	(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)))))

I suggest the following inner part:

    (when (string= "file" (org-element-property :type link))
      (let* ((old-path (org-element-property :path link))
             (new-path (expand-file-name old-path (file-name-directory file))))
        (delete-region (org-element-property :begin link)
                       (org-element-property :end link))
        (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))))
                  (org-element-interpret-data new)))))
              
Also, would you mind adding a test in "text-ox.el", within
`test-org-export/expand-include'?

Regards,

-- 
Nicolas Goaziou

  reply	other threads:[~2018-03-03  1:24 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 [this message]
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=878tbahrxv.fsf@nicolasgoaziou.fr \
    --to=mail@nicolasgoaziou.fr \
    --cc=emacs-orgmode@gnu.org \
    --cc=gomez.danp@gmail.com \
    /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).