emacs-orgmode
[Top][All Lists]
Advanced

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

Links & images with different attributes in the same paragraph


From: Max Nikulin
Subject: Links & images with different attributes in the same paragraph
Date: Sat, 11 Nov 2023 22:54:37 +0700
User-agent: Mozilla Thunderbird

Hi,

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:
raster
[[wrap::attr_html ":alt PNG Unicorn"
:href "https://orgmode.org/worg/images/orgmode/org-mode-unicorn-original-logo.png";]]
and vector
[[wrap::attr_html ":alt SVG Unicorn"
:href "https://orgmode.org/resources/img/org-mode-unicorn.svg";]]
images.
--- >8 ---

is exported as

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

<p>
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" noopener="1">https://microsoft.com/</a>
</p>

<p>
Images in the same paragraph may have different <code>alt</code> attribute:
raster
<img src="https://orgmode.org/worg/images/orgmode/org-mode-unicorn-original-logo.png"; alt="PNG Unicorn" />
and vector
<img src="https://orgmode.org/resources/img/org-mode-unicorn.svg"; alt="SVG Unicorn" class="org-svg" />
images.
</p>
--- >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-put-property
         link
         name
         ;; `org-element--collect-affiliated-keywords'
         (if (string-match-p "\\`:attr_" (symbol-name name))
           (append (org-element-property name link) (list value))
         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) "]]")
                   (org-mode)
                   (goto-char (point-min))
                   (org-element-link-parser)))))
      (nm/org-link-wrap-copy-props props link)
      (when description
        (org-element-set-contents link description))
      (org-no-properties
       (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)))
      (org-link-open-from-string
       (concat "[[" (org-link-escape href) "]]")
       arg)))

  (org-link-set-parameters
   "wrap"
   :follow #'nm/org-link-wrap-follow
   :export #'nm/org-link-wrap-export)
#+end_src

An example of earlier discussion:
https://list.orgmode.org/t8q71r$mgv$1@ciao.gmane.io/
Max Nikulin. [BUG] manual: confusing example of adding attributes to a link (affiliated keywords). Mon, 20 Jun 2022 23:25:29 +0700




reply via email to

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