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; 2+ 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] 2+ messages in thread

* Re: Links & images with different attributes in the same paragraph
  2023-11-11 15:54 Links & images with different attributes in the same paragraph Max Nikulin
@ 2023-12-05 13:46 ` Ihor Radchenko
  0 siblings, 0 replies; 2+ messages in thread
From: Ihor Radchenko @ 2023-12-05 13:46 UTC (permalink / raw)
  To: Max Nikulin; +Cc: emacs-orgmode

Max Nikulin <manikulin@gmail.com> writes:

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

We definitely need something to fine-tune export properties inline.
However, I am skeptical about using links for this purpose in Org
proper, not individual user configs.

This is because link description cannot contain arbitrary objects. In
particular, one cannot put other links, radio-targets, line-breaks,
citations, etc.
I believe that the right way to go here is what we previously discussed
as inline special block equivalent. That way, we will not need to create
workarounds with special link type.

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>


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

end of thread, other threads:[~2023-12-05 13:44 UTC | newest]

Thread overview: 2+ 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

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