emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Links & images with different attributes in the same paragraph
@ 2023-11-11 15:54 Max Nikulin
  2023-12-05 13:46 ` Ihor Radchenko
  0 siblings, 1 reply; 10+ messages in thread
From: Max Nikulin @ 2023-11-11 15:54 UTC (permalink / raw)
  To: emacs-orgmode

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



^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2023-12-21 14:01 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-11-11 15:54 Links & images with different attributes in the same paragraph Max Nikulin
2023-12-05 13:46 ` Ihor Radchenko
2023-12-12 11:08   ` Max Nikulin
2023-12-12 13:18     ` Ihor Radchenko
2023-12-13 11:50       ` Max Nikulin
2023-12-14 15:23         ` Ihor Radchenko
2023-12-16  8:06           ` Max Nikulin
2023-12-16 14:44             ` Ihor Radchenko
2023-12-19 14:39               ` Max Nikulin
2023-12-21 14:03                 ` Ihor Radchenko

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

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