emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [O] How to keep correct filepaths when using the #+INCLUDE derivativ


From: Nicolas Goaziou
Subject: Re: [O] How to keep correct filepaths when using the #+INCLUDE derivative?
Date: Sat, 03 Mar 2018 02:24:28 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.3 (gnu/linux)

Hello,

Daniel P Gomez <address@hidden> 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 via email to

[Prev in Thread] Current Thread [Next in Thread]