mirror of
https://github.com/foo-dogsquared/asciidoctor-foodogsquared-extensions.git
synced 2025-01-30 22:57:56 +00:00
Add audio block for HTML5-modified
This commit is contained in:
parent
153e110489
commit
36dffaf3e4
@ -67,11 +67,82 @@ module Asciidoctor::Foodogsquared::Converters
|
||||
html.to_html(indent: 2)
|
||||
end
|
||||
|
||||
|
||||
# A modified version of the audio node except it can accept multiple
|
||||
# sources.
|
||||
def convert_audio(node)
|
||||
html = Nokogiri::HTML5::DocumentFragment.parse <<~HTML
|
||||
<figure>
|
||||
<audio></audio>
|
||||
</figure>
|
||||
HTML
|
||||
figure = html.first_element_child
|
||||
audio = figure.first_element_child
|
||||
|
||||
add_common_attributes node, figure
|
||||
add_boolean_attribute node, audio, %w[loop controls muted]
|
||||
|
||||
if node.attr? 'sources'
|
||||
_, sources = add_sources_elem node, audio, 'audio'
|
||||
|
||||
sources_download_links = sources.map do |src|
|
||||
%(<a href="#{src}">#{src}</a>)
|
||||
end
|
||||
fallback_text = html.document.parse "Download the audio at #{sources_download_links.join ', '}."
|
||||
else
|
||||
audio['src'] = node.attr 'target'
|
||||
fallback_text = html.document.parse "Download the audio at #{node.attr 'target'}."
|
||||
end
|
||||
|
||||
audio.add_child fallback_text
|
||||
|
||||
if node.title?
|
||||
html.document.create_element 'figcaption' do |block|
|
||||
block.inner_html = node.captioned_title
|
||||
figure.add_child block
|
||||
end
|
||||
end
|
||||
|
||||
html.to_html(indent: 2)
|
||||
end
|
||||
def add_common_attributes(node, html)
|
||||
html['id'] = node.id if node.id
|
||||
html['class'] = node.role if node.role
|
||||
html.add_class node.role unless node.role.nil?
|
||||
|
||||
html
|
||||
end
|
||||
|
||||
def add_attributes_from_node(node, html, options)
|
||||
options.each do |option|
|
||||
html[option] = node.attr option if node.attr? option
|
||||
end
|
||||
|
||||
html
|
||||
end
|
||||
|
||||
def add_boolean_attribute(node, html, options)
|
||||
options.each do |option|
|
||||
html[option] = option if node.option? option
|
||||
end
|
||||
|
||||
html
|
||||
end
|
||||
|
||||
def add_sources_elem(node, html, media_type)
|
||||
sources = node.attr('sources', '').split(',').each do |src|
|
||||
src = html.document.create_element 'source' do |block|
|
||||
block['src'] = src
|
||||
|
||||
type = MIME::Types.type_for(src).find do |mime|
|
||||
mime.media_type == media_type
|
||||
end
|
||||
block['type'] = type unless media_type.nil?
|
||||
end
|
||||
|
||||
html.add_child src
|
||||
end
|
||||
|
||||
[html, sources]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -65,4 +65,52 @@ describe Asciidoctor::Foodogsquared::Converters::HTML5Modified do
|
||||
actual = (Asciidoctor.convert input).chomp
|
||||
(expect actual).to eq expected.chomp
|
||||
end
|
||||
|
||||
it 'should have an audio block with multiple <source>' do
|
||||
input = <<~INPUT
|
||||
audio::hello.mp3[sources="hello.mp3,hello.webm"]
|
||||
INPUT
|
||||
|
||||
expected = <<~HTML
|
||||
<figure>
|
||||
<audio><source src="hello.mp3" type="audio/mpeg"><source src="hello.webm" type="audio/webm"><p>Download the audio at <a href="hello.mp3">hello.mp3</a>, <a href="hello.webm">hello.webm</a>.</p></audio>
|
||||
</figure>
|
||||
HTML
|
||||
|
||||
actual = (Asciidoctor.convert input).chomp
|
||||
(expect actual).to eq expected.chomp
|
||||
end
|
||||
|
||||
it 'should have an audio block with multiple <source> and a caption' do
|
||||
input = <<~INPUT
|
||||
.Making up stuff right now
|
||||
audio::./hello.mp3[sources="hello.mp3,hello.webm"]
|
||||
INPUT
|
||||
|
||||
expected = <<~HTML
|
||||
<figure>
|
||||
<audio><source src="hello.mp3" type="audio/mpeg"><source src="hello.webm" type="audio/webm"><p>Download the audio at <a href="hello.mp3">hello.mp3</a>, <a href="hello.webm">hello.webm</a>.</p></audio>
|
||||
<figcaption>Making up stuff right now</figcaption></figure>
|
||||
HTML
|
||||
|
||||
actual = (Asciidoctor.convert input).chomp
|
||||
(expect actual).to eq expected.chomp
|
||||
end
|
||||
|
||||
it 'should have an audio element with a caption, ID, and classes' do
|
||||
input = <<~INPUT
|
||||
[#audio-sample.reverse]
|
||||
.Making up stuff right now
|
||||
audio::./hello.mp3[sources="hello.mp3,hello.webm"]
|
||||
INPUT
|
||||
|
||||
expected = <<~HTML
|
||||
<figure id="audio-sample" class="reverse">
|
||||
<audio><source src="hello.mp3" type="audio/mpeg"><source src="hello.webm" type="audio/webm"><p>Download the audio at <a href="hello.mp3">hello.mp3</a>, <a href="hello.webm">hello.webm</a>.</p></audio>
|
||||
<figcaption>Making up stuff right now</figcaption></figure>
|
||||
HTML
|
||||
|
||||
actual = (Asciidoctor.convert input).chomp
|
||||
(expect actual).to eq expected.chomp
|
||||
end
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user