From fd6d686bb9e09981d05cbfc0ddab74dc8bcceb99 Mon Sep 17 00:00:00 2001 From: Max Nikulin Date: Fri, 20 Oct 2023 17:29:03 +0700 Subject: [PATCH 2/3] ox-ascii.el: Refactor link export * lisp/ox-ascii.el (org-ascii--describe-links, org-ascii-link): Avoid duplication of fragments of code. (org-ascii-link-inline): A new helper function for `org-ascii-link'. Prepare to expanding `org-link-parameters' :export protocol to allow export of custom links as notes at the end of headings. --- lisp/ox-ascii.el | 56 +++++++++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 25 deletions(-) diff --git a/lisp/ox-ascii.el b/lisp/ox-ascii.el index ae4273489..10bb1fce7 100644 --- a/lisp/ox-ascii.el +++ b/lisp/ox-ascii.el @@ -946,11 +946,13 @@ (defun org-ascii--describe-links (links width info) (lambda (link) (let* ((type (org-element-property :type link)) (description (org-element-contents link)) + (raw-link (org-element-property :raw-link link)) (anchor (org-export-data - (or description (org-element-property :raw-link link)) - info))) + (or description raw-link) + info)) + location) (cond - ((member type '("coderef" "radio")) nil) + ((member type '("coderef" "radio"))) ((member type '("custom-id" "fuzzy" "id")) ;; Only links with a description need an entry. Other are ;; already handled in `org-ascii-link'. @@ -963,25 +965,24 @@ (defun org-ascii--describe-links (links width info) (condition-case nil (org-export-resolve-id-link link info) (org-link-broken nil))))) - (when dest - (concat - (org-ascii--fill-string - (format "[%s] %s" anchor (org-ascii--describe-datum dest info)) - width info) - "\n\n"))))) + (setq location + (and dest (org-ascii--describe-datum dest info)))))) ;; Do not add a link that cannot be resolved and doesn't have ;; any description: destination is already visible in the ;; paragraph. - ((not (org-element-contents link)) nil) + ((not description)) ;; Do not add a link already handled by custom export ;; functions. ((org-export-custom-protocol-maybe link anchor 'ascii info) nil) - (t - (concat - (org-ascii--fill-string - (format "[%s] <%s>" anchor (org-element-property :raw-link link)) - width info) - "\n\n"))))) + (t (setq location (format "<%s>" raw-link)))) + (and + location + anchor + (concat + (org-ascii--fill-string + (format "[%s] %s" anchor location) + width info) + "\n\n")))) links "")) (defun org-ascii--checkbox (item info) @@ -1584,6 +1585,15 @@ (defun org-ascii-line-break (_line-break _contents _info) ;;;; Link +(defun org-ascii-link-inline (link desc info) + (cond + ((not desc) link) + ((plist-get info :ascii-links-to-notes) + (format "[%s]" desc)) + ((string-match-p "\\`(.*)\\'" link) + (format "[%s] %s" desc link)) + (t (format "[%s] (%s)" desc link)))) + (defun org-ascii-link (link desc info) "Transcode a LINK object from Org to ASCII. @@ -1605,11 +1615,10 @@ (defun org-ascii-link (link desc info) (org-export-resolve-id-link link info)))) (pcase (org-element-type destination) ((guard desc) - (if (plist-get info :ascii-links-to-notes) - (format "[%s]" desc) - (format "[%s] (%s)" - desc - (org-ascii--describe-datum destination info)))) + (org-ascii-link-inline + (org-ascii--describe-datum destination info) + desc + info)) ;; External file. (`plain-text destination) (`headline @@ -1628,10 +1637,7 @@ (defun org-ascii-link (link desc info) (_ "???")))) (t (let ((path (org-element-property :raw-link 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))))))))) + (org-ascii-link-inline (format "<%s>" path) desc info)))))) ;;;; Node Properties -- 2.39.2