emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Max Nikulin <manikulin@gmail.com>
To: emacs-orgmode@gnu.org
Subject: Links & images with different attributes in the same paragraph
Date: Sat, 11 Nov 2023 22:54:37 +0700	[thread overview]
Message-ID: <uio840$qde$1@ciao.gmane.io> (raw)


I raised this topic earlier. It seems, there is a rather generic 
workaround that may help.

Sometimes it is necessary to assign specific export attributes to some 
links in a paragraph while others should get another set of them. Inline 
images should have different alt text.

--- 8< ---
Text with <<anchor>> in a paragraph.

Any link types are supported including
[[wrap::href "anchor"][internal]] ones.
Attributes may be added selectively:
<wrap::href "https://microsoft.com/" :attr_html ":class external \
:title Link that does not increase target page rank \
:noreferrer 1" :attr_html ":nofollow 1 :noopener 1">

Images in the same paragraph may have different =alt= attribute:
[[wrap::attr_html ":alt PNG Unicorn"
and vector
[[wrap::attr_html ":alt SVG Unicorn"
:href "https://orgmode.org/resources/img/org-mode-unicorn.svg"]]
--- >8 ---

is exported as

--- 8< ---
Text with <a id="org0424ec3"></a> in a paragraph.

Any link types are supported including
<a href="#org0424ec3">internal</a> ones.
Attributes may be added selectively:
<a href="https://microsoft.com/" class="external" title="Link that does 
not increase target page rank" noreferrer="1" nofollow="1" 

Images in the same paragraph may have different <code>alt</code> attribute:
alt="PNG Unicorn" />
and vector
<img src="https://orgmode.org/resources/img/org-mode-unicorn.svg" 
alt="SVG Unicorn" class="org-svg" />
--- >8 ---

and it requires not so much code:

# #+header: :eval never-export
#+begin_src elisp :exports results :results silent
   (defun nm/org-link-wrap-path-to-props (path)
     (read (concat "(" path ")")))

   (defun nm/org-link-wrap-copy-props (props link)
     (while props
       (let ((name (pop props))
   	  (value (pop props))
   	  (case-fold-search t))
          ;; `org-element--collect-affiliated-keywords'
          (if (string-match-p "\\`:attr_" (symbol-name name))
   	   (append (org-element-property name link) (list value))

   (defun nm/org-link-wrap-export
       (path description backend info)
     (let* ((props-all (nm/org-link-wrap-path-to-props path))
   	 (href (plist-get props-all :href))
   	 ;; `org-plist-delete' removes other duplicated keys as well.
   	 (props (map-delete props-all :href))
   	 ;; Taken from `org-link-open-from-string'.
   	 (link (with-temp-buffer
   		 (let ((org-inhibit-startup nil))
   		   (insert "[[" (org-link-escape href) "]]")
   		   (goto-char (point-min))
       (nm/org-link-wrap-copy-props props link)
       (when description
         (org-element-set-contents link description))
        (org-export-data-with-backend link backend info))))

   (defun nm/org-link-wrap-follow (path arg)
     (let* ((props (nm/org-link-wrap-path-to-props path))
   	 (href (plist-get props :href)))
        (concat "[[" (org-link-escape href) "]]")

    :follow #'nm/org-link-wrap-follow
    :export #'nm/org-link-wrap-export)

An example of earlier discussion:
Max Nikulin. [BUG] manual: confusing example of adding attributes to a 
link (affiliated keywords). Mon, 20 Jun 2022 23:25:29 +0700

                 reply	other threads:[~2023-11-11 15:56 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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:

  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='uio840$qde$1@ciao.gmane.io' \
    --to=manikulin@gmail.com \
    --cc=emacs-orgmode@gnu.org \


* 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


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