Improve attachment link export functionality

Make attachment links export just as file links.

* lisp/ox-ascii.el
* lisp/ox-html.el
* lisp/ox-latex.el
* lisp/ox-man.el
* lisp/ox-md.el
* lisp/ox-texinfo.el
* lisp/org-attach.el: Move export functionality for attachment links
  into the respective export backend, for feature parity with file
  links.
This commit is contained in:
Gustav Wikström 2020-01-14 03:07:53 +01:00
parent 32aa7139fd
commit d70db54dbc
8 changed files with 116 additions and 48 deletions

View File

@ -637,7 +637,6 @@ Basically, this adds the path to the attachment directory."
(org-link-set-parameters "attachment"
:follow #'org-attach-open-link
:export #'org-attach-export-link
:complete #'org-attach-complete-link)
(defun org-attach-open-link (link &optional in-emacs)
@ -678,26 +677,6 @@ and to use an external application to visit the file."
(t (concat "attachment:" file))))
(error "No attachment directory exist"))))
(defun org-attach-export-link (link description format)
"Translate attachment LINK from Org mode format to exported FORMAT.
Also includes the DESCRIPTION of the link in the export."
(save-excursion
(let (path desc)
(cond
((string-match "::\\([0-9]+\\)\\'" link)
(setq link (substring link 0 (match-beginning 0))))
((string-match "::\\(.+\\)\\'" link)
(setq link (substring link 0 (match-beginning 0)))))
(setq path (file-relative-name (org-attach-expand link))
desc (or description link))
(pcase format
(`html (format "<a target=\"_blank\" href=\"%s\">%s</a>" path desc))
(`latex (format "\\href{%s}{%s}" path desc))
(`texinfo (format "@uref{%s,%s}" path desc))
(`ascii (format "%s (%s)" desc path))
(`md (format "[%s](%s)" desc path))
(_ path)))))
(defun org-attach-archive-delete-maybe ()
"Maybe delete subtree attachments when archiving.
This function is called by `org-archive-hook'. The option

View File

@ -31,7 +31,10 @@
(require 'ox-publish)
(require 'cl-lib)
;;; Function Declarations
(declare-function aa2u "ext:ascii-art-to-unicode" ())
(declare-function org-attach-expand "org-attach" (file))
;;; Define Back-End
;;
@ -1567,13 +1570,24 @@ CONTENTS is nil. INFO is a plist holding contextual
DESC is the description part of the link, or the empty string.
INFO is a plist holding contextual information."
(let ((type (org-element-property :type link)))
(let* ((raw-type (org-element-property :type link))
(type (if (string= raw-type "attachment")
;; Attachments are simplified representations of
;; file links. When exporting, expose attachments
;; as if they were file links.
"file"
raw-type))
(raw-path (org-element-property :path link))
(path (cond
((string= raw-type "attachment")
(setq raw-path (file-relative-name (org-attach-expand raw-path)))
(concat type ":" raw-path))
(t (concat type ":" raw-path)))))
(cond
((org-export-custom-protocol-maybe link desc 'ascii))
((string= type "coderef")
(let ((ref (org-element-property :path link)))
(format (org-export-get-coderef-format ref desc)
(org-export-resolve-coderef ref info))))
(format (org-export-get-coderef-format path desc)
(org-export-resolve-coderef path info)))
;; Do not apply a special syntax on radio links. Though, use
;; transcoded target's contents as output.
((string= type "radio") desc)
@ -1605,13 +1619,10 @@ INFO is a plist holding contextual information."
;; Don't know what to do. Signal it.
(_ "???"))))
(t
(let ((raw-link (concat (org-element-property :type link)
":"
(org-element-property :path link))))
(if (not (org-string-nw-p desc)) (format "<%s>" raw-link)
(concat (format "[%s]" desc)
(and (not (plist-get info :ascii-links-to-notes))
(format " (<%s>)" raw-link)))))))))
(if (not (org-string-nw-p desc)) (format "<%s>" path)
(concat (format "[%s]" desc)
(and (not (plist-get info :ascii-links-to-notes))
(format " (<%s>)" path))))))))
;;;; Node Properties

View File

@ -42,6 +42,7 @@
(declare-function org-id-find-id-file "org-id" (id))
(declare-function htmlize-region "ext:htmlize" (beg end))
(declare-function mm-url-decode-entities "mm-url" ())
(declare-function org-attach-expand "org-attach" (file))
(defvar htmlize-css-name-prefix)
(defvar htmlize-output-type)
@ -884,10 +885,9 @@ link to the image."
:type 'boolean)
(defcustom org-html-inline-image-rules
'(("file" . "\\.\\(jpeg\\|jpg\\|png\\|gif\\|svg\\)\\'")
("attachment" . "\\.\\(jpeg\\|jpg\\|png\\|gif\\|svg\\)\\'")
("http" . "\\.\\(jpeg\\|jpg\\|png\\|gif\\|svg\\)\\'")
("https" . "\\.\\(jpeg\\|jpg\\|png\\|gif\\|svg\\)\\'"))
`(("file" . ,(regexp-opt '(".jpeg" ".jpg" ".png" ".gif" ".svg")))
("http" . ,(regexp-opt '(".jpeg" ".jpg" ".png" ".gif" ".svg")))
("https" . ,(regexp-opt '(".jpeg" ".jpg" ".png" ".gif" ".svg"))))
"Rules characterizing image files that can be inlined into HTML.
A rule consists in an association whose key is the type of link
to consider, and value is a regexp that will be matched against
@ -3064,7 +3064,13 @@ INFO is a plist holding contextual information. See
(concat (file-name-sans-extension raw-path) "."
(plist-get info :html-extension)))
(t raw-path))))
(type (org-element-property :type link))
(raw-type (org-element-property :type link))
(type (if (string= raw-type "attachment")
;; Attachments are simplified representations of
;; file links. When exporting, expose attachments
;; as if they were file links.
"file"
raw-type))
(raw-path (org-element-property :path link))
;; Ensure DESC really exists, or set it to nil.
(desc (org-string-nw-p desc))
@ -3073,6 +3079,11 @@ INFO is a plist holding contextual information. See
((member type '("http" "https" "ftp" "mailto" "news"))
(url-encode-url (concat type ":" raw-path)))
((string= type "file")
;; Pre-parse the path from attachment-format to
;; file-format to make attachment links use all export
;; functionality from file links with correct pathing.
(when (string= raw-type "attachment")
(setq raw-path (org-attach-expand raw-path)))
;; During publishing, turn absolute file names belonging
;; to base directory into relative file names. Otherwise,
;; append "file" protocol to absolute file name.

View File

@ -30,6 +30,10 @@
(require 'ox)
(require 'ox-publish)
;;; Function Declarations
(declare-function org-attach-expand "org-attach" (file))
(defvar org-latex-default-packages-alist)
(defvar org-latex-packages-alist)
(defvar orgtbl-exp-regexp)
@ -737,6 +741,8 @@ environment."
(defcustom org-latex-inline-image-rules
`(("file" . ,(regexp-opt
'("pdf" "jpeg" "jpg" "png" "ps" "eps" "tikz" "pgf" "svg")))
("attachment" . ,(regexp-opt
'("pdf" "jpeg" "jpg" "png" "ps" "eps" "tikz" "pgf" "svg"))))
"Rules characterizing image files that can be inlined into LaTeX.
@ -2356,6 +2362,8 @@ LINK is the link pointing to the inline image. INFO is a plist
used as a communication channel."
(let* ((parent (org-export-get-parent-element link))
(path (let ((raw-path (org-element-property :path link)))
(when (string= (org-element-property :type link) "attachment")
(setq raw-path (file-relative-name (org-attach-expand raw-path))))
(if (not (file-name-absolute-p raw-path)) raw-path
(expand-file-name raw-path))))
(filetype (file-name-extension path))
@ -2511,7 +2519,13 @@ used as a communication channel."
DESC is the description part of the link, or the empty string.
INFO is a plist holding contextual information. See
`org-export-data'."
(let* ((type (org-element-property :type link))
(let* ((raw-type (org-element-property :type link))
(type (if (string= raw-type "attachment")
;; Attachments are simplified representations of
;; file links. When exporting, expose attachments
;; as if they were file links.
"file"
raw-type))
(raw-path (org-element-property :path link))
;; Ensure DESC really exists, or set it to nil.
(desc (and (not (string= desc "")) desc))
@ -2521,6 +2535,8 @@ INFO is a plist holding contextual information. See
(cond ((member type '("http" "https" "ftp" "mailto" "doi"))
(concat type ":" raw-path))
((string= type "file")
(when (string= raw-type "attachment")
(setq raw-path (file-relative-name (org-attach-expand raw-path))))
(org-export-file-uri raw-path))
(t
raw-path)))))

View File

@ -40,6 +40,10 @@
(require 'cl-lib)
(require 'ox)
;;; Function Declarations
(declare-function org-attach-expand "org-attach" (file))
(defvar org-export-man-default-packages-alist)
(defvar org-export-man-packages-alist)
(defvar orgtbl-exp-regexp)
@ -605,14 +609,23 @@ CONTENTS is nil. INFO is a plist holding contextual information."
DESC is the description part of the link, or the empty string.
INFO is a plist holding contextual information. See
`org-export-data'."
(let* ((type (org-element-property :type link))
(let* ((raw-type (org-element-property :type link))
(type (if (string= raw-type "attachment")
;; Attachments are simplified representations of
;; file links. When exporting, expose attachments
;; as if they were file links.
"file"
raw-type))
(raw-path (org-element-property :path link))
;; Ensure DESC really exists, or set it to nil.
(desc (and (not (string= desc "")) desc))
(path (cond
((member type '("http" "https" "ftp" "mailto"))
(concat type ":" raw-path))
((string= type "file") (org-export-file-uri raw-path))
((string= type "file")
(when (string= raw-type "attachment")
(setq raw-path (file-relative-name (org-attach-expand raw-path))))
(org-export-file-uri raw-path))
(t raw-path))))
(cond
;; Link type is handled by a special function.

View File

@ -33,6 +33,10 @@
(require 'ox-publish)
;;; Function Declarations
(declare-function org-attach-expand "org-attach" (file))
;;; User-Configurable Variables
(defgroup org-export-md nil
@ -396,12 +400,20 @@ INFO is a plist holding contextual information. See
(if (string= ".org" (downcase (file-name-extension raw-path ".")))
(concat (file-name-sans-extension raw-path) ".md")
raw-path)))
(type (org-element-property :type link))
(raw-path (raw-path (org-element-property :path link)))
(raw-type (org-element-property :type link))
(type (if (string= raw-type "attachment")
;; Attachments are simplified representations of
;; file links. When exporting, expose attachments
;; as if they were file links.
"file"
raw-type))
(raw-path (org-element-property :path link))
(path (cond
((member type '("http" "https" "ftp" "mailto"))
(concat type ":" path))
(concat type ":" raw-path))
((string= type "file")
(when (string= raw-type "attachment")
(setq raw-path (file-relative-name (org-attach-expand raw-path))))
(org-export-file-uri (funcall link-org-files-as-md raw-path)))
(t raw-path))))
(cond
@ -432,7 +444,7 @@ INFO is a plist holding contextual information. See
(org-export-get-reference destination info))))
(_
(let ((description
(or (org-string-nw-p contents)
(or (org-string-nw-p desc)
(let ((number (org-export-get-ordinal destination info)))
(cond
((not number) nil)

View File

@ -32,6 +32,10 @@
(require 'ox)
(require 'table nil 'noerror)
;;; Function Declarations
(declare-function org-attach-expand "org-attach" (file))
;;; Define Back-End
(org-export-define-backend 'odt
@ -2692,7 +2696,13 @@ Return nil, otherwise."
DESC is the description part of the link, or the empty string.
INFO is a plist holding contextual information. See
`org-export-data'."
(let* ((type (org-element-property :type link))
(let* ((raw-type (org-element-property :type link))
(type (if (string= raw-type "attachment")
;; Attachments are simplified representations of
;; file links. When exporting, expose attachments
;; as if they were file links.
"file"
raw-type))
(raw-path (org-element-property :path link))
;; Ensure DESC really exists, or set it to nil.
(desc (and (not (string= desc "")) desc))
@ -2701,7 +2711,10 @@ INFO is a plist holding contextual information. See
(path (cond
((member type '("http" "https" "ftp" "mailto"))
(concat type ":" raw-path))
((string= type "file") (org-export-file-uri raw-path))
((string= type "file")
(when (string= raw-type "attachment")
(setq raw-path (file-relative-name (org-attach-expand raw-path))))
(org-export-file-uri raw-path))
(t raw-path)))
;; Convert & to &amp; for correct XML representation
(path (replace-regexp-in-string "&" "&amp;" path)))

View File

@ -28,6 +28,10 @@
(require 'cl-lib)
(require 'ox)
;;; Function Declarations
(declare-function org-attach-expand "org-attach" (file))
(defvar orgtbl-exp-regexp)
@ -1045,14 +1049,23 @@ nil."
DESC is the description part of the link, or the empty string.
INFO is a plist holding contextual information. See
`org-export-data'."
(let* ((type (org-element-property :type link))
(let* ((raw-type (org-element-property :type link))
(type (if (string= raw-type "attachment")
;; Attachments are simplified representations of
;; file links. When exporting, expose attachments
;; as if they were file links.
"file"
raw-type))
(raw-path (org-element-property :path link))
;; Ensure DESC really exists, or set it to nil.
(desc (and (not (string= desc "")) desc))
(path (cond
((member type '("http" "https" "ftp"))
(concat type ":" raw-path))
((string= type "file") (org-export-file-uri raw-path))
((string= type "file")
(when (string= raw-type "attachment")
(setq raw-path (org-attach-expand raw-path)))
(org-export-file-uri raw-path))
(t raw-path))))
(cond
((org-export-custom-protocol-maybe link desc 'texinfo))