Skip to content

Commit

Permalink
Add an audio shortcode to embed audio file.
Browse files Browse the repository at this point in the history
Close #5
  • Loading branch information
coatless committed Feb 11, 2024
1 parent 2c0db6e commit 1271375
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 1 deletion.
1 change: 1 addition & 0 deletions _extensions/embedio/_extension.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ version: 0.0.0-dev.1
quarto-required: ">=1.4.549"
contributes:
shortcodes:
- audio.lua
- pdf.lua
- revealjs.lua
72 changes: 72 additions & 0 deletions _extensions/embedio/audio.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
local function audio(args, kwargs, meta)

if not quarto.doc.is_format("html") then
return
end

-- Start of HTML tag
local htmlTable = {"<figure "}

-- Extracting caption and class from kwargs
local caption = pandoc.utils.stringify(kwargs.caption)
local class = pandoc.utils.stringify(kwargs.class)

-- Add class attribute if provided
if class then
table.insert(htmlTable, 'class="' .. class .. '">')
end

-- Add caption if provided
if caption then
table.insert(htmlTable, '<figcaption>' .. caption .. '</figcaption>')
end

-- Start the audio tag
table.insert(htmlTable, "<audio controls")

-- Extracting input from args or kwargs
local input = args[1] or kwargs.file
input = pandoc.utils.stringify(input)

-- Add source attribute with input file path
if input then
table.insert(htmlTable, ' src="' .. input .. '"')
else
quarto.log.error("Error: Audio file path is required for the audio shortcode.")
assert(false)
end

-- Automatically detect file type from the file extension
local fileType = string.match(input, "%.([^%.]+)$")

if fileType then
table.insert(htmlTable, ' type="audio/' .. fileType .. '">')
else
quarto.log.error("Error: Unable to detect file type from the audio file path.")
assert(false)
end

-- Add source element for browsers that do not support the audio tag
table.insert(htmlTable, 'Your browser does not support the audio tag.')

-- Add closing audio tag
table.insert(htmlTable, "</audio>")

-- Extract download option
local download = pandoc.utils.stringify(kwargs.download) or "false"

-- Add download link if provided
if download == "true" then
table.insert(htmlTable, '<a href="' .. input ..'"> Download audio </a>')
end

-- Add closing figure tag
table.insert(htmlTable, "</figure>")

return pandoc.RawBlock('html', table.concat(htmlTable))
end


return {
['audio'] = audio
}
3 changes: 2 additions & 1 deletion docs/_quarto.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ website:
href: qembedio-embed-pdf.qmd
- text: "RevealJS"
href: qembedio-embed-revealjs.qmd
- text: "HTML"
- text: "Audio"
href: qembedio-embed-audio.qmd
- text: "HTML"
- text: "Maps"
- text: "Social"
- section: "Support"
Expand Down
14 changes: 14 additions & 0 deletions docs/qembedio-embed-audio.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
title: "Embed Audio"
---

The `audio` short code generates an embedded audio object for HTML websites. You can use it with:

```markdown
{{{< audio file="assets/test.mp3" >}}}
```

For example, we can show the `test.mp3` with:

{{< audio file="assets/test.mp3" >}}

0 comments on commit 1271375

Please sign in to comment.