8

I would like to add a markup to org-mode, to simplify adding <audio> HTML tags. Like other inline markup:

In Thai, /sabai dee mai/ is pronounced as [audio:assets/sabai.mp3]

Which would export as:

<p>In Thai, <i>sabai dee mai</i> is pronounced as <audio src="assets/sabai.mp3"></audio></p>

I looked at org.el and ox-html.el to perhaps just copy how bold and such inline markup works, but I don't see how the pieces fit together.

Gambhiro
  • 245
  • 1
  • 9

3 Answers3

9

Org is extremely versatile when it comes to defining links, so they're probably your best bet. For that you need the org-add-link-type function.

(org-add-link-type "audio" #'ignore #'endless/export-audio-link)

The second argument is telling org you don't care about opening audio links for now, and the third argument says how to export them to html. Use the following example function, or improve it to your needs.

(defun endless/export-audio-link (path desc format)
  "Export org audio links to hmtl."
  (cl-case format
    (html (format "<audio src=\"%s\">%s</audio>" path (or desc "")))
    (latex (format "(HOW DO I EXPORT AUDIO TO LATEX? \"%s\")" path))))

With the above configuration, the links

[[audio:file.mp3][description]]
[[audio:file-2.mp3]]

would export to

<audio src="file.mp3">description</audio>
<audio src="file-2.mp3"></audio>

This post also explains how you can define a way to actually open these audio links inside org-mode (by using the second argument).

Malabarba
  • 22,878
  • 6
  • 78
  • 163
5

You can use the org-mode macro replacement to achieve this.

A macro called AUDIO can be defined as below

#+MACRO AUDIO @@html:<audio src="$1"></audio>@@

Using your example, here is what its usage will look like:

In Thai, /sabai dee mai/ is pronounced as {{{AUDIO(assets/sabai.mp3)}}}

which would export to html as:

<p>In Thai, <i>sabai dee mai</i> is pronounced as <audio src="assets/sabai.mp3"></audio></p>
Kaushal Modi
  • 25,203
  • 3
  • 74
  • 179
3

This post describes you can alter existing markup to produce any kind of tag you want. I'm not aware of any way of defining new markup with this, but, since ~ and = are redundant when exporting to html, you can change one of the two while still using the other as <code>.

The following snippet should turn ~sabai.mp3~ into <audio src="sabai.mp3"></audio> when exporting.

(eval-after-load 'ox-html
  '(push '(code . "<audio src="%s"></audio>") org-html-text-markup-alist))

And the following will do the same with =sabai.mp3= instead.

(eval-after-load 'ox-html
  '(push '(verbatim . "<audio src="%s"></audio>") org-html-text-markup-alist))
Malabarba
  • 22,878
  • 6
  • 78
  • 163